Commit Graph

232 Commits

Author SHA1 Message Date
Kir Kolyshkin 14c831e003 libnetwork/netavark: simplify isMacVlan init
This fixes the following staticcheck warning:

> libnetwork/netavark/config.go:297:2: QF1007: could merge conditional assignment into variable declaration (staticcheck)
> 	isMacVlan := true
> 	^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-26 09:36:14 -07:00
Kir Kolyshkin 555b817630 libnetwork/types: rename RegexError to ErrInvalidName
... and add a deprecated alias so backward compatibility is still
preserved (and users can gradually switch to the new name).

Done because this is now also reported by staticcheck
(in addition to revive) linter.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-26 09:36:14 -07:00
Kir Kolyshkin 41e8b24890 libnetwork: simplify write
Fix the following staticcheck warning:

> libnetwork/slirp4netns/slirp4netns.go:685:15: QF1012: Use fmt.Fprintf(...) instead of Write([]byte(fmt.Sprintf(...))) (staticcheck)
> 	if _, err := conn.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
> 	             ^

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-03-26 09:36:14 -07:00
Lucas Pablo Calisi 4a929deddf fix(libnetwork): interface name length validation
Implements interface name length validation during network creation to prevent
netlink errors when names exceed the kernel's 15-character limit.

This prevents creation of networks with interface names that would cause failures
when running containers.

Signed-off-by: Lucas Pablo Calisi <lucas.calisi@mercadolibre.com>
2025-03-20 11:02:13 -03:00
Paul Holzinger dc745a5a0b vendor: update to go.etcd.io/bbolt v1.4.0
Need to replace one deprecated error type alias.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-03-18 11:59:28 +01:00
Paul Holzinger 829598d49a libnetwork/netavark: dealloc ips on setup error
While we already did clean up the allocated ips on the regular setup
error this did not worked for errors from the rootlessNetns.Setup()
call. To ensure we dealloc the ips on all error paths use a defer
function that checks the return error value.

Fixes containers/podman#25422

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-03-03 14:04:33 +01:00
Paul Holzinger 04b578ae9a fix issues reported by errcheck
Mainly type casting issues. I ignored some of them where I don't think
it can fail or when it is in tests where we would notice anyway.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-02-14 11:50:50 +01:00
openshift-merge-bot[bot] 47b9c19c43 Merge pull request #2308 from Luap99/bridge-vlan
libnetwork/netavark: allow same bridge name with different vlan
2025-01-31 16:24:17 +00:00
Paul Holzinger b0fc9a6550 libnetwork/netavark: allow same bridge name with different vlan
When a vlan is used there should be no bridge name conflict check. It is
totally valid to have the same bridge with different vlans in two
configs and that is the intended use case.

Fixes #2095

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-01-30 19:28:24 +01:00
Paul Holzinger 67826c0c25 rootlessnetns: fix setup error when file already exists
It is possible that the netns file where we bind mount the netns already
exists. This can happen if a previous setup process was killed between
creating the file and mounting to it. Or likely more common as described
in the podman issue if the runroot is not a tmpfs and not deleted after
boot.

Fixes containers/podman#25144

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-01-30 13:52:04 +01:00
Paul Holzinger 6dfd732abc libnetwork/netavark: do not create config dir in init
Podman creates/initializes the network backend for every command. However
most commands will not need it so we should keep the required actions we
do to a minimum.

In this case the config directory /etc/containers/networks by default as
root may not exists and then we try to create it which can fail, i.e.
when /etc is read only[1].

The code here are a bit more changes then I would have liked but we must
make sure the default in memory network always exists and do not create
the directory there.

[1] https://github.com/containers/common/pull/2265

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-12-09 14:18:30 +01:00
openshift-merge-bot[bot] 7e20cb769e Merge pull request #2258 from Luap99/pasta-link-local
libnetwork/pasta: do not ignore ipv4 link local
2024-12-04 15:37:39 +00:00
George Joseph be77cc4e2d Allow Podman to send the container's hostname to Netavark
* Added ContainerHostname to NetworkOptions. Podman will set this
  and Netavark will read it.

* Added the `container_name_as_hostname` option to the
  CONTAINERS table in containers.conf.  Currently, if you don't
  explicitly set a hostname when creating a container, podman will
  set it to the short ID. If this option set to `true` and a
  hostname isn't explicitly set, podman will use the container's
  name, with characters not in the set `[0-9a-zA-Z.-]` removed,
  as the hostname instead of the short ID. Set to false by default
  to preserve existing behavior.

