Commit graph

1458 commits

Author SHA1 Message Date
Thomas Haller
82e31b2816 platform/netlink: cleanup nla_memcpy()
- use size_t arguments for the memory sizes. While sizes from netlink
  API currently are int typed and inherrently limited, use the more
  appropriate data type.

- rename the arguments. The "count" is really the size of the
  destination buffer.

- return how many bytes we wanted to write (like g_strlcpy()).
  That makes more sense than how many bytes we actually wrote
  because previously, we could not detect truncation.
  Anyway, none of the callers cared about the return-value either
  way.
2019-02-22 09:58:09 +01:00
Thomas Haller
b080146cc6 platform/netlink: cleanup nla_strlcpy()
- let nla_strlcpy() return how many bytes we would like to have
  copied. That way, the caller could detect string truncation.
  In practice, no caller cared about that.

- the code before would also fill the entire buffer with zeros first,
  like strncpy(). We still do that. However, only copy the bytes up
  to the first NUL byte. The previous version would have copied
  "a\0b\0" (with srclen 4) as "a\0b". Strip all bytes after the
  first NUL character from src. That seems more correct here.

- accept nla argument as %NULL.
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
cf22d28c2e platform/netlink: add nla_parse* macros that safely determine the max-type
The common idiom is to stack allocate the tb array. Hence,
the maxtype is redundant. Add macros that autodetect the
maxtype based on the C type infomation.

Also, there is a static assertion that the size of the policy
(if provided) matches.
2019-02-22 09:58:09 +01:00
Thomas Haller
6245569c6d platform/netlink: cleanup unnecessary "goto out" from nla_parse() 2019-02-22 09:58:09 +01:00
Thomas Haller
d8727f6aa9 platform/netlink: fix return value of nla_get_s8() 2019-02-22 09:58:09 +01:00
Thomas Haller
7f1865cad9 platform/wifi: fix size check for parsing netlink attribute NL80211_BSS_BSSID 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
106e4ebc13 platform/netlink: fix _genl_parse_getfamily() for checking error code from genlmsg_parse()
Errors are negative numbers.

