Commit graph

58 commits

Author SHA1 Message Date
Rob Murray
2ddec74d59 Remove unused params from etchosts.Build()
Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-01-29 15:37:08 +00:00
Rob Murray
a8f7c5ee48 Detect IPv6 support in containers.
Some configuration in a container depends on whether it has support for
IPv6 (including default entries for '::1' etc in '/etc/hosts').

Before this change, the container's support for IPv6 was determined by
whether it was connected to any IPv6-enabled networks. But, that can
change over time, it isn't a property of the container itself.

So, instead, detect IPv6 support by looking for '::1' on the container's
loopback interface. It will not be present if the kernel does not have
IPv6 support, or the user has disabled it in new namespaces by other
means.

Once IPv6 support has been determined for the container, its '/etc/hosts'
is re-generated accordingly.

The daemon no longer disables IPv6 on all interfaces during initialisation.
It now disables IPv6 only for interfaces that have not been assigned an
IPv6 address. (But, even if IPv6 is disabled for the container using the
sysctl 'net.ipv6.conf.all.disable_ipv6=1', interfaces connected to IPv6
networks still get IPv6 addresses that appear in the internal DNS. There's
more to-do!)

Signed-off-by: Rob Murray <rob.murray@docker.com>
2024-01-19 20:24:07 +00:00
Sebastiaan van Stijn
cff4f20c44
migrate to github.com/containerd/log v0.1.0
The github.com/containerd/containerd/log package was moved to a separate
module, which will also be used by upcoming (patch) releases of containerd.

This patch moves our own uses of the package to use the new module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-10-11 17:52:23 +02:00
Albin Kerouanton
cbc2a71c27
libnet/d/bridge: Don't set container's gateway when network is internal
So far, internal networks were only isolated from the host by iptables
DROP rules. As a consequence, outbound connections from containers would
timeout instead of being "rejected" through an immediate ICMP dest/port
unreachable, a TCP RST or a failing `connect` syscall.

This was visible when internal containers were trying to resolve a
domain that don't match any container on the same network (be it a truly
"external" domain, or a container that don't exist/is dead). In that
case, the embedded resolver would try to forward DNS queries for the
different values of resolv.conf `search` option, making DNS resolution
slow to return an error, and the slowness being exacerbated by some libc
implementations.

This change makes `connect` syscall to return ENETUNREACH, and thus
solves the broader issue of failing fast when external connections are
attempted.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2023-10-09 13:57:54 +02:00
Sebastiaan van Stijn
6dba98cf38
libnetwork: implement Controller.GetSandbox(containerID)
Various parts of the code were using "walkers" to iterate over the
controller's sandboxes, and the only condition for all of them was
to find the sandbox for a given container-ID. Iterating over all
sandboxes was also sub-optimal, because on Windows, the ContainerID
is used as Sandbox-ID, which can be used to lookup the sandbox from
the "sandboxes" map on the controller.

This patch implements a GetSandbox method on the controller that
looks up the sandbox for a given container-ID, using the most optimal
approach (depending on the platform).

The new method can return errors for invalid (empty) container-IDs, and
a "not found" error to allow consumers to detect non-existing sandboxes,
or potentially invalid IDs.

This new method replaces the (non-exported) Daemon.getNetworkSandbox(),
which was only used internally, in favor of directly accessing the
controller's method.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-21 15:06:26 +02:00
Sebastiaan van Stijn
d4e1c072e2
libnetwork: move resolverIPSandbox closer to where it's used
It's only used on non-Windows platforms, so let's move it there.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-08-11 15:00:30 +02:00
Brian Goff
74da6a6363 Switch all logging to use containerd log pkg
This unifies our logging and allows us to propagate logging and trace
contexts together.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2023-06-24 00:23:44 +00:00
Sebastiaan van Stijn
ab35df454d
remove pre-go1.17 build-tags
Removed pre-go1.17 build-tags with go fix;

    go mod init
    go fix -mod=readonly ./...
    rm go.mod

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-19 20:38:51 +02:00
Sebastiaan van Stijn
55d18b7db9
libnetwork/resolvconf: use []byte for hash instead of string
After my last change, I noticed that the hash is used as a []byte in most
cases (other than tests). This patch updates the type to use a []byte, which
(although unlikely very important) also improves performance:

Compared to the previous version:

    benchstat new.txt new2.txt
    name         old time/op    new time/op    delta
    HashData-10     128ns ± 1%     116ns ± 1%   -9.77%  (p=0.000 n=20+20)

    name         old alloc/op   new alloc/op   delta
    HashData-10      208B ± 0%       88B ± 0%  -57.69%  (p=0.000 n=20+20)

    name         old allocs/op  new allocs/op  delta
    HashData-10      3.00 ± 0%      2.00 ± 0%  -33.33%  (p=0.000 n=20+20)