Signed-off-by: George Joseph <g.devel@wxy78.net>
2024-11-28 13:35:36 -07:00
Paul Holzinger 4fae670ff5 libnetwork/pasta: do not ignore ipv4 link local
Starting with pasta 2024_11_27.c0fbc7e there is new "local mode"[1] in
pasta that defaults to setting up link local addresses in the netns when
no suitable interface was found. this is done to fix the podman issue[2]
where we fail to start in these cases which was a poor UX. Now the pasta
change alone works fine for these users but there is one problem.

Podman adds hosts entries for the container ip/name tuple and for the
host.containers.internal. These entries are filtered out thus neither
ipv4 or ipv6 bool was set and no addresses where added to IPAddresses.
Thus podman had no info to add entries and just left them empty, while
for most cases this is fine there might be a few users who expect
host.containers.internal and the container name to resolve correctly.

This commit changes the logic to only skip ipv6 link local addresses but
allow ipv4 link local addresses. With that podman will add the proper
entry.

[1] https://archives.passt.top/passt-dev/20241127042725.3133538-1-sbrivio@redhat.com/
[2] https://github.com/containers/podman/issues/24614

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-11-27 15:51:18 +01:00
Michael Zimmermann 336b85849c libnetwork: support `mode` option for bridge
L2 will be used to allow using existing bridges which netavark will
neither create nor delete.

Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
2024-11-25 18:10:36 +01:00
Michael Zimmermann 0ac7770583 PerNetworkOptions: add driver specific options
This allows us to pass through data to netavaark without bloating this
struct. See #24523.

Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
2024-11-11 22:43:09 +01:00
Paul Holzinger 27aeb4ad8c libnetwork/resolvconf: filter out ipv6 link local
One thing I noticed in the recent aardvark-dns bug[1] that we copy link
local nameservers into the container. This makes no sense as the link
local address contains a zone (interface name/index) and cannot work
without it. However a container by design will have a different
interface name/index so the address can never work in the normal case.

Only when we do share the host netns then we should keep it.

[1] https://github.com/containers/aardvark-dns/pull/537

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-11-07 16:49:02 +01:00
Paul Holzinger 43eb4478e4 libnetwork/pasta: remove Setup2()
This was just added as alias to allow vendoring without having to fix
all callers at the same time.

These are the PRs that update the callers:
https://github.com/containers/podman/pull/24417
https://github.com/containers/buildah/pull/5724

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-10-30 13:42:50 +01:00
openshift-merge-bot[bot] 81540fe91b Merge pull request #2136 from Luap99/pasta-map-guest-addr
pasta: use new --map-guest-addr option
2024-09-06 12:32:48 +00:00
Paul Holzinger 805e7ae406 update golangci-lint to 1.60.3
Contains fixes for new linters, removed depracted and removed linters
from the config.

Most notably because we use go 1.22 now we can get rid of the copy for
loop vars[1]. Also as of the go 1..2 we can use the new int range syntax
in for loops the new intrange linter checks that.

[1] https://go.dev/blog/loopvar-preview

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-03 15:57:02 +02:00
Paul Holzinger 564e755ad8 libnetwork/etchosts: add PreferIP option
For the pasta network mode we now use --map-guest-addr which means we
have a specific ip that we want to use as host.containers.internal
address. I first thought we could handle it in podman but that doesn't
work as the contianers.conf option must have a higher priority.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 11:04:36 +02:00
Paul Holzinger 0f62427d9d libnetwork/etchosts: rework GetHostContainersInternalIP()
GetHostContainersInternalIP() is no longer called in podman or buildah
as they use GetHostContainersInternalIPExcluding(). I need to add a new
option so chnage the function to accept the parameters as struct so we
do not have to break the API every time we add a new parameter.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 11:02:08 +02:00
Paul Holzinger 31a2c9818a pasta: update warnings line
Use %s as %q just quotes/escapes everything which makes it harder to
read and trim of the last newline and spaces as well.

Also update the warnings comment, we still see warnings by default on
our debian VMs in podman CI so this cannot be on the warning level yet.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:58 +02:00
Paul Holzinger 74553777c6 rootlessnetns: cache dns and guest addr options
When using the rootless netns (bridge mode) so far podman ignored the
proper pasta or slirp4netns dns sever for networks without aardvark-dns.
This is not good. We should try to use them by default, and with the new
MapGuestAddr option we need to use that as well for
host.containers.internal. The problem is that becuase we only know what
options we uses when we started the process later container starts from
a new podman process do not really see these options if we just cache
the result in memory. So in order to make all following podman process
aware we serialize this info struct as json and later processes read it
when needed.

