Commit graph

1423 commits

Author SHA1 Message Date
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
Thomas Haller
691e5d5cc9 platform: return platform-error from link-add function
We need more information what failed. Don't only return success/failure,
but an error number.

Note that we still don't actually return an error number. Only
the link_add() function is changed to return an nm-error integer.
2018-12-27 21:33:59 +01:00
Thomas Haller
d18f40320d platform: merge NMPlatformError with nm-error
Platform had it's own scheme for reporting errors: NMPlatformError.
Before, NMPlatformError indicated success via zero, negative integer
values are numbers from <errno.h>, and positive integer values are
platform specific codes. This changes now according to nm-error:
success is still zero. Negative values indicate a failure, where the
numeric value is either from <errno.h> or one of our error codes.
The meaning of positive values depends on the functions. Most functions
can only report an error reason (negative) and success (zero). For such
functions, positive values should never be returned (but the caller
should anticipate them).
For some functions, positive values could mean additional information
(but still success). That depends.

This is also what systemd does, except that systemd only returns
(negative) integers from <errno.h>, while we merge our own error codes
into the range of <errno.h>.

The advantage is to get rid of one way how to signal errors. The other
advantage is, that these error codes are compatible with all other
nm-errno values. For example, previously negative values indicated error
codes from <errno.h>, but it did not entail error codes from netlink.
2018-12-27 21:33:59 +01:00
Thomas Haller
5326100001 trivial: rename nl-errno to nm-errno 2018-12-27 21:33:59 +01:00
Thomas Haller
4fe18e5bdf core: move netlink errors to nm-errno.h
No other changes (yet).
2018-12-27 21:33:59 +01:00
Iñigo Martínez
35171b3c3f build: meson: Add trailing commas
Add missing trailing commas that avoids getting noise when another
file/parameter is added and eases reviewing changes[0].

[0] https://gitlab.gnome.org/GNOME/dconf/merge_requests/11#note_291585
2018-12-20 13:50:34 +01:00
Thomas Haller
a51c09dc12 all: don't use static buffer for nm_utils_inet*_ntop()
While nm_utils_inet*_ntop() accepts a %NULL buffer to fallback
to a static buffer, don't do that.

I find the possibility of using a static buffer here error prone
and something that should be avoided. There is of course the downside,
that in some cases it requires an additional line of code to allocate
the buffer on the stack as auto-variable.
2018-12-19 09:23:08 +01:00
Thomas Haller
91b5babff2 core/trivial: rename nm_platform_sysctl_set_ip6_hop_limit_safe()
Now that we have other helper function on platfrom for setting
IP configuration sysctls, rename the function to set the hop-limit
to match the pattern.
2018-12-19 09:05:12 +01:00
Thomas Haller
7fa398d596 platform: add nm_platform_sysctl_ip_conf_*() wrappers 2018-12-19 09:05:12 +01:00
Thomas Haller
8b9fd01ef3 platform: use struct initializer instead of memset()
I think this is preferred over memset(), because it allows the
compiler to better unstand what is happening.

Also, strictly speaking in the C language, %NULL pointers are not
guaranteed to have an all zero bit pattern. Of course, that is already
required on any architecture where NetworkManager is running.
2018-12-19 08:58:12 +01:00
Thomas Haller
67f02b2a14 platform: assert length of stack allocation in NMP_SYSCTL_PATHID_NETDIR_unsafe()
NMP_SYSCTL_PATHID_NETDIR_unsafe() uses alloca() to allocate the string.
Assert that the "path" argument is reasonably short.

In practice, that is of course the case, because there are only 2 callers
which take care not to pass an untrusted, unbounded path argument.
2018-12-19 08:56:51 +01:00
Beniamino Galvani
1e41495d9a platform: sriov: write new values when we can't read old ones
Fixes: 7df3333879
2018-12-12 14:38:18 +01:00
Beniamino Galvani
e01a7c1154 core: use NMTernary for SR-IOV autoprobe-drivers 2018-12-12 14:38:18 +01:00
Thomas Haller
f94142284d platform: don't consult cache before invoking netlink operation
Checking whether the link exists in the cache, before talking to kernel
serves no purpose.

- in all cases, the caller already has a good indication that the link
  in fact exists. That is, because the caller makes decisions on what to
  do, based on what platform told it earlier. Thus, the check usually succeeds
  anyway.

- in the unexpected case it doesn't succeed, we

  - should not silently return without logging at least a message

  - we possibly still want to send the netlink message to kernel,
    just to have it fail. Note that the ifindex is indeed the identifier
    for the link, so there is no danger of accidentally killing the
    wrong link.
    Well, theoretically there is, because the kernel's ifindex counter can
    wrap or get reused when moving links between namespaces. But checking
    the cache would not protect against that anyway! Worst case, the cache
    would already have the impostor link and would not prevent from doing
    the wrong thing. After all, they do have the same identifier, so how
    would we know that this is in fact a different link?
2018-12-03 12:26:16 +01:00
Thomas Haller
945c904f95 platform: assert against valid ifindex and remove duplicate assertions
We want that all code paths assert strictly and gracefully.

That means, if we have function nm_platform_link_get() which calls
nm_platform_link_get_obj(), then we don't need to assert the same things
twice. Don't have the calling function assert itself, if it is obvious
that the first thing that it does, is calling a function that itself
asserts the same conditions.

On the other hand, it simply indicates a bug passing a non-positive
ifindex to any of these platform functions. No longer let
nm_platform_link_get_obj() handle negative ifindex gracefully. Instead,
let it directly pass it to nmp_cache_lookup_link(), which eventually
does a g_return_val_if_fail() check. This quite possible enables
assertions on a lot of code paths. But note that g_return_val_if_fail()
is graceful and does not lead to a crash (unless G_DEBUG=fatal-criticals
is set for debugging).
2018-12-03 12:26:16 +01:00
Thomas Haller
da39a0ada3 platform/tests: improve nmtstp_link_delete() for deleting links
nm_platform_link_delete() will soon assert against positive ifindex
argument.

    nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, DEVICE_NAME));

will result in an assertion, if the link does not exist.

Extend nmtstp_link_delete() to gracefully skip deleting the link
so that it can be used in such situations.

Also, rename nmtstp_link_del() to nmtstp_link_delete(), because it's
closer to nm_platform_link_delete().
2018-12-03 12:26:16 +01:00
Thomas Haller
1c7b747f8c platform: move assertion from nm_platform_link_get() to nm_platform_link_get_obj()
We want to assert for valid input arguments, but we don't want
multiple assertions for the same.

Move the assertion from nm_platform_link_get() to
nm_platform_link_get_obj().

That way, nm_platform_link_get_obj() also checks the input arguments.
At the same time, nm_platform_link_get() gets simpler and still does
the same amount of assertions.
2018-12-03 12:26:16 +01:00
Thomas Haller
f47f9e3956 platform: let nmp_cache_lookup_link_full() prefer visible links
In nmp_cache_lookup_link_full(), we may have multiple candidates that match.
Continue searching, until we find a visible one. That way, visible results
are preferred.

Note that for links, nmp_object_is_visible() checks whether the link is
visible in netlink (instead of only udev).
2018-12-03 12:26:16 +01:00
Lubomir Rintel
b385ad0159 all: say Wi-Fi instead of "wifi" or "WiFi"
Correct the spelling across the *entire* tree, including translations,
comments, etc. It's easier that way.

Even the places where it's not exposed to the user, such as tests, so
that we learn how is it spelled correctly.
2018-11-29 17:53:35 +01:00
Lubomir Rintel
64b95d567b wifi/wext: fix double quoting
_nm_utils_ssid_to_string_arr() already escapes/quotes the string.
2018-11-29 17:50:00 +01:00
Thomas Haller
b445b1f8fe platform: add nm_platform_link_get_ifi_flags() helper
Add helper nm_platform_link_get_ifi_flags() to access the
ifi-flags.

This replaces the internal API _link_get_flags() and makes it public.
However, the return value also allows to distinguish between errors
and valid flags.

