Commit Graph

310 Commits

Author SHA1 Message Date
Thomas Haller
70d8f5ddfe libnm-core: avoid cloning attributes of NMTCQdisc/NMTCAction to convert to string
(cherry picked from commit 48316f987a)
2019-04-20 08:56:27 +02:00
Thomas Haller
284ac92eee shared: build helper "libnm-libnm-core-{intern|aux}.la" library for libnm-core
"libnm-core" implements common functionality for "NetworkManager" and
"libnm".

Note that clients like "nmcli" cannot access the internal API provided
by "libnm-core". So, if nmcli wants to do something that is also done by
"libnm-core", , "libnm", or "NetworkManager", the code would have to be
duplicated.

Instead, such code can be in "libnm-libnm-core-{intern|aux}.la".
Note that:

  0) "libnm-libnm-core-intern.la" is used by libnm-core itsself.
     On the other hand, "libnm-libnm-core-aux.la" is not used by
     libnm-core, but provides utilities on top of it.

  1) they both extend "libnm-core" with utlities that are not public
     API of libnm itself. Maybe part of the code should one day become
     public API of libnm. On the other hand, this is code for which
     we may not want to commit to a stable interface or which we
     don't want to provide as part of the API.

  2) "libnm-libnm-core-intern.la" is statically linked by "libnm-core"
     and thus directly available to "libnm" and "NetworkManager".
     On the other hand, "libnm-libnm-core-aux.la" may be used by "libnm"
     and "NetworkManager".
     Both libraries may be statically linked by libnm clients (like
     nmcli).

  3) it must only use glib, libnm-glib-aux.la, and the public API
     of libnm-core.
     This is important: it must not use "libnm-core/nm-core-internal.h"
     nor "libnm-core/nm-utils-private.h" so the static library is usable
     by nmcli which couldn't access these.

Note that "shared/nm-meta-setting.c" is an entirely different case,
because it behaves differently depending on whether linking against
"libnm-core" or the client programs. As such, this file must be compiled
twice.

(cherry picked from commit af07ed01c0)
2019-04-18 20:07:44 +02:00
Thomas Haller
d984b2ce4a shared: move most of "shared/nm-utils" to "shared/nm-glib-aux"
From the files under "shared/nm-utils" we build an internal library
that provides glib-based helper utilities.

Move the files of that basic library to a new subdirectory
"shared/nm-glib-aux" and rename the helper library "libnm-core-base.la"
to "libnm-glib-aux.la".

Reasons:

 - the name "utils" is overused in our code-base. Everything's an
   "utils". Give this thing a more distinct name.

 - there were additional files under "shared/nm-utils", which are not
   part of this internal library "libnm-utils-base.la". All the files
   that are part of this library should be together in the same
   directory, but files that are not, should not be there.

 - the new name should better convey what this library is and what is isn't:
   it's a set of utilities and helper functions that extend glib with
   funcitonality that we commonly need.

There are still some files left under "shared/nm-utils". They have less
a unifying propose to be in their own directory, so I leave them there
for now. But at least they are separate from "shared/nm-glib-aux",
which has a very clear purpose.

(cherry picked from commit 80db06f768)
2019-04-18 19:57:27 +02:00
Thomas Haller
05a547133b libnm: minor refactoring of _nm_utils_bridge_vlan_verify_list()
- if there is only one vlan in the list, then we can return success
  early. That is, because one NMBridgeVlan instance is always valid
  due to the way how users must use the API to construct the element.

- the implementation for check_normalizable is only correct, if there
  are no duplicate or overlapping ranges. Assert for that. In fact,
  all callers first check for errors and then for normalizable errors.

- avoid duplicate calls to nm_bridge_vlan_get_vid_range(). There are
  duplicate assertions that we don't need.

- only check for pvid once per range.

- combine calls to g_hash_table_contains() and g_hash_table_add().

(cherry picked from commit a358da096f)
2019-04-18 09:53:24 +02:00
Beniamino Galvani
da204257b1 all: support bridge vlan ranges
In some cases it is convenient to specify ranges of bridge vlans, as
already supported by iproute2 and natively by kernel. With this commit
it becomes possible to add a range in this way:

 nmcli connection modify eth0-slave +bridge-port.vlans "100-200 untagged"

vlan ranges can't be PVIDs because only one PVID vlan can exist.