And compared to the original version:

    benchstat old.txt new2.txt
    name         old time/op    new time/op    delta
    HashData-10     201ns ± 1%     116ns ± 1%  -42.39%  (p=0.000 n=18+20)

    name         old alloc/op   new alloc/op   delta
    HashData-10      416B ± 0%       88B ± 0%  -78.85%  (p=0.000 n=20+20)

    name         old allocs/op  new allocs/op  delta
    HashData-10      6.00 ± 0%      2.00 ± 0%  -66.67%  (p=0.000 n=20+20)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-04-26 22:49:47 +02:00
Cory Snider
046cc9e776 libnetwork: check DNS loopback with user DNS opts
DNS servers in the loopback address range should always be resolved in
the host network namespace when the servers are configured by reading
from the host's /etc/resolv.conf. The daemon mistakenly conflated the
presence of DNS options (docker run --dns-opt) with user-supplied DNS
servers, treating the list of servers loaded from the host as a user-
supplied list and attempting to resolve in the container's network
namespace. Correct this oversight so that loopback DNS servers are only
resolved in the container's network namespace when the user provides the
DNS server list, irrespective of other DNS configuration.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-02-10 16:20:06 -05:00
Cory Snider
0e91d2e0e9 libnetwork: return concrete-typed *Sandbox
Basically every exported method which takes a libnetwork.Sandbox
argument asserts that the value's concrete type is *sandbox. Passing any
other implementation of the interface is a runtime error! This interface
is a footgun, and clearly not necessary. Export and use the concrete
type instead.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-13 14:19:06 -05:00
Cory Snider
d6cc02d301 libnetwork: drop (resolver).resolverKey field
...as it is now unused.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2023-01-11 12:14:32 -05:00
Tianon Gravi
bcb8f69cc5
Merge pull request #44239 from thaJeztah/resolvconf_refactor_step2
libnetwork: simplify handling of reading resolv.conf
2022-12-22 13:18:47 -08:00
Sebastiaan van Stijn
46f7c92c9a
libnetwork: use strings.Cut() and minor refactor
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-12-21 11:09:03 +01:00
Sebastiaan van Stijn
0ef0dab2f7
libnetwork: sandbox.rebuildDNS() make updating sandbox more atomic
Don't update external resolvers until after we successfully parsed all options.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:42 +01:00
Sebastiaan van Stijn
806b4fbcad
libnetwork: sandbox.rebuildDNS() move variables closer to where they're used
Some of these options required parsing the resolv.conf file, but the function
could return an error further down; this patch moves the parsing closer to
where their results are used (which may not happen if we're encountering an
error before).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:42 +01:00
Sebastiaan van Stijn
d178a71b7c
libnetwork: sandbox.rebuildDNS() simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the code by
just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:41 +01:00
Sebastiaan van Stijn
05b3356631
libnetwork: sandbox.setupDNS() don't parse host resolv.conf if not needed
The existing code was always parsing the host's resolv.conf to read
the nameservers, searchdomain and options, but those options were
only needed if these options were not configured on the sandbox.

This patch reverses the logic to only parse the resolv.conf if
no options are present in the sandbox configuration.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:41 +01:00
Sebastiaan van Stijn
08d5e3d0fa
libnetwork: sandbox.setupDNS() simplify reading of resolv.conf
We only need the content here, not the checksum, so simplifying the
code by just using os.ReadFile().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-29 20:10:40 +01:00
Sebastiaan van Stijn
60ace31be0
libnetwork: sandbox.updateDNS(): use const for file permissions
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-10-02 01:28:05 +02:00
Sebastiaan van Stijn
cd381aea56
libnetwork: fix empty-lines (revive)
libnetwork/etchosts/etchosts_test.go:167:54: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/osl/route_linux.go:185:74: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/osl/sandbox_linux_test.go:323:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/bitseq/sequence.go:412:48: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/datastore/datastore_test.go:67:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/datastore/mock_store.go:34:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld.go:202:44: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/firewalld_test.go:76:36: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:256:67: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/iptables/iptables.go:303:128: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/networkdb/cluster.go:183:72: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipams/null/null_test.go:44:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/macvlan/macvlan_store.go:45:52: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1058:39: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/port_mapping.go:88:111: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/link.go:26:90: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ipv6_test.go:17:34: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/bridge/setup_ip_tables.go:392:4: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/bridge/bridge.go:804:50: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_serf.go:183:29: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/ov_utils.go:81:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:172:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:209:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:344:89: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/peerdb.go:436:63: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/overlay.go:183:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/drivers/overlay/encryption.go:69:28: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/drivers/overlay/ov_network.go:563:81: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/default_gateway.go:32:43: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:184:64: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/endpoint.go:161:55: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store.go:320:33: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/store_linux_test.go:11:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox.go:571:36: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/service_common.go:317:246: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/endpoint.go:550:17: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/sandbox_dns_unix.go:213:106: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/controller.go:676:85: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/agent.go:876:60: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/resolver.go:324:69: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1153:92: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/network.go:1955:67: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/network.go:2235:9: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/libnetwork_internal_test.go:336:26: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/resolver_test.go:76:35: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:303:38: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/libnetwork_test.go:985:46: empty-lines: extra empty line at the end of a block (revive)
    libnetwork/ipam/allocator_test.go:1263:37: empty-lines: extra empty line at the start of a block (revive)
    libnetwork/errors_test.go:9:40: empty-lines: extra empty line at the end of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-26 19:21:58 +02:00
Eng Zer Jun
c55a4ac779
refactor: move from io/ioutil to io and os package
The io/ioutil package has been deprecated in Go 1.16. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-08-27 14:56:57 +08:00
Sebastiaan van Stijn
686be57d0a
Update to Go 1.17.0, and gofmt with Go 1.17
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-24 23:33:27 +02:00
Sebastiaan van Stijn
c21be64e1a
libnetwork: remove resolvconf/dns package
The IsLocalhost utility was not used, which only leaves the IsIPv4Localhost
utility.

Go's "net" package provides a `IsLoopBack()` check, but it checks for both
IPv4 and IPv6 loopback interfaces. We likely should also do IPv6 here, but
that's better left for a separate change, so instead, I replicated the IPv4
bits from Go's net.IP.IsLoopback().

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-20 00:35:34 +02:00
Sebastiaan van Stijn
c9ba301a49
libnetwork: move resolvconf consts into the resolvconf package
This allows using the package without having to import the "types" package,
and without having to consume github.com/ishidawataru/sctp.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-20 00:23:43 +02:00
Brian Goff
116f200737
Fix gosec complaints in libnetwork
These were purposefully ignored before but this goes ahead and "fixes"
most of them.
Note that none of the things gosec flagged are problematic, just
quieting the linter here.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-25 18:02:03 +02:00
Brian Goff
4b981436fe Fixup libnetwork lint errors
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 23:48:32 +00:00
Brian Goff
a0a473125b Fix libnetwork imports
After moving libnetwork to this repo, we need to update all the import
paths for libnetwork to point to docker/docker/libnetwork instead of
docker/libnetwork.
This change implements that.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-06-01 21:51:23 +00:00
Kamil Domański
226fde5cdd etchosts: allow adding multiple container ips
Signed-off-by: Kamil Domański <kamil@domanski.co>
2019-08-30 23:49:33 +02:00
Tibor Vass
c575631528 resolvconf: use /run/systemd/resolve/resolv.conf if systemd-resolved manages DNS
Signed-off-by: Tibor Vass <tibor@docker.com>
2019-06-04 04:50:37 +00:00
Flavio Crisciani
55ad3ef1a4 Fix handling of the resolv.conf
Leverage what is it passed from the daemon
Fix check about the host networking

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2018-07-24 10:18:10 -07:00
Josh Soref
a06f1b2c4e Spelling fixes
* addresses
* assigned
* at least
* attachments
* auxiliary
* available
* cleanup
* communicate
* communications
* configuration
* connection
* connectivity
* destination
* encountered
* endpoint
* example
* existing
* expansion
* expected
* external
* forwarded
* gateway
* implementations
* implemented
* initialize
* internally
* loses
* message
* network
* occurred
* operational
* origin
* overlapping
* reaper
* redirector
* release
* representation
* resolver
* retrieve
* returns
* sanbdox
* sequence
* succesful
* synchronizing
* update
* validates

Signed-off-by: Josh Soref <jsoref@gmail.com>
2018-07-12 12:54:44 -07:00
Sebastiaan van Stijn
341845b5f2 ndots: produce error on negative numbers
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-06-29 01:22:17 +02:00
Sebastiaan van Stijn
b306706062 improve error message for invalid ndots number
instead of printing the whole option, print the _number_ only,
because that's what the error-message is pointing at;

Before this change:

    invalid number for ndots option ndots:foobar