Also, consider non-visible links. These are links that are in netlink,
but not visible in udev. The ifi-flags are inherrently netlink specific,
so it seems wrong to pretend that the link doesn't exist.
2018-11-29 13:50:10 +01:00
Thomas Haller
e180464bcc platform/tests: fix assertion for unit test for address lifetime
Sometimes the test fail:

    $ make -j 10 src/platform/tests/test-address-linux
    $ while true; do
          NMTST_DEBUG=d ./tools/run-nm-test.sh src/platform/tests/test-address-linux 2>&1 > log.txt || break;
      done

fails with:

    ERROR: src/platform/tests/test-address-linux - Bail out! test:ERROR:src/platform/tests/test-common.c:790:nmtstp_ip_address_assert_lifetime: assertion failed (adr <= lft): (1001 <= 1000)

That is, because of a wrong check. Fix it.
2018-11-28 16:13:04 +01:00
Thomas Haller
37e47fbdab build: avoid header conflict for <linux/if.h> and <net/if.h> with "nm-platform.h"
In the past, the headers "linux/if.h" and "net/if.h" were incompatible.
That means, we can either include one or the other, but not both.
This is fixed in the meantime, however the issue still exists when
building against older kernel/glibc.

That means, including one of these headers from a header file
is problematic. In particular if it's a header like "nm-platform.h",
which itself is dragged in by many other headers.

Avoid that by not including these headers from "platform.h", but instead
from the source files where needed (or possibly from less popular header
files).

Currently there is no problem. However, this allows an unknowing user to
include <net/if.h> at the same time with "nm-platform.h", which is easy
to get wrong.
2018-11-12 16:02:35 +01:00
Thomas Haller
8d6d7c48f9 core/trivial: fix whitespace 2018-10-22 14:03:11 +02:00
Thomas Haller
948abdb84d platform/tests: extend timeout for link-linux tests with meson
Our gitlab CI sometimes takes a long time with the
"/link/create-many-links/1000" test.
2018-10-22 13:42:20 +02:00
Jan Alexander Steffens (heftig)
e0b168d6a8 meson: Fix platform tests
All platform tests were run twice with the `linux` platform, instead of
`fake` and `linux`, as expected.
2018-10-22 13:19:15 +02:00
Thomas Haller
581be6b8d2 platform/tests: fix test-nmp-object when running on system without udev
Fix the test, to check that the nmp-object was deleted. It is
no longer visible and no longer alive.
2018-10-22 13:19:15 +02:00
Thomas Haller
cfc0565604 platform/tests: don't compare dangling pointer in "test-nmp-object.c"
This wouldn't even dereference the dangling pointer, but
merely comparing it for pointer equality. Still, it's actually
undefined behavior. Avoid it.
2018-10-22 13:17:53 +02:00
Thomas Haller
c295d45a3b platform/netlink: fix overrun in attribute iteration in nla_ok()
See-also: 123dc07bcc
See-also: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1045b03e07d85f3545118510a587035536030c1c
2018-10-10 12:04:27 +02:00
Lubomir Rintel
0573656eeb platform/wpan: allow setting channel 2018-10-07 15:46:02 +02:00
Lubomir Rintel
8f107f5c00 platform: bring back the interface name to log messages
Fixes: ecf607cce6
2018-10-01 11:45:57 +02:00
Lubomir Rintel
8aa3e6de5c wifi-utils: remove log domain argument
Makes Thomas happy.
2018-10-01 10:26:06 +02:00
Lubomir Rintel
74ce1e963e wifi-utils: rename nl80211 to self
Makes Thomas happy.
2018-10-01 10:26:05 +02:00
Lubomir Rintel
2346210c11 wifi-utils: downgrade a log message
This is pretty much of no use to the user and clobbers the log.
2018-10-01 10:26:05 +02:00
Lubomir Rintel
ecf607cce6 platform: log the interface names 2018-10-01 10:26:05 +02:00
Lubomir Rintel
03e3651794 wifi: include the interface name in logs 2018-10-01 10:26:05 +02:00