https://bugzilla.redhat.com/show_bug.cgi?id=1652910
(cherry picked from commit 7093515777)
2019-04-18 09:53:18 +02:00
Lubomir Rintel
65831ee84c core/qdisc: add support for attributes 2019-04-12 18:04:04 +02:00
Lubomir Rintel
f5f90a0d69 core/qdisc: drop useless code
The call to nm_utils_parse_variant_attributes() is useless. The following
_tc_read_common_opts() call does the same thing. This was probably left
in place by accident.
2019-04-12 18:03:09 +02:00
Thomas Haller
da4f229805 libnm,shared: bzero secrets on failure in nm_utils_base64secret_decode()
Now that unbase64mem_full() understands a secure flag, we can
get this right.
2019-04-12 07:39:50 +02:00
Thomas Haller
84f2037648 shared: add flags argument to nm_utils_strsplit_set()
It will be useful to extend nm_utils_strsplit_set() with various
flavors and subtly different behaviors. Add a flags argument to
support these.
2019-04-10 15:05:57 +02:00
Beniamino Galvani
80a3031a7c libnm-core: fix invalid memory access
When we delete the runner.name property, the runner object itself gets
deleted if that was the only property, and @runner becomes invalid.

 ==13818== Invalid read of size 1
 ==13818==    at 0x55EAF4: nm_streq (nm-macros-internal.h:869)
 ==13818==    by 0x55EAF4: _json_team_normalize_defaults (nm-utils.c:5573)
 ==13818==    by 0x566C89: _nm_utils_team_config_set (nm-utils.c:6057)
 ==13818==    by 0x5498A6: _nm_utils_json_append_gvalue (nm-utils-private.h:228)
 ==13818==    by 0x5498A6: set_property (nm-setting-team.c:1622)
 ==13818==  Address 0x182a9330 is 0 bytes inside a block of size 13 free'd
 ==13818==    at 0x4839A0C: free (vg_replace_malloc.c:530)
 ==13818==    by 0x4857868: json_delete_string (value.c:763)
 ==13818==    by 0x4857868: json_delete (value.c:975)
 ==13818==    by 0x4851FA1: UnknownInlinedFun (jansson.h:129)
 ==13818==    by 0x4851FA1: hashtable_do_del (hashtable.c:131)
 ==13818==    by 0x4851FA1: hashtable_del (hashtable.c:289)
 ==13818==    by 0x55DFDD: _json_del_object (nm-utils.c:5384)
 ==13818==    by 0x55EA70: _json_delete_object_on_string_match (nm-utils.c:5532)
 ==13818==    by 0x55EADB: _json_team_normalize_defaults (nm-utils.c:5549)
 ==13818==    by 0x566C89: _nm_utils_team_config_set (nm-utils.c:6057)
 ==13818==    by 0x5498A6: _nm_utils_json_append_gvalue (nm-utils-private.h:228)
 ==13818==    by 0x5498A6: set_property (nm-setting-team.c:1622)
 ==13818==  Block was alloc'd at
 ==13818==    at 0x483880B: malloc (vg_replace_malloc.c:299)
 ==13818==    by 0x4852E8C: lex_scan_string (load.c:389)
 ==13818==    by 0x4852E8C: lex_scan (load.c:620)
 ==13818==    by 0x4853458: parse_object (load.c:738)
 ==13818==    by 0x4853458: parse_value (load.c:862)
 ==13818==    by 0x4853466: parse_object (load.c:739)
 ==13818==    by 0x4853466: parse_value (load.c:862)
 ==13818==    by 0x4853655: parse_json.constprop.7 (load.c:899)
 ==13818==    by 0x48537CF: json_loads (load.c:959)
 ==13818==    by 0x566780: _nm_utils_team_config_set (nm-utils.c:5961)
 ==13818==    by 0x5498A6: _nm_utils_json_append_gvalue (nm-utils-private.h:228)
 ==13818==    by 0x5498A6: set_property (nm-setting-team.c:1622)

Fixes: a5642fd93a ('libnm-core: team: rework defaults management on runner properties')
2019-04-08 18:49:27 +02:00
Francesco Giudici
a0d1971c23 libnm-core: make compiler happy
../libnm-core/nm-utils.c:6784:30: error: unused variable 'var_unref' [-Werror,-Wunused-variable]
                gs_unref_variant GVariant *var_unref = vlan_var;
2019-03-29 12:16:37 +01:00
Thomas Haller
d0727509b2 libnm: add nm_utils_is_valid_iface_name_utf8safe() util 2019-03-27 16:23:30 +01:00
Beniamino Galvani
cf71860183 libnm-core: add bridge vlan conversion utils 2019-03-26 17:18:29 +01:00
Beniamino Galvani
1e5b0788bc libnm-core: add vlans property to bridge-port setting 2019-03-26 17:18:29 +01:00
Beniamino Galvani
2691fd279d libnm-core: fix typos in API documentation 2019-03-26 17:18:29 +01:00
Thomas Haller
65c8c19fa7 cli: don't support removing sriov vfs by index
The VFs already can be parsed as plain number (to indicate the
ifindex). We should not also support accepting the plain number
as index to be removed.

Fixes: a2f12994b7 ('cli: add support for configuring SR-IOV')
2019-03-25 09:12:33 +01:00
Thomas Haller
2a93ce9122 libnm/team: preserve unset priv->runner_tx_hash in _align_team_properties()
There is a difference between an unset JSON array element and an empty
one. Don't loose that information.
2019-03-24 09:21:14 +01:00
Thomas Haller
265864952d libnm/team: simplify handling in _nm_utils_team_config_set()
- use nm_auto_decref_json for "json_value" to indicate ownership
  transfer.
- don't reuse variable json_element and json_link to construct
  watchers list. It's confusing. In general, use different variables
  for different purposes.
2019-03-24 09:21:14 +01:00
Thomas Haller
0cff18e055 libnm/team: indicate whether there were changes in _nm_utils_team_config_set() 2019-03-24 09:21:13 +01:00
Thomas Haller
f84323bf47 libnm/team: return early from _nm_utils_team_config_set() 2019-03-24 09:21:13 +01:00
Thomas Haller
1283d9d321 libnm/team: minor coding style cleanup 2019-03-24 09:21:13 +01:00
Thomas Haller
acf1cf61cf shared: add _nm_utils_strv_cmp_n() and _nm_utils_strv_equal() 2019-03-24 09:21:13 +01:00
Thomas Haller
0d178a968d libnm: rename and expose nm_utils_base64secret_decode() in libnm
A NetworkManager client requires an API to validate and decode
a base64 secret -- like it is used by WireGuard. If we don't have
this as part of the API, it's inconvenient. Expose it.

Rename it from _nm_utils_wireguard_decode_key(), to give it a more
general name.

Also, rename _nm_utils_wireguard_normalize_key() to
nm_utils_base64secret_normalize(). But this one we keep as internal
API. The user will care more about validating and decoding the base64
key. To convert the key back to base64, we don't need a public API in
libnm.

This is another ABI change since 1.16-rc1.

(cherry picked from commit e46ba01867)
2019-03-07 22:22:39 +01:00
Thomas Haller
b680d64b47 wireguard: accept all-zero private-key, public-key and preshared-key
- For PSK, an all-zero PSK means to don't do symmetric encryption. As such,
  at first it seems a bit odd when the user sets

      - preshared-key-flags != "4 (not-required)"

      - preshared-key = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

  Here the user indicates that a PSK is required, but then provides an
  all-zero PSK that effectively disables it. Still, we should not reject
  such a configuration. This has the benefit that it allos the user for
  being prompted for a PSK, only to disable it by entering the all-zero key.

- For the private-key (and consequently the public-key), "public-key-flags=4"
  is rejected by libnm. A private key is always required for NetworkManager to
  configure the link. However, let's not care for all-zero keys either. If the user
  configures that, we just set that key. It's a valid setting as far as WireGuard
  (the kernel module) is concerned, so we shouldn't reject it.

(cherry picked from commit 78dccb8bb9)
2019-03-07 22:22:39 +01:00
Marco Trevisan (Treviño)
b5bbf8edc2 nm: Fix syntax on introspection annotations
Various annotations were added using multiple colons, while only one has
to be added or g-ir-introspect will consider them part of the description

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/94
(cherry picked from commit 73005fcf5b)
2019-03-07 10:09:23 +01:00
Thomas Haller
53b747fff5 all: move nm_utils_hexstr2bin*() to shared
libnm exposes simplified variants of hexstr2bin in its public API. I
think that was a mistake, because libnm should provide NetworkManager
specific utils. It should not provide such string functions.

However, nmcli used to need this, so it was added to libnm.

The better approach is to add it to our internally shared static
library, so that all interested components can make use of it.
2019-02-22 14:04:13 +01:00
Thomas Haller
b521f426ab libnm,cli: add NMSettingWireGuard
For now only add the core settings, no peers' data.

To support peers and the allowed-ips of the peers is more complicated
and will be done later. It's more complicated because these are nested
lists (allowed-ips) inside a list (peers). That is quite unusual and to
conveniently support that in D-Bus API, in keyfile format, in libnm,
and nmcli, is a effort.
Also, it's further complicated by the fact that each peer has a secret (the
preshared-key). Thus we probably need secret flags for each peer, which
is a novelty as well (until now we require a fixed set of secrets per
profile that is well known).
2019-02-22 11:00:10 +01:00
Beniamino Galvani
b5efcf08f4 all: move nm_utils_bin2hexstr_full() to shared
reuse++
2019-02-21 09:36:17 +01:00
Thomas Haller
713e879d76 libnm: add NMSockAddrEndpoint API
NMSockAddrEndpoint is an immutable structure that contains the endpoint
string of a service. It also includes the (naive) parsing of the host and
port/service parts.

This will be used for the endpoint of WireGuard's peers. But since endpoints
are not something specific to WireGuard, give it a general name (and
purpose) independent from WireGuard.

Essentially, this structure takes a string in a manner that libnm
understands, and uses it for node and service arguments for
getaddrinfo().

NMSockAddrEndpoint allows to have endpoints that are not parsable into
a host and port part. That is useful because our settings need to be
able to hold invalid values. That is for forward compatibility (server
sends a new endpoint format) and for better error handling (have
invalid settings that can be constructed without loss, but fail later
during the NMSetting:verify() step).
2019-02-14 08:00:29 +01:00
Thomas Haller
d93845e2c2 libnm: add internal _nm_utils_secret_flags_validate() util 2019-02-14 08:00:29 +01:00
Thomas Haller
d216e2f305 libnm: fix usage of nm_streq() macro
Yes, C has a preprocessor and nm_streq() currently is a macro.

Still, macros should very much behave like regular functions.
For example, no unexpected side-effects aside what a regular function
would have, evaluating all arguments exactly once, or no side-effects
w.r.t. the order in which arguments are evaluated.

In some cases, we deviate from that for good reasons. For example
NM_IN_SET() may not evaluate all arguments. _LOGD() may not evaluate
any arguments, and NM_UTILS_LOOKUP_STR_DEFINE() is not a function-like
macro at all.

Still, that is not the case here. We avoid to misuse macros to write
code that does not look like C.
2019-02-13 16:03:23 +01:00
Thomas Haller
9beed4f661 all: replace strerror() calls with nm_strerror_native() 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
814bcf5575 libnm: avoid "-Wmissing-braces" warning for intializing NMUuid 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
efbcac25b8 libnm-core: fix and cleanup validation of WPS-method for Wi-Fi P2P setting
The check for Wi-Fi P2P's wps-method was not correct.

While at it, move the logic to validate WPS-method flags in an utility
function.
2019-02-01 17:02:57 +01:00
Thomas Haller
e8bf89a906 libnm: pass serialization flags to settings synth_func()
We will need access to the serialization flags from within the synth_func().

That will be for WireGuard's peers. Peers are a list of complex, structured
elements, and some fields (the peer's preshared-key) are secret and
others are not. So when serializing the peers, we need to know whether
to include secrets or not.

Instead of letting _nm_setting_to_dbus() check the flags, pass them
down.

While at it, don't pass the property_name argument. Instead, pass the
entire meta-data information we have. Most synth functions don't care
about the property or the name either way. But we should not pre-filter
information that we have at hand. Just pass it to the synth function.
If the synth function would be public API, that would be a reason to be
careful about what we pass. But it isn't and it only has one caller.
So passing it along is fine. Also, do it now when adding the flags
argument, as we touch all synth implementations anyway.
2019-01-07 10:09:10 +01:00
Thomas Haller
b9ebaf3a2b libnm: discourage static buffer in nm_utils_inet*_ntop() API
nm_utils_inet[46]_ntop() are public API of libnm, so we cannot change
it. However, discourage the use of the static buffer.
2018-12-19 09:23:08 +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
e442e3881e core: implement nm_utils_ip4_netmask_to_prefix() via __builtin_ctz()
Taken from systemd's in4_addr_netmask_to_prefixlen().

Yes, this adds the requirement that "int" is 32 bits. But systemd
already has the same requirement in u32ctz(), hence we anyway cannot
build on other architectures. If that is ever necessary, it's easy
to adjust.
2018-12-19 09:23:08 +01:00
Beniamino Galvani
769e0726a8 cli: strictly validate SR-IOV attributes
Report an error when the user tries to add an unknown attribute
instead of silently accepting (and ignoring) it.

Note that this commit also changes the behavior of public API
nm_utils_sriov_vf_from_str() to return an error when an unknown
attribute is found. I think the previous behavior was buggy as wrong
attributes were simply ignored without any way for the user to know.

Fixes: a9b4532fa7
2018-12-12 14:18:53 +01:00
Patrick Talbert
f44c793f6c team: add support for 'vlanid' link-watchers property
Add support for the teaming arp_ping link watcher 'vlanid' property.

Signed-off-by: Patrick Talbert <ptalbert@redhat.com>

[thaller@redhat.com: minor fixes to original patch]

https://bugzilla.redhat.com/show_bug.cgi?id=1652931
2018-12-12 14:07:30 +01:00
Thomas Haller
140a5e3316 all: make use of NM_MAKE_STRV() macro 2018-12-01 15:16:48 +01:00
Thomas Haller
eb9f950a33 all: cleanup GChecksum handling
- prefer nm_auto_free_checksum over explicit free.
- use nm_utils_checksum_get_digest*().
- prefer defines for digest length.
- assume g_checksum_new() cannot fail.
2018-11-13 18:30:03 +01:00
Thomas Haller
01239e99d7 libnm: add nm_utils_uuid_is_null() helper 2018-10-31 11:34:31 +01:00
Thomas Haller
4db431191c libnm: add nm_utils_uuid_generate_from_string_bin() function 2018-10-31 11:34:31 +01:00
Thomas Haller
070a4d9355 libnm: add support for SHA1 based version 5 UUIDs
The entire point of using version 3/5 UUIDs is to generate
stable UUIDs based on a string. It's usually important that
we don't change the UUID generation algorithm later on.

Since we didn't have a version 5 implementation, we would always
resort to the MD5 based version 3. Version 5 is recommended by RFC 4122:

   o  Choose either MD5 [4] or SHA-1 [8] as the hash algorithm; If
      backward compatibility is not an issue, SHA-1 is preferred.

Add a version 5 implementation so we can use it in the future.

All test values are generated with python's uuid module or OSSP uuid.
2018-10-31 11:34:31 +01:00
Thomas Haller
c150b0fa29 libnm/trivial: rename uuid type VARIANT3 to VERSION3
In RFC 4122, this is called "version 3", not "variant 3". While for
UUIDs there is also a concept of "variants", that is something else.

Fix naming.
2018-10-31 09:41:12 +01:00
Thomas Haller
88b081fce4 libnm: expose UUID utils as internal API
We link against libuuid.so, but it was entirely internal to
libnm-core. We only exposed UUIDs in string form.

Add API to also handle UUIDs in binary form.

Note that libuuid already defines a type "uuid_t". However,
don't use it and instead use our own typedef NMUuid.
Reasons:

  - uuid.h should be internal to libnm-core (nm-utils.c specifically),
    and not be used by or exposed it other parts of the code.

  - uuid_t is a typedef for a guchar[16] array. Typedefs
    for arrays are confusing, because depending on whether
    it's an automatic variable or a pointer in a function argument,
    they behave differently regarding whether to take their address
    or not and usage of "sizeof()".
2018-10-31 09:41:12 +01:00
Beniamino Galvani
9b04b871a0 libnm-core: fix coverity warning
3. NetworkManager-1.14.0/libnm-core/nm-utils.c:4944: var_compare_op: Comparing "str" to null implies that "str" might be null.
 4. NetworkManager-1.14.0/libnm-core/nm-utils.c:4958: var_deref_op: Dereferencing null pointer "str".
 #  4956|
 #  4957|   	/* do some very basic validation to see if this might be a JSON object. */
 #  4958|-> 	if (str[0] == '{') {
 #  4959|   		gsize l;
 #  4960|
2018-10-06 10:03:48 +02:00