After this change:

    invalid number for ndots option: foobar

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-06-29 01:19:34 +02:00
Sebastiaan van Stijn
6e260332e8 do not ignore user-provided "ndots:0" option
`ndots:0` is a valid DNS option; previously, `ndots:0` was
ignored, leading to the default (`ndots:0`) also being applied;

Before this change:

    docker network create foo
    docker run --rm --network foo --dns-opt ndots:0 alpine cat /etc/resolv.conf
    nameserver 127.0.0.11
    options ndots:0 ndots:0

After this change:

    docker network create foo
    docker run --rm --network foo --dns-opt ndots:0 alpine cat /etc/resolv.conf
    nameserver 127.0.0.11
    options ndots:0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2018-06-29 00:58:36 +02:00
Flavio Crisciani
5a658d4cc1 Fix user specified ndots option
Setting ndots to 0 does not allow to resolve search domains
The default will remain ndots:0 that will directly resolve
services, but if the user specify a different ndots value
just propagate it into the container

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2018-01-26 10:07:08 -08:00
Flavio Crisciani
78627b6f14 Fix ndots configuration
When ndots was being explicitely passed in the daemon conf
the configuration landing into the container was corrupted
e.g. options ndots:1 ndots:0
The fix just removes the user option so that is not replicated

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2017-10-20 18:26:03 +02:00
Flavio Crisciani
729d45379f Fix lint issues
The package updated and now shows new warnings that had to be corrected
to let the CI pass

Signed-off-by: Flavio Crisciani <flavio.crisciani@docker.com>
2017-09-20 08:57:02 -07:00
Yuanhong Peng
10c88fc3ab Ignore "no such file" error when docker cannot find resolv.conf
`/etc/resolv.conf` is not an essential file in filesystem. (see
http://man7.org/linux/man-pages/man5/resolv.conf.5.html)

> If this file does not exist, only the name server on the local machine
> will be queried

It's baffling to users that containers can start with an empty
`resolv.conf` but cannot without this file.

This PR:
* ignore this error and use default servers for containers in `bridge`
  mode networking.
* create an empty resolv.conf in `/var/lib/docker/containers/<id>` in
  `host` mode networking.

Signed-off-by: Yuanhong Peng <pengyuanhong@huawei.com>
2017-08-22 10:20:44 +08:00
Derek McGowan
710e0664c4 Update logrus to v1.0.1
Fix case sensitivity issue
Update docker and runc vendors

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
2017-08-07 11:20:47 -07:00
Santhosh Manohar
6f507f53e3 Fix the data model inconsistency that breaks daemon upgrade to 1.14-dev
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2017-01-19 14:25:26 -08:00
Alessandro Boch
6dc6fb703b Merge pull request #1595 from sanimej/host
Add support in embedded DNS server for host loopback resolver
2017-01-05 12:18:48 -08:00
Santhosh Manohar
bf832ec2a7 Add embedded DNS server support for host loopback resolver
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-12-22 14:34:13 -08:00
Sebastiaan van Stijn
6c3fc691e7 Fix fqdn hostnames not added to /etc/hosts
This fixes an issue where using a fqdn as hostname
not being added to /etc/hosts.

The etchosts.Build() function was never called
with an IP-address, therefore the fqdn was not
added.

The subsequent updateHostsFile() was not updated
to support fqdn's as hostname, and not adding
the record correctly to /etc/hosts.

This patch implements the functionality in
updateHostsFile()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2016-12-12 22:13:45 +01:00
Daehyeok Mun
7f473c779a Refactoring logrus import and formatting
This fix tries to fix logrus formatting by removing `f` from
`logrus.[Error|Warn|Debug|Fatal|Panic|Info]f` when formatting string
is not present.
Also fix import name to use original project name 'logrus' instead of
'log'

Signed-off-by: Daehyeok Mun <daehyeok@gmail.com>
2016-11-08 12:42:41 -07:00
Santhosh Manohar
2b0d5b1339 Check for ExecFunc failure
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-09-21 13:53:08 -07:00
msabansal
7f43fd30f3 DNS support
Signed-off-by: msabansal <sabansal@microsoft.com>
2016-09-20 13:02:02 -07:00
Santhosh Manohar
db9a7021ac Drop queries in root doamin when ndots is set
Signed-off-by: Santhosh Manohar <santhosh@docker.com>
2016-09-12 14:25:05 -07:00
Jana Radhakrishnan
b0f5f02224 Merge pull request #1284 from liubin/fix-typos
fix typos
2016-08-01 09:49:00 -07:00
Derek McGowan
ccabedfbe3 Fix file descriptor leaks
Ensures network connections and file are closed when done writing.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
2016-07-01 16:29:51 -07:00