Commit graph

788 commits

Author SHA1 Message Date
Thomas Haller
f5e8bbc8e0 libnm,core: enable "onlink" flags also for IPv6 routes
Previously, onlink (RTNH_F_ONLINK) did not work for IPv6.
In the meantime, this works in kernel ([1], [2]). Enable it also
in NetworkManager.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fc1e64e1092f62290d59151d16f9de0210e303c8
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=68e2ffdeb5dbf54bc3a0684aa4e73c6db8675eed

https://github.com/NetworkManager/NetworkManager/pull/337
2019-04-10 09:02:35 +02:00
Beniamino Galvani
8698f512d7 platform: assume 'sriov_drivers_autoprobe' is 1 when sysfs file is missing
'sriov_drivers_autoprobe' was added in kernel 4.12. With previous
kernel versions NM is currently unable to set any SR-IOV parameter
because it tries to read 'sriov_drivers_autoprobe' which doesn't
exist, assumes that current value is -1 and tries to change it,
failing.

When the file doesn't exist, drivers are automatically probed so we
can assume the value is 1. In this way NM is able to activate a
connection with sriov.autoprobe-drivers=1 (the default) even on older
kernel versions.

Fixes: 1e41495d9a ('platform: sriov: write new values when we can't read old ones')

https://bugzilla.redhat.com/show_bug.cgi?id=1695093
2019-04-04 15:35:13 +02:00
Thomas Haller
dc64745dd8 platform: set errno to ENOENT for nm_platform_sysctl_get() 2019-04-04 15:35:13 +02:00
Thomas Haller
ccf59be7f5 platform: set fib_rule_hdr.table to RT_TABLE_UNSPEC for tables larger 255
No difference in practice, because kernel will ignore the table field
is FRA_TABLE attribute is present.
2019-03-27 16:23:30 +01:00
Beniamino Galvani
fafde171ea platform: add support for bridge vlans 2019-03-26 17:19:39 +01:00
Beniamino Galvani
be2fbd0c69 platform: add @family argument to new-link function
This will be used to create bridge vlans, which require an AF_BRIDGE
message family.
2019-03-26 17:19:39 +01:00
Thomas Haller
9992ac1cf8 platform: add routing-rule add/delete netlink functions 2019-03-13 09:03:59 +01:00
Thomas Haller
9934a6a0e3 platform: add support for routing-rule objects and cache them in platform
Add and implement NMPlatformRoutingRule types and let the platform cache
handle rules.

Rules are special in two ways:

- they don't have an ifindex. That makes them different from all other
  currently existing NMPlatform* types, which have an "ifindex" field and
  "implement" NMPlatformObjWithIfindex.

- they have an address family, but contrary to addresses and routes, there
  is only one NMPlatformRoutingRule object to handle both address
  families.

Both of these points require some special considerations.

Kernel treats routing-rules quite similar to routes. That is, kernel
allows to add different rules/routes, as long as they differ in certain
fields. These "fields" make up the identity of the rules/routes. But
in practice, it's not defined which fields contribute to the identity
of these objects. That makes using the netlink API very hard. For
example, when kernel gains support for a new attribute which
NetworkManager does not know yet, then users can add two rules/routes
that look the same to NetworkManager. That can easily result in cache
inconsistencies.

Another problem is, that older kernel versions may not yet support all
fields, which NetworkManager (and newer kernels) considers for identity.
The older kernel will not simply reject netlink messages with these unknown
keys, instead it will proceed adding the route/rule without it. That means,
the added route/rule will have a different identity than what NetworkManager
intended to add.
2019-03-13 09:03:59 +01:00
Thomas Haller
b9ee40b86b platform: separate the refresh-type from the object type
Currently, there is a directy one to one relation between

 - DELAYED_ACTION_TYPE_REFRESH_ALL_*

 - REFRESH_ALL_TYPE_*

 - NMP_OBJECT_TYPE_*

For IP addresses, routes and routing policy rules, when we request
a refresh-all (NLM_F_DUMP), we want to specify the address family.

For addresses and routes that is currently solved by having two
sets of NMPObject types, for each address family one.

I think that is cumbersome because the implementations of both address
families are quite similar. By implementing both families as different
object types, we have a lot of duplicate code and it's hard to see where
the families actually differ. It would be better to have only one NMPObject
type, but then when we "refresh-all" such types, we still want to be able
to dump all (AF_UNSPEC) or only a particular address family (AF_INET, AF_INET6).

Decouple REFRESH_ALL_TYPE_* from NMP_OBJECT_TYPE_* to make that
possible.
2019-03-13 09:03:59 +01:00
Thomas Haller
0a2a861782 platform/trivial: rename enum DELAYED_ACTION_IDX_REFRESH_ALL_* to REFRESH_ALL_TYPE_*
While these numbers are strongly related to DELAYED_ACTION_TYPE_REFRESH_ALL_*,
they differ in their meaning.

These are the refresh-all-types that we support. While some of the delayed-actions
are indeed for refresh-all, they are not the same thing.

Rename the enum.
2019-03-13 09:03:59 +01:00
Thomas Haller
7c5ad2d910 platform: drop unused nm_platform_refresh_all()
The function is unused. It would require redesign to work with
future changes, and since it's unused, just drop it.

The long reasoning is:

    Currently, a refresh-all is tied to an NMPObjectType. However, with
    NMPObjectRoutingRule (for policy-routing-rules) that will no longer
    be the case.

    That is because NMPObjectRoutingRule will be one object type for
    AF_INET and AF_INET6. Contrary to IPv4 addresses and routes, where
    there are two sets of NMPObject types.

    The reason is, that it's preferable to treat IPv4 and IPv6 objects
    similarly, that is: as the same type with an address family property.

    That also follows netlink, which uses RTM_GET* messages for both
    address families, and the address family is expressed inside the
    message.

    But then an API like nm_platform_refresh_all() makes little sense,
    it would require at least an addr_family argument. But since the
    API is unused, just drop it.
2019-03-13 09:03:59 +01:00
Thomas Haller
bbfb8a9b33 platform: suppress unnecessary logging in do_request_all_no_delayed_actions()
When we refresh all links, we clear all flags to refresh a specific
link. However, only log a message if there was anything to clear.
2019-03-13 09:03:59 +01:00
Thomas Haller
4e399d82ac platform/wireguard: fix WGPEER_A_LAST_HANDSHAKE_TIME to use int64 typed timespec structure
The netlink API changed for WireGuard. Adjust for that.

https://git.zx2c4.com/WireGuard/commit/?id=c870c7af53f44a37814dfc76ceb8ad88e290fcd8
2019-03-07 17:54:25 +01:00
Lubomir Rintel
d551a0893e platform/linux: fix detection of IFA_FLAGS support
The condition got accidentally reversed, which means we're always
undecided and thus wrongly assuming support and never being able to set
any addresses.

This would bother the few that are struck with 3.4 android kernels. Very
few indeed, given this got unnoticed since 1.10.

Fixes: 8670aacc7c ('platform: cleanup detecting kernel support for IFA_FLAGS and IPv6LL')
2019-03-07 10:17:42 +01:00
Thomas Haller
bf863aba93 platform: move creation of nlmsg dump request in separate function
Split out the creation of the nlmsg with the netlink dump request.
Also, add a (still unused) parameter to overwrite the preferred
address family.
2019-02-22 10:08:00 +01:00
Thomas Haller
40e0ff9fbf platform: pass lookup instance to nmp_cache_dirty_set_all() instead of object-type
We already have NMPLookup to express a set of objects that can be looked
up via the multi-index.

Change the behavior of nmp_cache_dirty_set_all(), not to accept an
object-type. Instead, make it generally useful and accept a lookup
instance that is used to filter the elements that should be set as
dirty.
2019-02-22 10:08:00 +01:00
Thomas Haller
d431de70ac platform: refactor FOR_EACH_DELAYED_ACTION() macro to have only one for(;;) statement
for-each macros can be nice to use. However, such macros are potentially
error prone, as they abstract C control statements and scoping blocks.

I find it ugly to expand the macro to:

    for (...)
       if (...)

Instead, move the additional "if" check inside the loop's condition
expression, so that the macro only expands to one "for(;;)" statement.
2019-02-22 10:07:33 +01:00
Thomas Haller
7bb6433214 platform: create id-only objects for deleting qdisc/tfilter in event_valid_msg()
It does not matter too much, but the code optimizes for deletion events.
In that case, we don't require parsing the full netlink message.
Instead, it's enough to parse only the ID part so that we can find the
object in the cache that is to be removed removed.

Fix that for qdisc/tfilter objects.
2019-02-22 10:06:35 +01:00
Thomas Haller
e43d1d90f3 platform: minor cleanup in event_valid_msg() 2019-02-22 10:05:00 +01:00
Thomas Haller
60e4595101 platform: cleanup parsing of RTA_MULTIPATH in _new_from_nl_route()
I think the code before was correct. At the very least because
we only run the while-loop at most once because multipath routes
are not supported.

However, it seems odd that the while loop checks for

    "tlen >= rtnh->rtnh_len"

but later we do

    "tlen -= RTNH_ALIGN (rtnh->rtnh_len)"

Well, arguably, tlen itself is aligned to 4 bytes (as kernel sends
the netlink message that way). So, it was indeed fine.

Still, confusing. Try to check more explicitly for the buffer sizes.
2019-02-22 10:05:00 +01:00
Thomas Haller
1b7e89ad7d platform: use nla_data_as() at some places
nla_data_as() has a static assertion that casting to the pointer is
valid (regarding the alignment of the structure). It also contains
an nm_assert() that the data is in fact large enough.

It's safer, hence prefer it a some places where it makes sense.
2019-02-22 10:04:48 +01:00
Thomas Haller
f7e4310a0f platform: use nm_auto_nlmsg cleanup macro instead of explict nlmsg_free() 2019-02-22 09:58:09 +01:00
Thomas Haller
a41ca7cc89 platform: use nlmsg_append_struct() macro instead of nlmsg_append()
- nlmsg_append_struct() determines the size based on the argument.
  It avoids typing, but more importantly: it avoids typing redundant
  information (which we might get wrong).

- also, declare the structs as const, where possible.
2019-02-22 09:58:09 +01:00
Thomas Haller
98e42a4552 platform: prettify parsing MACSec from netlink 2019-02-22 09:58:09 +01:00
Thomas Haller
dca6d24aab platform: drop READ_STAT64() macro from _new_from_nl_link()
It doesn't make it easier to read. Also, the code comment points
out something rather obvious, as we later use unaligned API to access the
fields.
2019-02-22 09:58:09 +01:00
Thomas Haller
9cc592ae05 platform: don't use memset() to initialize variable in _new_from_nl_route() 2019-02-22 09:58:09 +01:00
Thomas Haller
495cfd9129 platform: sort #include in "nm-linux-platform.h" 2019-02-22 09:58:09 +01:00
Thomas Haller
451073e5bb platform: use nla_get_be64() helper 2019-02-22 09:58:09 +01:00
Thomas Haller
6c24846929 platform/trivial: coding style fixes/whitespace 2019-02-22 09:58:09 +01:00
Thomas Haller
6f8208c0d4 platform/netlink: cleanup nla_parse*() code by using safer macros
- drop explicit MAX sizes like

      static const struct nla_policy policy[IFLA_INET6_MAX+1] = {

  The compiler will deduce that.

  It saves redundant information (which is possibly wrong). Also,
  the max define might be larger than we actually need it, so we
  just waste a few bytes of static memory and do unnecesary steps
  during validation.

  Also, the compiler will catch bugs, if the array size of policy/tb
  is too short for what we access later (-Warray-bounds).

- avoid redundant size specifiers like:

      static const struct nla_policy policy[IFLA_INET6_MAX+1] = {
      ...
      struct nlattr *tb[IFLA_INET6_MAX+1];
      ...
      err = nla_parse_nested (tb, IFLA_INET6_MAX, attr, policy);

- use the nla_parse*_arr() macros that determine the maxtype
  based on the argument types.

- move declaration of "static const struct nla_policy policy" variable
  to the beginning, before auto variables.

- drop unneeded temporay error variables.
2019-02-22 09:58:09 +01:00
Thomas Haller
a4cff10ceb platform: fix error handling for creating nlmsg in do_request_all_no_delayed_actions()
In practice, we don't fail to create the nlmsg, because in glib
malloc() cannot fail and we always create large enough buffers.

Anyway, handle the error correctly, and reduce the in-progress
counter again.
2019-02-22 09:58:09 +01:00
Thomas Haller
fc9d661018 platform/wireguard: fix parsing WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL from netlink
Fixes: 0827d4c2e4
2019-02-18 15:10:07 +01:00
Thomas Haller
9ae71bf555 all: use nm_c_list_move_*() helpers 2019-02-18 15:00:10 +01:00
Thomas Haller
153b41fa97 platform: add peer_flags argument to nm_platform_link_wireguard_change() 2019-02-14 08:00:29 +01:00
Thomas Haller
1e1b03c089 platform: add flags for setting individual WireGuard options of link 2019-02-14 08:00:29 +01:00
Thomas Haller
2ed01e2e34 platform: add change-flags argument to platform's link_wireguard_change()
We will need more flags.

WireGuard internal tools solve this by embedding the change flags inside
the structure that corresponds to NMPlatformLnkWireGuard. We don't do
that, NMPlatformLnkWireGuard is only for containing the information about
the link.
2019-02-14 08:00:29 +01:00
Thomas Haller
9beed4f661 all: replace strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
a4fb6ddfca all: replace g_strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
737ab51472 all: include "nm-utils/nm-errno.h" via "nm-default.h" 2019-02-12 08:50:28 +01:00
Thomas Haller
4d9918aac2 all: assert that native errno numbers are positive
Use the NM_ERRNO_NATIVE() macro that asserts that these errno numbers are
indeed positive. Using the macro also serves as a documentation of what
the meaning of these numbers is.

That is often not obvious, whether we have an nm_errno(), an nm_errno_native()
(from <errno.h>), or another error number (e.g. WaitForNlResponseResult). This
situation already improved by merging netlink error codes (nle),
NMPlatformError enum and <errno.h> as nm_errno(). But we still must
always be careful about not to mix error codes from different
domains or transform them appropriately (like nm_errno_from_native()).
2019-02-12 08:50:28 +01:00
Thomas Haller
047998f80a all: cache errno in local variable before using it 2019-02-12 08:50:28 +01:00
Thomas Haller
a3370af3a8 all: drop unnecessary includes of <errno.h> and <string.h>
"nm-macros-interal.h" already includes <errno.h> and <string.h>.
No need to include it everywhere else too.
2019-02-12 08:50:28 +01:00
Beniamino Galvani
3a0f7114fe platform: limit the maximum size of sysctl cache
When the logging level is DEBUG or TRACE, we keep all the sysctl
values we read in a cache to log how they change. Currently there is
no limit on the size of this cache and it can take a large amount of
memory.

Implement a LRU cache where the oldest entries are deleted to make
space for new ones.

https://github.com/NetworkManager/NetworkManager/pull/294
2019-02-10 10:38:54 +01:00
Thomas Haller
ba1bf0390d logging: make _nm_logging_clear_platform_logging_cache() a regular function
Previously, _nm_logging_clear_platform_logging_cache was an extern variable,
and NMLinuxPlatform would set it to a function pointer at certain points.

That's unnecessary complex, also when trying to make nm-logging thread-safe,
it's just more global variables that need to be considered. Don't do it
that way, but just link in a regular function.
2019-02-05 08:18:07 +01:00
Lubomir Rintel
ef6d461b7f platform/linux: fix setting of IFA_ADDRESS without a peer
Since commit 9ecdba316 ('platform: create netlink messages directly
without libnl-route-3') we're unconditionally setting IFA_ADDRESS to
the peer address, even if there's no peer and it's all zeroes.

The kernel actually stopped caring somewhere around commit caeaba790
('ipv6: add support of peer address') in v3.10, but Ubuntu Touch likes
to run Android's v3.4 on some poorly supported hardware.

Fixes: 9ecdba316c

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/77
2019-02-05 07:11:12 +01:00
Rafael Fontenelle
d81e10942f all: fix misspellings
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/64
2019-01-24 17:19:44 +01:00
Thomas Haller
6f8c7b580d platform: add @replace_peers argument to nm_platform_link_wireguard_change()
The caller may not wish to replace existing peers, but only update/add
the peers explicitly passed to nm_platform_link_wireguard_change().

I think that is in particular interesting, because for the most part
NetworkManager will configure the same set of peers over and over again
(whenever we resolve the DNS name of an IP endpoint of the WireGuard
peer).

At that point, it seems disruptive to drop all peers and re-add them
again. Setting @replace_peers to %FALSE allows to only update/add.
2019-01-22 16:30:23 +01:00
Thomas Haller
32749cea99 platform: support missing endpoint in _wireguard_create_change_nlmsgs() 2019-01-22 16:30:23 +01:00
Thomas Haller
977b033d12 platform: improve API of sockaddr handling
Add cmp/hash functions that correctly honor the well known fields, instead
of doing memcmp/memcpy of the entire sockaddr structure.

Also, move the set function to nm_sock_addr_union_cpy() and
nm_sock_addr_union_cpy_untrusted(). This also gets it right
to ensure all bytes of the union are initialized (to zero).
2019-01-22 16:30:23 +01:00
Thomas Haller
a5c894c35f platform: create wireguard netdev interface
The netlink code for WG_CMD_SET_DEVICE is strongly inspired by
WireGuard ([1]) and systemd ([2]).

Currently, nm_platform_link_wireguard_change() always aims to reset
all peers and allowed-ips settings. I think that should be improved
in the future, to support only partial updates.

[1] https://git.zx2c4.com/WireGuard/tree/contrib/examples/embeddable-wg-library/wireguard.c?id=5e99a6d43fe2351adf36c786f5ea2086a8fe7ab8#n1073
[2] 04ca4d191b/src/network/netdev/wireguard.c (L48)
2019-01-09 16:46:41 +01:00