It also means we do not have to lookup the netns ip evey time so I
removed that code.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:58 +02:00
Paul Holzinger a3c9f2bd9d pasta: rename Setup2() to Setup()
I already switch all user from the old Setup over to Setup2(), so no we
can again reuse the Setup() name. As such alias Setup and Setup for the
same function and then once I migrated all callers in podman and buildah
I will remove Setup2() here.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:58 +02:00
Paul Holzinger 13e70455ad pasta: make sure --map-guest-addr is backwards compatible
--map-guest-addr was just added in 20240814, we cannot yet hard require
this option to be present. This means we must deal with the case where
the option is not working. Both a version check or checking --help would
add extra overhead in the good case. To avoid this we try first with the
new option and if this fails check the error message for the right
error. If it didn't know about the new option we remove it and try to
exec pasta again.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:58 +02:00
Paul Holzinger 374514285f pasta: add new --map-guest-addr option
The --map-guest-addr option allows us to sepcify a ip that is remapped
to the actual host ip that was used by pasta. This is done to fix the
problem where connecting to the host ip was not possible as the same ip
was used in the netns.

We now set --map-guest-addr 169.254.1.2 which follows the same idea we
already used for the --dns-forward option. With that podman can use this
ip to set it for host.containers.internal which should the case where
there was no second host ip available, see
https://github.com/containers/podman/issues/19213

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:56 +02:00
Paul Holzinger 12e36c11c1 pasta: switch dns ip to 169.254.1.1
Per feedback[1] the 169.254.0.0/24 range is reserved for future use in
RFC 3927. As such we should not use it here as it might break in the
future if the range gets assigned a new meaning. Switch to 169.254.1.1.

[1] https://github.com/containers/podman/pull/23791#discussion_r1737913730

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-02 10:36:39 +02:00
Jerome Degroote d8c44e54c8 Add multiple host for single IP
Signed-off-by: Jerome degroote <jeromedu59230@gmx.fr>
2024-08-29 10:47:51 +02:00
Kir Kolyshkin 35ff885555 Remove old-style build tags
Brought to you by

	go1.23rc2 fix ./...

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-08-09 17:09:19 -07:00
Paul Holzinger 2a6838e4e2 rootlessnetns: make cleanup idempotent
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-08-08 16:46:38 +02:00
Paul Holzinger 066200cf4b rootlessnetns: fix cleanup for errors in Run
The Run() function is used to run long running command in the netns,
namly podman unshare --rootless-netns used that. As such the function
actually unlocks for the main command as otherwise a user could hold the
lock forever effectively causing deadlocks.

Now because we unlock the ref count might change during that time. And
just because we create the netns doesn't mean there are now other users
of the netns. Therefore the cleanup in runInner() was wrong in that
case causing problems for other running containers.

To fix this make sure we do not cleanup in the Run() case unless the
count is 0.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-08-08 16:45:42 +02:00
Paul Holzinger ae5b548b78 rootlessnetns: decrement refCount after teardown
When I wrote this originally I thought we must avoid leaking the netns
so I tried to decrement first. However now I think this wrong because
podman actially calls into the cleanup function again if it returned an
error on the next cleanup attempt. As such we ended up doing a double
decrement and the ref counter went below zero causing a sort of issues[1].

Now if we have a bug the other way around were we not decrement
correctly this is much less of a problem. It simply means we leak once
netns file and the pasta/slirp4netns process which isn't a problem other
than needed a bit of resources.

[1] https://github.com/containers/podman/issues/21569

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-08-08 16:45:42 +02:00
Paul Holzinger 17b90ccc2e libnetwork/netavark: fix rootless unit tests
Commit 56c6a9ac07 added check for the env var inside the network
interface setup code as this is something that is always set by podman.
However if you try to run the unit tests as rootless they now always
failed as the code assumed we have access to a rootful path.

The easy fix is to just fix the test to set the env when running
rootless.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-06-25 11:39:33 +02:00
Paul Holzinger 80e2634dee libnetwork/netavark: accept metric option for mac/ipvlan
This option is also supported by netavark for macvlan and ipvlan
networks.

Fixes #2051

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-06-25 11:32:01 +02:00
Antoine Blin b9edbab93a Allow --debug/--trace options for pasta
Signed-off-by: Antoine Blin <antoine.blin3@gmail.com>
2024-06-18 14:27:16 +02:00
Paul Holzinger 77fd934291 libnetwork/rootlessnetns: create run dir explicitly
Currently it does the mkdir only implicitly because the code creates
run/systemd but this only happens when /run/systemd exists on the host.
As such the rootless code was broken on all non systemd distros[1].

[1] https://github.com/containers/podman/discussions/22903

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-06-05 11:37:09 +02:00
Paul Holzinger 56c6a9ac07 libnetwork: fix rexec env check for rootlessnetns
For some unknown reason the podman container image sets the
_CONTAINERS_USERNS_CONFIGURED env to an empty value. I don't know what
the purpose of this is but is will trigger the check here which is wrong
when the container is privileged.

To fix this check that the value is set to done like it is by the reexec
logic. Also make sure the lock dir uses the same condition to stay
consistent.

Fixes containers/podman#22791

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-05-27 15:18:43 +02:00
openshift-merge-bot[bot] 0fb7fdabe0 Merge pull request #1998 from Luap99/rootlessnetns-info
libnetwork: add option to return rootless-netns ips
2024-05-16 18:50:24 +00:00
Paul Holzinger bed6a2dd43 libnetwork: add option to return rootless-netns ips
When using the bridge network mode as rootless we use the rootless netns
logic, for podman this looks like just as using bridge as root. The
issue is however due the extra namespace we block certain address there.
This can be seen best with pasta but actually effects other cases too.
The podman logic tries to use any host ip address for
host.containers.internal but we must make sure to exculde all these
address in the rootless netns as they are not actually the hostns as
thus cause great confusion.

For the --network pasta case I already fixed this by returning the ips on
the pasta.Setup2() call in 83573fa60c.
For the bridge mode this more complicated due several layers of function
calls. I decided to implement this as extra function call on the interface
to return the ips as this makes the usage in podman the easiest. And I
also didn't want to break the API as we only have to fix this in podman
not buildah.

It is needed to address #22653 but it needs podman changes as well to
use this new function.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-05-16 15:24:52 +02:00
Paul Holzinger 8dad68ced6 libnetwork/cni: remove dnsname dependency from tests
Skip or remove tests that need dnsname in order to function.
As of fedora 40 dnsname is no longer packaged so our CI VM cannot use
it.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-05-15 18:23:29 +02:00
Paul Holzinger ced4bf5f7b libnetwork/pasta: fix multiple --map-gw parsing
It is possible for a user to set --map-gw more than once so we should
handle it correctly.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-25 15:06:48 +02:00
Paul Holzinger 7c57f840af libnetwork/pasta: append ports after custom opts
The main advantage is that we do not have to iterate through all the
ports we added again to check for the custom ports.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-25 14:50:08 +02:00
Paul Holzinger cced079805 libnetwork/pasta: fix --map-gw parsing
If a port option was given after --map-gw then parsing failed as the
next arg was always skipped due the modification of the slice.

Modifing the slice inside the loop is bad and does not do what some
might think. Append here basically creates a new slice (thus you always
have to assign the result to the variable) with the same pointer to the
same underlying array of data[1]. The loop however will still continue to
loop over the slice as it saw it at the begining of the loop.

So in the bug case the underlying array would look like this:
{"--config-net", "--map-gw", "-T", "80"}
and after the append call to remove --map-gw like this:
{"--config-net", "-T", "80", "80"}

The loop iterator has no idea this happen and just moves to the next
index 2 ("80") and thus we never passed "-T" causing this bug.

[1] https://go.dev/blog/slices-intro

Fixes containers/podman#22477

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-24 15:39:39 +02:00
Paul Holzinger 27d56e6d8e libnetwork/pasta: local vars should be lowercase
Upper case is used for exported types/vars and these are local variables
so make sure the start lower case to not confuse readers.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-24 15:39:22 +02:00
Paul Holzinger 19672e24da libnetwork/pasta: split out argument parsing
So we can add unit tests for this, the code is not so trivial after all.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-24 15:38:02 +02:00
Paul Holzinger 7e485a635f Replace golang.org/x/exp/slices with slices from std
Use "slices" from the standard library, this package was added in go
1.21 so we can use it now.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-22 14:12:47 +02:00
Giuseppe Scrivano c6a7a1cd2a libnetwork: use fileutils.(Le|E)xists
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-04-10 12:23:07 +02:00
Paul Holzinger d4ce3d5274 libnetwork/netavark: remove ipam bucket on network rm
This is good to prevent any leaks but more important here there is a
bug because we cache the last assigned ip. However when a network is
removed the recreated with a different LeaseRange that ip might be very
well outside the expected range and the logic seems to handle this
correctly. I could fix it there but deleting the full bucket seems best
as it avoid other issues and leaking the bucket forever.

Fixes containers/podman#22034

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-03 18:19:57 +02:00
Paul Holzinger 7c89762d52 libnetwork/rootlessnetns: do not cleanup twice in Run()
This here just logs unnecessary errors in case there is an error during
the Run() call (podman unshare --rootless-netns). runInner() will
already call cleanup on errors if it created a new netns so we only need
to cleanup when there is no error.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-04-03 12:04:21 +02:00