This had no effect however, because currently genlmsg_parse() won't ever return
positive values.
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
a3d4dea61f platform: print persistent-keepalive setting of WireGuard peers 2019-02-14 08:00:29 +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
128099151d shared: fix nm_errno_from_native() for negative input
Fixes: 67130e6706
2019-02-12 09:13: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
Thomas Haller
65884733ec all: minor coding style fixes (space before parentheses) 2019-02-11 15:22:57 +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
3458c02acb platform/tests: avoid "-Wmissing-braces" warning in "test-route.c" 2019-02-08 20:14:50 +01:00
Thomas Haller
a44d276f55 libnm: avoid "-Wmissing-braces" warning for test-link.c
../src/platform/tests/test-link.c: In function ‘_test_wireguard_change’:
    ../src/platform/tests/test-link.c:861:16: warning: missing braces around initializer [-Wmissing-braces]
         endpoint = (NMSockAddrUnion) {
                    ^
    ../src/platform/tests/test-link.c:864:21:
           .sin_addr   = nmtst_inet4_from_string (nm_sprintf_buf (s_addr, "192.168.7.%d", i)),
                         {                                                                   }
    ../src/platform/tests/test-link.c:861:16: warning: missing braces around initializer [-Wmissing-braces]
         endpoint = (NMSockAddrUnion) {
                    ^
    ../src/platform/tests/test-link.c:864:21:
           .sin_addr   = nmtst_inet4_from_string (nm_sprintf_buf (s_addr, "192.168.7.%d", i)),
                         {
2019-02-08 20:14:50 +01:00
Thomas Haller
d25ed0820c all: don't use "static inline" in source files
For static functions inside a module, the compiler determines on its own
whether to inline the function.

Also, "inline" was used at some places that don't immediatly look like
candidates for inlining. It was most likely a copy&paste error.
2019-02-06 09:31:00 +01:00
Thomas Haller
c6c6295a96 platform: refactor NMPNetns avoiding global variable
Avoid accessing the global variable @_netns_stack (formerly @netns_stack)
directly. Access it once when needed, and pass it on as parameter.
2019-02-05 08:18:09 +01:00
Thomas Haller
12df49f8ab platform: make NMPNetns thread-safe
NMPNetns instances are immutable, hence they can be easily shared
between threads. All we need, is that the stack of namespaces is
thread-local.

Also note that NMPNetns uses almost no other API, except some bits from
"shared/nm-utils/" and nm-logging. These parts are already supposed to
be thread-safe.

The only complications is that when the thread exits, we need to
destroy the NMPNetns instances. That is especially important because
they hold file descriptors. This is accomplished using pthread's
thread-specific data. An alternative would be C11 threads' tss_create(),
but not all systems that we run against support that yet. This means,
we need to link with pthreads, but we already do that anyway.
Note that glib also requires pthreads. So, we don't get an additional
dependency here.
2019-02-05 08:18:08 +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
Thomas Haller
43a12462e0 platform/tests: increase timeout for platform tests
Seems 180 seconds is still not enough for "/link/create-many-links/1000"
tests on gitlab-CI.
2019-02-04 10:52:19 +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
78ce4307c0 platform: add and use nm_sock_addr_union_to_string()
We still don't use getnameinfo(). This is used for logging,
where we want to see a string representation that is as close
as possible to the actual bytes (to spot differences). It should
not be obfuscated by a libc function out of our control.

Also fix the notation for the IPv6 scope ID to use the common '%'
character.
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
3263cab596 all: add static assertion for maximumg alloca() allocated buffer
Add a compile time check that the buffer that we allocate on the stack
is reasonably small.
2019-01-15 09:52:01 +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
Thomas Haller
fb79c79d87 netlink: add nla_put() helpers to set integers 2019-01-09 16:46:41 +01:00
Thomas Haller
7a105c2191 netlink: don't require a callback argument in nl_recvmsgs() 2019-01-09 16:46:41 +01:00
Thomas Haller
0713a6996c platform: track wireguard endpoint as sockaddr struct
We need to track the IPv6 endpoint as struct sockaddr_in6, so that
we have access to the scope-id.
2019-01-09 16:46:41 +01:00
Thomas Haller
2e48f6419f netlink: don't heap allocate struct ucred during nla_recv()
Instead, fill a preallocated output buffer provided by the caller.
2019-01-09 16:46:41 +01:00
Thomas Haller
e5fb1dd6e7 netlink: track ucred via a boolean field in struct nl_msg
Also, accept NULL in nlmsg_set_creds() to clear the set credentials.
2019-01-09 16:46:41 +01:00
Thomas Haller
fce3243f12 shared/trivial: rename nm_utils_mem_all_zero() to nm_utils_memeqzero()
in systemd/systemd, systemd/casync, and rustyrussel/ccan (github) this
function is called "memeqzero()". Rename, to use a more popular name.
2019-01-09 16:46:41 +01:00
Thomas Haller
aab3e14883 shared: add nm_utils_getpagesize() and use it in netlink code
Since we already cached the result of getpagesize() in a static variable (at
two places), move the code to nm-shared-utils, so it is reusable.

Also, use sysconf() instead of getpagesize(), like suggested by `man
getpagesize`.
2019-01-09 16:46:41 +01:00
Thomas Haller
1cd167c774 platform: fix hash/cmp for NMPObjectLnkWireGuard
It's totally bogus.

Luckily, NMPlatformLnkVlan is smaller than NMPlatformLnkWireGuard, hence there
was no crash, just wrong results.

Fixes: 0827d4c2e4
2019-01-09 16:46:41 +01:00
Thomas Haller
2ddfa5b265 platform: fix nm_platform_wireguard_peer_to_string()
Fixes: 62d14e1884
2018-12-30 15:17:11 +01:00
Thomas Haller
9096b5572d platform: use nm_steal_fd() in nmp_utils_sysctl_open_netdir() 2018-12-27 21:33:59 +01:00