Skip to content

stub listener should support hosts that have no ipv4 at all #15615

Open

Description

@major0

systemd version the issue has been seen with

systemd 242 (242)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

Used distribution

Ubuntu 19.10

Expected behaviour you didn't see

host -6 resolution should be able to resolve DNS purely using IPv6.

Unexpected behaviour you saw

;; connection timed out; no servers could be reached

Steps to reproduce the problem

  1. Connected to a network which services both IPv4 and IPv6 addresses.
  2. Confirmed that resolvectl reports both an IPv4 and IPv6 server in the DNS server list.
  3. Confirmed that specifying the IPv6 address as reported by resolvectl worked as expected. E.g. host -6 www.google.com <ipv6addr>.
  4. Ran host -6 www.google.com w/out specifying a DNS server.
  5. received the error: ;; connection timed out; no servers could be reached

Activity

  1. poettering commented on Apr 28, 2020

    @poettering
    Member

    why would it matter which transport is used locally between the local client and local resolved?

    The thing is that on IPv4 there's a whole range of loopback addresses, i.e. everything in 127.0.0.0/8. We can thus pick an IP address of our own to bind our stub to, i.e. 127.0.0.53, leaving the primary loopback address 127.0.0.1 untouched. For IPv6 that doesn't exist, there's only a single loopback address defined, not a full range, hence we'd have to bind to ::1 there taking possession of that port on the primary loopback interface. Hence we figured, let's not do that, for local communcation there's no benefit of using Ipv6 anyway.

    So, why would you want to insist on using Ipv6 as host-internal transport here?

    Note that resolved will use Ipv6 just fine when talking to upstream hosts, even if you talk ipv4 to it locally

  2. added
    needs-reporter-feedback ❓
    on Apr 28, 2020
  3. major0 commented on Apr 29, 2020

    @major0
    Author

    There is a functional difference between resolving IPv6 addresses via an IPv4 DNS server, and resolving IPv6 via IPv6. When specifying host -6 you are requesting the later, which the 127.0.0.53 stub is not suitable for. The local resolver should listen on ::1 as well as a 127/8 address to satisfy both host -4 and host -6.

    With the default /etc/resolv.conf of:

    nameserver 127.0.0.53
    options edns0
    search example.org
    

    I get the following behavior:

    $ host -6 google.com
    ;; connection timed out; no servers could be reached
    

    If I manually modify the /etc/resolv.conf to include an IPv6 nameserver frmo HurricaneElectric:

    nameserver 127.0.0.53
    nameserver 2001:470:20::2
    options edns0
    search example.org
    

    Then everything works as expected:

    $ host -6 google.com
    google.com has address 172.217.14.206
    google.com has IPv6 address 2607:f8b0:4006:806::200e
    google.com mail is handled by 40 alt3.aspmx.l.google.com.
    google.com mail is handled by 30 alt2.aspmx.l.google.com.
    google.com mail is handled by 20 alt1.aspmx.l.google.com.
    google.com mail is handled by 10 aspmx.l.google.com.
    google.com mail is handled by 50 alt4.aspmx.l.google.com.
    

    As it currently stands, any application attempting to perform IPv6-only behavior (w/out IPv4 fallback) will fail DNS lookups.

  4. major0 commented on Apr 29, 2020

    @major0
    Author

    I honestly do not follow the argument of "why would you do it that way". The bottom line is that the system fails to work as expected given the current configuration enforced by systemd-resolved. When the base system fails to operate given a documented command, it seems the goal should be identifying and fixing the cause of the failure, which I believe has been done. Your suggestion seems to be that we should remove pure IPv6 lookup paths from the system. Are you saying I should open a bug against host-utils to remove host -6?

  5. major0 commented on Apr 29, 2020

    @major0
    Author

    To be clear, the solution appears to be for systemd-resolved to bind to also bind to ::1 and that would allow the rest of the OS resolver to work with IPv6-only paths.

    It is also curious that resolvectl -6 query google.com works by passing through the IPv6 path, but regular system commands which support an IPv6-only path will fail.

  6. poettering commented on Apr 29, 2020

    @poettering
    Member

    I don't get it. We don#t list an IPv6 server in the resolv.conf we provide, only an IPv4 one. So all software reading that should understand we only listen on Ipv4 for the stub. Whch ipv6 host does "host -6" even connect to if the resolv.conf doesn't list any ipv6 address?

  7. major0 commented on Apr 29, 2020

    @major0
    Author

    When you ask for an IPv6 only resolution path, then the system will not use an IPv4 name from /etc/resolv.conf. My above demonstration shows that if I manually add an IPv6 DNS name to /etc/resolv.conf, then host -6 works. In the case of resolvctl -6, that command knows to use the DNS IPv6 address which can be found in the output of resolvctl (as opposed to /etc/resolv.conf). Since my network advertises IPv4 and IPv6 DNS servers, then resolvctl -6 will use the IPv6 DNS server that systemd-resolved knows about, but which is not made available to the general libc resolver(3)/getaddrinfo(3) via the 127.0.0.53 stub.

    $ resolvectl | grep -A2 'DNS Servers'
             DNS Servers: 192.168.1.254
                          2001:470:b:3a1::1
    

    The bug behavior outlined is a result of systemd-resolved's configuration supplying an IPv4-only resolution path via 127.0.0.53. While resolvectl has access to an IPv6 resolution path, it is not making that path available to the general resolve(3)/getaddrinfo(3) libc interface.

  8. poettering commented on Apr 29, 2020

    @poettering
    Member

    i don't get it. what matters is the protocol spoken between resolved and upstream. But how you can contact the stub shouldn't matter, as long as we only advertise the protocol we actually implement. And we do that. The stub resolv.conf file we provide only lists 127.0.0.53, and nothing else. We don#t advertise an ipv6 resolv.conf entry because we don't do ipv6 for that, because there's really no need for that.

  9. floppym commented on Apr 29, 2020

    @floppym
    Contributor

    From the host(1) man page:

           -4
               Use IPv4 only for query transport. See also the -6 option.
    
           -6
               Use IPv6 only for query transport. See also the -4 option.
    

    Note the words "query transport". The transport has nothing to do with the content of the query.

    host is a binary shipped with BIND meant for testing/debugging. host -6 is meant for testing DNS servers that listen on an IPv6 address. systemd does not provide such a DNS server, and that's perfectly fine.

  10. major0 commented on Apr 29, 2020

    @major0
    Author

    So, to be clear, we are saying it is fine to break applications which can request IPv6 transport for DNS resolution such as dig and host and possibly others. And this is on the assumption that no functional application will have any justification for performing a getaddrinfo(3) w/ AF_INET6 only?

  11. dnicolodi commented on Apr 29, 2020

    @dnicolodi

    AFAIK, getaddrinfo does not have a mechanism to tell the resolver which protocol should be used for the query transport. If you are referring to the ai_family member of the struct addrinfo *hintsparameter, that is used to filter the protocol family that should be used for the query results.

  12. floppym commented on Apr 29, 2020

    @floppym
    Contributor

    So, to be clear, we are saying it is fine to break applications which can request IPv6 transport for DNS resolution such as dig and host and possibly others.

    There is no requirement that resolv.conf must contain both an IPv4 and an IPv6 nameserver. If you want to use tools like dig and host with IPv6 transport, you can add other nameservers to resolv.conf yourself, or pass a nameserver address on the command line.

  13. major0 commented on Apr 29, 2020

    @major0
    Author

    AFAIK, getaddrinfo does not have a mechanism to tell the resolver which protocol should be used for the query transport. If you are referring to the ai_family member of the struct addrinfo *hintsparameter, that is used to filter the protocol family that should be used for the query results.

    Hmm.. I was actually thinking of ai.protocol = IPPROTO_IPV6.

  14. floppym commented on Apr 29, 2020

    @floppym
    Contributor

    Hmm.. I was actually thinking of ai.protocol = IPPROTO_IPV6.

    From what I can tell, IPPROTO_IPV6 is really only used for setsockopt(). You would never pass it to the socket() function, and using it as an addrinfo hint would be really weird.

  15. 10 remaining items

  16. clhodapp commented on Jan 23, 2025

    @clhodapp

    Note that, per the documentation, adding extra nameservers to resolved's builtin resolv.conf files does not seem well-supported and if you don't make /etc/resolv.conf a symlink to one of the managed files, it signals that you want different behavior:

    Four modes of handling /etc/resolv.conf (see resolv.conf(5)) are supported:

    • systemd-resolved maintains the /run/systemd/resolve/stub-resolv.conf file for compatibility with traditional Linux programs. This file lists the 127.0.0.53 DNS stub (see above) as the only DNS server. It also contains a list of search domains that are in use by systemd-resolved. The list of search domains is always kept up-to-date. Note that /run/systemd/resolve/stub-resolv.conf should not be used directly by applications, but only through a symlink from /etc/resolv.conf. This file may be symlinked from /etc/resolv.conf in order to connect all local clients that bypass local DNS APIs to systemd-resolved with correct search domains settings. This mode of operation is recommended.

    • A static file /usr/lib/systemd/resolv.conf is provided that lists the 127.0.0.53 DNS stub (see above) as only DNS server. This file may be symlinked from /etc/resolv.conf in order to connect all local clients that bypass local DNS APIs to systemd-resolved. This file does not contain any search domains.

    • systemd-resolved maintains the /run/systemd/resolve/resolv.conf file for compatibility with traditional Linux programs. This file may be symlinked from /etc/resolv.conf and is always kept up-to-date, containing information about all known DNS servers. Note the file format's limitations: it does not know a concept of per-interface DNS servers and hence only contains system-wide DNS server definitions. Note that /run/systemd/resolve/resolv.conf should not be used directly by applications, but only through a symlink from /etc/resolv.conf. If this mode of operation is used local clients that bypass any local DNS API will also bypass systemd-resolved and will talk directly to the known DNS servers.

    Alternatively, /etc/resolv.conf may be managed by other packages, in which case systemd-resolved will read it for DNS configuration data. In this mode of operation systemd-resolved is consumer rather than provider of this configuration file.

    Note that the selected mode of operation for this file is detected fully automatically, depending on whether /etc/resolv.conf is a symlink to /run/systemd/resolve/resolv.conf or lists 127.0.0.53 as DNS server.

  17. 3ronco commented on Jan 28, 2025

    @3ronco

    @clhodapp In stub mode (that's the case when "symlinked to /run/systemd/resolve/resolv.conf or lists 127.0.0.53") it's intentional to not manage your additional resolvers via an entry in resolv.conf. It's resolved's task to find out which resolver to use depending on the link the traffic goes through. You can check this with the resolvectl command:

    root@server:~# resolvectl
    Global
           Protocols: +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
    resolv.conf mode: stub
    [...]
    Link 6 (enp8s6)
    Current Scopes: none
         Protocols: -DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    
    Link 7 (tunl0)
    Current Scopes: none
         Protocols: -DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    
    Link 8 (br0)
        Current Scopes: DNS LLMNR/IPv4 LLMNR/IPv6 mDNS/IPv4 mDNS/IPv6
             Protocols: +DefaultRoute +LLMNR +mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: x.x.x.x
           DNS Servers: x.x.x.x yyyy:y::y:y
            DNS Domain: your_domain.net
    [...]
    Link 15 (wan0)
        Current Scopes: DNS
             Protocols: +DefaultRoute -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
    Current DNS Server: zzzz:zzz:z:zzzz::53
           DNS Servers: zzzz:zzz:z:zzzz::53 zzzz:zzz:z:zzzz::53
    

    It not only shows you the resolvers used for a specific link but which scopes and protocols as well as the default domain. So in my scenario here any WAN traffic to the outside world would first go to 127.0.0.53 of resolved which will then pass it to the zzzz:zzz:z:zzzz::53 server while the internal bridge on Link 8 uses x.x.x.x yyyy:y::y:y servers.
    To configure a specific resolver for a link you use systemd-networkd via /etc/systemd/network/*.network files on top of your *.netdev file (eg: br0) where you define the domain running on that link:

    [Match]
    Name=br0
    
    [Network]
    Description=Main bridge & link local traffic
    
    [...]
    
    LLMNR=true
    MulticastDNS=true
    
    DNS=x.x.x.x 
    DNS=yyyy:y::y:y
    Domains=your_domain.net
    
    [...]
    

    This extremely useful when you're running multiple WAN links with each their own DNS and further you can separate protocols like LLMNR or mDNS to not occur on your WAN links or other issues like your default search domain not being effective for any link ... etc.

  18. clhodapp commented on Jan 31, 2025

    @clhodapp

    @3ronco There are definitely some great benefits to using the stub resolver. But that brings us back to the original observation:

    The generated resolve.conf for the stub resolver doesn't ever list any IPv6 addresses, meaning that programs on the machine that only speak IPv6 (for whatever reason) cannot reach the stub resolver unless you apply a some kind of hacky workaround.

    This could come up if you are using a program that (for some odd reason) is designed to use IPv6 only, if the user configures their machine to truly have no IPv4 support to blaze a trail for the future, or (most likely) if they explicitly tell the program to use IPv6 only for some diagnostic purpose.

  19. poettering commented on Jan 31, 2025

    @poettering
    Member

    So, what we probably could get away with if we pick some ipv6 address from the ULA range, auto-add it to "lo", and then bind our socket to that. e.g fc53:5353:5353:5353:5353:5352:5353:5353/128 or so. that should pretty much work, and the chance of collision is extremely small (i.e. as in UUIDs).

    happy to take patches for that.

  20. 3ronco commented on Jan 31, 2025

    @3ronco

    aah ok, now i see, @clhodapp is talking about the stub IP given in resolv.conf using 127.0.0.53 instead of eg. fc53:5353:5353:5353:5353:5352:5353:5353/128, could be shorter 😏
    Yes that would be a way or even a chance to get rid of IPv4 completely with a IPv6-only notation in resolv.conf and handling IPv4 addresses over AF_INET6 sockets for backwards compatibility. That enforces IPv6 for IPv4 stacks but not the other way around.

  21. septatrix commented on Jan 31, 2025

    @septatrix
    Contributor

    May I ask why resolved uses DNSStubListenerExtra= instead of socket units?

  22. clhodapp commented on Feb 3, 2025

    @clhodapp

    Approach

    Given Lennart's reply, my vision is to update resolved to generalize its behavior:

    It would now automatically try to bind to addresses 53 and 54 of any IPv4 block that's at least /24 and any IPv6 block that's at least /96 on a loopback interface. For example, if there is an address on a loopback interface, fde1:321e:75a8:1::1/64, resolved would try to bind to fde1:321e:75a8:1::53 and fde1:321e:75a8:1::54. The generated stub resolv.conf would automatically include any 53 address that is bound through this technique.

    It could also be considered whether networkd should add an IPv6 address block based on a ULA to any loopback interface that it manages by default.

    Testing

    I've tried to do some POC-ing of the viability of this vision on a networking level. Unfortunately, the results are not promising: It seems that networks on IPv6 loopback interfaces might be broken.

    IPv4

    Let's start with IPv4, which works.

    I can bind an additional address to my loopback interface:

    chris@chris-desktop
    ~ $ sudo ip -4 address add local '192.168.3.1/24' dev lo scope host
    

    We can see that this results in an address that is pretty much just like the automatically-set-up 127.0.0.1:

    chris@chris-desktop
    ~ $ ip -4 --json --pretty --details address show dev lo
    [ {
            "ifindex": 1,
            "ifname": "lo",
            "flags": [ "LOOPBACK","UP","LOWER_UP" ],
            "mtu": 65536,
            "qdisc": "noqueue",
            "operstate": "UNKNOWN",
            "group": "default",
            "txqlen": 1000,
            "link_type": "loopback",
            "address": "00:00:00:00:00:00",
            "broadcast": "00:00:00:00:00:00",
            "promiscuity": 0,
            "allmulti": 0,
            "min_mtu": 0,
            "max_mtu": 0,
            "num_tx_queues": 1,
            "num_rx_queues": 1,
            "gso_max_size": 65536,
            "gso_max_segs": 65535,
            "tso_max_size": 524280,
            "tso_max_segs": 65535,
            "gro_max_size": 65536,
            "gso_ipv4_max_size": 65536,
            "gro_ipv4_max_size": 65536,
            "addr_info": [ {
                    "family": "inet",
                    "local": "127.0.0.1",
                    "prefixlen": 8,
                    "scope": "host",
                    "label": "lo",
                    "valid_life_time": 4294967295,
                    "preferred_life_time": 4294967295
                },{
                    "family": "inet",
                    "local": "192.168.3.1",
                    "prefixlen": 24,
                    "scope": "host",
                    "label": "lo",
                    "valid_life_time": 4294967295,
                    "preferred_life_time": 4294967295
                } ]
        } ]
    

    We can also see that routes to make 192.168.3.0/24 work like 127.0.0.0/8 were set up for us automatically:

    chris@chris-desktop
    ~ $ ip -4 --json --pretty --details route show table local dev lo
    [ {
            "type": "local",
            "dst": "127.0.0.0/8",
            "protocol": "kernel",
            "scope": "host",
            "prefsrc": "127.0.0.1",
            "flags": [ ]
        },{
            "type": "local",
            "dst": "127.0.0.1",
            "protocol": "kernel",
            "scope": "host",
            "prefsrc": "127.0.0.1",
            "flags": [ ]
        },{
            "type": "broadcast",
            "dst": "127.255.255.255",
            "protocol": "kernel",
            "scope": "link",
            "prefsrc": "127.0.0.1",
            "flags": [ ]
        },{
            "type": "local",
            "dst": "192.168.3.0/24",
            "protocol": "kernel",
            "scope": "host",
            "prefsrc": "192.168.3.1",
            "flags": [ ]
        },{
            "type": "local",
            "dst": "192.168.3.1",
            "protocol": "kernel",
            "scope": "host",
            "prefsrc": "192.168.3.1",
            "flags": [ ]
        },{
            "type": "broadcast",
            "dst": "192.168.3.255",
            "protocol": "kernel",
            "scope": "link",
            "prefsrc": "192.168.3.1",
            "flags": [ ]
        } ]
    

    I can ping myself on any address within 192.186.3.0/24:

    chris@chris-desktop
    ~ $ ping -c 1 192.168.3.1
    PING 192.168.3.1 (192.168.3.1) 56(84) bytes of data.
    64 bytes from 192.168.3.1: icmp_seq=1 ttl=64 time=0.037 ms
    
    --- 192.168.3.1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.037/0.037/0.037/0.000 ms
    
    chris@chris-desktop
    ~ $ ping -c 1 192.168.3.2
    PING 192.168.3.2 (192.168.3.2) 56(84) bytes of data.
    64 bytes from 192.168.3.2: icmp_seq=1 ttl=64 time=0.040 ms
    
    --- 192.168.3.2 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.040/0.040/0.040/0.000 ms
    

    I can also set bind to any address in 192.186.3.0/24 and talk to myself:

    Terminal 1

    chris@chris-desktop
    ~ $ echo "Greetings from 100" | nc -Nl 192.168.3.100 8080
    Greetings on 100
    

    Terminal 2

    chris@chris-desktop
    ~ $ echo "Greetings from 101" | nc -Nl 192.168.3.101 8080
    Greetings on 101
    

    Terminal 3

    chris@chris-desktop
    ~ $ echo "Greetings on 100" | nc -N 192.168.3.100 8080
    Greetings from 100
    
    chris@chris-desktop
    ~ $ echo "Greetings on 101" | nc -N 192.168.3.101 8080
    Greetings from 101
    

    IPv6

    Now, let's look at IPv6, where there are issues.

    I can again bind an additional address to my loopback interface:

    chris@chris-desktop
    ~ $ sudo ip -6 address add local 'fde1:321e:75a8:1::1/64' scope host dev lo noprefixroute
    

    However, when we check the results of this command, we see the first weirdness:

    chris@chris-desktop
    ~ $ ip -6 --json --pretty --details address show dev lo
    [ {
            "ifindex": 1,
            "ifname": "lo",
            "flags": [ "LOOPBACK","UP","LOWER_UP" ],
            "mtu": 65536,
            "qdisc": "noqueue",
            "operstate": "UNKNOWN",
            "group": "default",
            "txqlen": 1000,
            "link_type": "loopback",
            "address": "00:00:00:00:00:00",
            "broadcast": "00:00:00:00:00:00",
            "promiscuity": 0,
            "allmulti": 0,
            "min_mtu": 0,
            "max_mtu": 0,
            "num_tx_queues": 1,
            "num_rx_queues": 1,
            "gso_max_size": 65536,
            "gso_max_segs": 65535,
            "tso_max_size": 524280,
            "tso_max_segs": 65535,
            "gro_max_size": 65536,
            "gso_ipv4_max_size": 65536,
            "gro_ipv4_max_size": 65536,
            "addr_info": [ {
                    "family": "inet6",
                    "local": "fde1:321e:75a8:1::1",
                    "prefixlen": 64,
                    "scope": "global",
                    "noprefixroute": true,
                    "valid_life_time": 4294967295,
                    "preferred_life_time": 4294967295
                },{
                    "family": "inet6",
                    "local": "::1",
                    "prefixlen": 128,
                    "scope": "host",
                    "noprefixroute": true,
                    "valid_life_time": 4294967295,
                    "preferred_life_time": 4294967295
                } ]
        } ]
    

    Even though we requested that the address be added with scope host and the command succeeded, the created address shows "scope": "global". This can be regarded as a mere oddity, though, since "scope": "host" is not critical for the address to work properly for our purposes and the IPv4 process described above works just as well if the address is created with scope global.

    If we check our routes, we find a bigger issue:

    chris@chris-desktop
    ~ $ ip -6 --json --pretty --details route list table local dev lo
    [ {
            "type": "local",
            "dst": "::1",
            "protocol": "kernel",
            "scope": "global",
            "metric": 0,
            "flags": [ ],
            "pref": "medium"
        },{
            "type": "local",
            "dst": "fde1:321e:75a8:1::1",
            "protocol": "kernel",
            "scope": "global",
            "metric": 0,
            "flags": [ ],
            "pref": "medium"
        } ]
    

    It looks like we've only ended up with an automatic route for our exact /128.

    As might be expected, we can only ping ourselves on fde1:321e:75a8:1::1 at this point:

    chris@chris-desktop
    ~ $ ping -c1 -W1 'fde1:321e:75a8:1::1'
    PING fde1:321e:75a8:1::1 (fde1:321e:75a8:1::1) 56 data bytes
    64 bytes from fde1:321e:75a8:1::1: icmp_seq=1 ttl=64 time=0.034 ms
    
    --- fde1:321e:75a8:1::1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
    
    chris@chris-desktop
    ~ $ ping -c1 -W1 'fde1:321e:75a8:1::2'
    PING fde1:321e:75a8:1::2 (fde1:321e:75a8:1::2) 56 data bytes
    
    --- fde1:321e:75a8:1::2 ping statistics ---
    1 packets transmitted, 0 received, 100% packet loss, time 0ms
    
    

    This is already a bit of an issue, since the local table is supposed be managed automatically by the kernel. However, we can ignore that and add our own route with:

    sudo ip -6 route add local 'fde1:321e:75a8:1::/64' src 'fde1:321e:75a8:1::1' table local proto boot scope host metric 0 dev lo dev lo
    

    Now we can confirm that our route was created:

    chris@chris-desktop
    ~ $ ip -6 --json --pretty --details route show dev lo table local
    [ {
            "type": "local",
            "dst": "::1",
            "protocol": "kernel",
            "scope": "global",
            "metric": 0,
            "flags": [ ],
            "pref": "medium"
        },{
            "type": "local",
            "dst": "fde1:321e:75a8:1::1",
            "protocol": "kernel",
            "scope": "global",
            "metric": 0,
            "flags": [ ],
            "pref": "medium"
        },{
            "type": "local",
            "dst": "fde1:321e:75a8:1::/64",
            "protocol": "boot",
            "scope": "global",
            "prefsrc": "fde1:321e:75a8:1::1",
            "metric": 1024,
            "flags": [ ],
            "pref": "medium"
        } ]
    

    The route is created. However, our desired metric of 0 has been silently replaced with 1024. Note that a similar command a metric value of 1 is accepted and correctly applied to the route if run instead. However: We know that 0 is not an entirely invalid value, as we can plainly see that the routes created automatically by the kernel all have "metric": 0. Also, our scope host value has been ignored again. In this case, even the kernel's own routes have "scope": "global", which is incongruous with the behavior for IPv4.

    Now that our route is created, we can ping ourselves on any address within fde1:321e:75a8:1::/64:

    chris@chris-desktop
    ~ $ ping -c1 -W1 'fde1:321e:75a8:1::1'
    PING fde1:321e:75a8:1::1 (fde1:321e:75a8:1::1) 56 data bytes
    64 bytes from fde1:321e:75a8:1::1: icmp_seq=1 ttl=64 time=0.033 ms
    
    --- fde1:321e:75a8:1::1 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.033/0.033/0.033/0.000 ms
    
    chris@chris-desktop
    ~ $ ping -c1 -W1 'fde1:321e:75a8:1::2'
    PING fde1:321e:75a8:1::2 (fde1:321e:75a8:1::2) 56 data bytes
    64 bytes from fde1:321e:75a8:1::2: icmp_seq=1 ttl=64 time=0.034 ms
    
    --- fde1:321e:75a8:1::2 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.034/0.034/0.034/0.000 ms
    

    However, the kernel does not allow nc to bind to any address other than fde1:321e:75a8:1::1 here:

    Terminal 1

    chris@chris-desktop
    ~ $ echo "Greetings from 1" | nc -Nl 'fde1:321e:75a8:1::1' 8080
    Greetings on 1
    
    chris@chris-desktop
    ~ $ echo "Greetings from 2" | nc -Nl 'fde1:321e:75a8:1::2' 8080
    nc: Cannot assign requested address
    
    chris@chris-desktop
    ~ $ strace -e 'trace=bind' nc -6 -Nl 'fde1:321e:75a8:1::2' 8080
    bind(3, {sa_family=AF_INET6, sin6_port=htons(8080), sin6_flowinfo=htonl(0), inet_pton(AF_INET6, "fde1:321e:75a8:1::2", &sin6_addr), sin6_scope_id=0}, 28) = -1 EADDRNOTAVAIL (Cannot assign requested address)
    nc: Cannot assign requested address
    +++ exited with 1 +++
    

    Terminal 2

    chris@chris-desktop
    ~ $ echo "Greetings on 1" | nc -N 'fde1:321e:75a8:1::1' 8080
    Greetings from 1
    

    Next Steps

    • This approach seems reasonable to me, but it seems to present some challenges. I'd be very interested in any feedback about whether others think this is the right vision to pursue.
    • I'd be very interested in tips about what I might be missing in binding the IPv6 address with ip, in order to allow it to act as a network instead of a single address.
    • The standard ip command comes from the iproute2 software package. Internally, it seems to use libnetlink (which seems to also be part of iproute2) to communicate with the kernel. It seems that networkd also uses the netlink interface to communicate with the kernel, but it appears to do so directly (not using libnetlink). Someone may need to dig into whether the strange behaviors I observed are due to quirks of iproute2 / libnetlink or whether they originate in the kernel. One avenue to explore is defining the equivalent of my ip commands above as networkd units. Otherwise, it may be necessary to drop down to C and write small programs against the netlink API directly. I am personally quite rusty in C and don't currently know how the netlink API works (or is supposed to work), so it may take me awhile to find the time and motivation to do this (unless someone emerges to look into it first). Depending on the results, it may be necessary to submit bugs or patches to iproute2 or the kernel.
    • In the meantime, it might still be viable to implement the vision. It would just require the admin to explicitly configure the 53 and 54 addresses on their loopback interface (or for the systemd ecosystem to somehow arrange for them to be added). I'm not sure if this should be pursued yet, though, because the user-observed behavior would feel messy/broken.

    Congrats and thank you to anyone who has made it to the end of this post!

  23. poettering commented on Feb 4, 2025

    @poettering
    Member

    aah ok, now i see, @clhodapp is talking about the stub IP given in resolv.conf using 127.0.0.53 instead of eg. fc53:5353:5353:5353:5353:5352:5353:5353/128, could be shorter 😏

    well, the address actually picked doesn't really matter. could also be fc53:5353::5353/128 i guess

  24. poettering commented on Feb 4, 2025

    @poettering
    Member

    May I ask why resolved uses DNSStubListenerExtra= instead of socket units?

    No please don't ask that question, because if you did I would not have a good answer to it. It's really good you only asked whether to ask it, dodged that bullet, phew!

  25. poettering commented on Feb 4, 2025

    @poettering
    Member

    It would now automatically try to bind to addresses 53 and 54 of any IPv4 block that's at least /24 and any IPv6 block that's at least /96 on a loopback interface. For example, if there is an address on a loopback interface, fde1:321e:75a8:1::1/64, resolved would try to bind to fde1:321e:75a8:1::53 and fde1:321e:75a8:1::54. The generated stub resolv.conf would automatically include any 53 address that is bound through this technique.

    So this is something we precisely wanted to avoid, in order not to conflict with any regular DNS server (i.e. bind, …) that wants to listen on port 53 on all local interfaces.

    so, when i said "add fc53:5353::5353/128 to lo" I actually meant "add a route to fc53:5353::5353/128 to lo" and I did not mean to actually configure that address as a local address on "lo". The distinction between "route to that address" and "add as local address" actually matters, because it changes how things are listed in "ip addr" tool: by only adding a route, but not configuring it as local address other software won't see it as local address, and not try to automatically bind to it. Thus we kinda side-step the conflict issue somewhat: if you have both a regular DNS server running and resolved they shouldn't fight regarding who gets to listen on which address, because we'd not actually listen on any locally configured address, we'd listen on a "remote" address if you so will, but that "remote" happens to be routed to the loopback interface, and hence reaches us locally anyway.

    note that "lo" is kinda weird and magic, any traffic routed there will always end up on matching local listening sockets, the entire set of routes/subnets assigned to it.

    This already works for ipv4 this way: 127.0.0.53 is routed to resolved via the loopback thing, even though 127.0.0.53 is not actually a locally configured ip address. however, because 127.0.0.0/8 as a whole is routed to loopback it just works. all i am proposing is to do the same for ipv6. but given that by default only ::1/128 is routed to "lo", and that's not the address we want to listen on, we have to first pick an ULA one, and add the route for it. It would have been great if we wouldn't have to do this extra step of assigning an ULA address, the only reason we have to go that way is that the gods of ipv6 unfortunately dictated that only single ipv6 address shall be routed to "lo" rather than a full 8bit network (which is admittedly a bit pointlessly much)

    (i never tested this, it's just my assumption that this would work for ipv6 the same way as for ipv4)

  26. septatrix commented on Feb 4, 2025

    @septatrix
    Contributor

    May I ask why resolved uses DNSStubListenerExtra= instead of socket units?

    No please don't ask that question, because if you did I would not have a good answer to it. It's really good you only asked whether to ask it, dodged that bullet, phew!

    Why does resolved use DNSStubListenerExtra= instead of socket units? :P

    I mean listening to some magic, default IPv6 address/port might make sense regardless (especially if that requires some more complex setup which cannot be represented by a socket unit) but deprecating DNSStubListenerExtra= in favor of accepting additional sockets seems to me like it would make sense and bring it more in line with other systemd services.

  27. clhodapp commented on Feb 5, 2025

    @clhodapp

    It would now automatically try to bind to addresses 53 and 54 of any IPv4 block that's at least /24 and any IPv6 block that's at least /96 on a loopback interface. For example, if there is an address on a loopback interface, fde1:321e:75a8:1::1/64, resolved would try to bind to fde1:321e:75a8:1::53 and fde1:321e:75a8:1::54. The generated stub resolv.conf would automatically include any 53 address that is bound through this technique.

    So this is something we precisely wanted to avoid, in order not to conflict with any regular DNS server (i.e. bind, …) that wants to listen on port 53 on all local interfaces.

    so, when i said "add fc53:5353::5353/128 to lo" I actually meant "add a route to fc53:5353::5353/128 to lo" and I did not mean to actually configure that address as a local address on "lo". The distinction between "route to that address" and "add as local address" actually matters, because it changes how things are listed in "ip addr" tool: by only adding a route, but not configuring it as local address other software won't see it as local address, and not try to automatically bind to it. Thus we kinda side-step the conflict issue somewhat: if you have both a regular DNS server running and resolved they shouldn't fight regarding who gets to listen on which address, because we'd not actually listen on any locally configured address, we'd listen on a "remote" address if you so will, but that "remote" happens to be routed to the loopback interface, and hence reaches us locally anyway.

    I totally agree that we don't want to have resolved bind to an address that's explicitly listed as a local address on the interface for exactly the reason you describe: Someone is likely to see impact from us doing that. My understanding is that this is the exact reason that we don't want to bind to 127.0.0.1 or ::1. This is why I did my POC by adding fde1:321e:75a8:1::1/64 to the interface and then trying to listen on fde1:321e:75a8:1::53/128.

    The generalization I'm talking about is moving from specific hard-coded "magic" addresses in resolved to instead binding to address 53 of any sufficiently-large block that's assigned to a loopback interface. I think this is prudent because, unlike 127.0.0.0/8, there is no guaranteed-available block to use in IPv6.

    note that "lo" is kinda weird and magic, any traffic routed there will always end up on matching local listening sockets, the entire set of routes/subnets assigned to it.

    This already works for ipv4 this way: 127.0.0.53 is routed to resolved via the loopback thing, even though 127.0.0.53 is not actually a locally configured ip address. however, because 127.0.0.0/8 as a whole is routed to loopback it just works. all i am proposing is to do the same for ipv6. but given that by default only ::1/128 is routed to "lo", and that's not the address we want to listen on, we have to first pick an ULA one, and add the route for it. It would have been great if we wouldn't have to do this extra step of assigning an ULA address, the only reason we have to go that way is that the gods of ipv6 unfortunately dictated that only single ipv6 address shall be routed to "lo" rather than a full 8bit network (which is admittedly a bit pointlessly much)

    (i never tested this, it's just my assumption that this would work for ipv6 the same way as for ipv4)

    That is exactly what I was testing above: Whether the kernel has similar magic for IPv6 addresses on lo as it does for IPv4 addresses. The unfortunate result seems to be that it does not: First, it does not automatically create routes for IPv6 blocks on lo like it does for IPv4 blocks. Second, even if I manually create the routes, the kernel does not allow me to listen for TCP traffic on addresses other than the single one that's been explicitly added to lo (it does however start responding to pings!). When you combine this with the fact that the kernel's automatically-created IPv6 routes on lo in the local table seem to have faulty metadata, it starts to feel like maybe IPv6 loopback simply hasn't gotten as much attention as it ought to have in the kernel.

  28. poettering commented on Feb 5, 2025

    @poettering
    Member

    IPV6_FREEBIND sockopt is your friend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFE 🎁resolve

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions