Commit Graph

45 Commits

Author SHA1 Message Date
Thomas Haller
70c2478f89 libnm: change NMVariantAttributeSpec.str_type to work for attributes of any type
First of all, all of NMVariantAttributeSpec is internal API. We only
expose the typedef itself as public API, but not its fields nor
their meaning. So we can change things.

Change "str_type" to "type_detail", so that it can work for any kind of
attribute, not only for strings. Usually, we want to avoid special
cases and treat all attributes the same, based on their GVariant type.
But sometimes, it is necessary to do something special with an
attribute. This is what the "type_detail" encodes, but it's not only
relevant for strings.

(cherry picked from commit 6f277d8fa6)
2022-02-10 08:41:16 +01:00
Thomas Haller
11d4d244dd libnm: avoid parsing IP addresses twice in NMIPAddress/NMIPRoute API
Usually the normalization (canonicalize) and validation of the IP
address string both requires to parse the string. As we always do
validation first, we can use the parsed address and don't need to parse
it a second time.

(cherry picked from commit 00e4f21629)
2022-02-10 08:41:16 +01:00
Thomas Haller
beca3a4d46 libnm: reorder fields in NMIPAddress/NMIPRoute struct
Order the fields by their size, to minimize the alignment gaps.
I guess, that doesn't matter because the alignment of the heap
allocation is larger than what we can safe here. Still, there is
on reason to do it any other way.

Also, it's not possible via API to set family/prefix to values outside
their range, so an 8bit integer is always sufficient. And we don't want
that invariant to change. We don't ever want to allow the caller to set
values that are clearly invalid, and will assert against that early (g_return()).
Point is, we can do this and there is no danger of future problems.
And even if we will support larger values, it's all an implementation
detail anyway.

(cherry picked from commit 6208a1bb84)
2022-02-10 08:41:15 +01:00
Thomas Haller
39c308f370 libnm: cleanup redundant code for direct properties of NMSetting 2022-01-18 16:22:32 +01:00
Thomas Haller
1d4a80cf7f libnm: refactor some NMSetting to use direct properties for int64 2022-01-18 16:22:30 +01:00
Thomas Haller
20d6793065 libnm: refactor some NMSetting to use direct properties for uint32 2022-01-18 16:22:25 +01:00
Thomas Haller
208df83491 libnm: refactor some NMSetting to use direct properties for int32 2022-01-18 16:22:24 +01:00
Thomas Haller
5e7400c832 libnm: add flag to map zero to NULL in _nm_utils_ipaddr_canonical_or_invalid()
This seems a questionable thing to do, and should be made clearer by
having a parameter (that makes you think about what is happening here).

Also, the normalization for vxlan.remote does not perform this mapping,
so the parameter is there so that the approach can handle both flavors.
2022-01-18 16:22:17 +01:00
Thomas Haller
1f58244268 libnm: let direct string property support AF_UNSPEC for normalizing IP addresses 2022-01-18 16:22:16 +01:00
Thomas Haller
615221a99c format: reformat source tree with clang-format 13.0
We use clang-format for automatic formatting of our source files.
Since clang-format is actively maintained software, the actual
formatting depends on the used version of clang-format. That is
unfortunate and painful, but really unavoidable unless clang-format
would be strictly bug-compatible.

So the version that we must use is from the current Fedora release, which
is also tested by our gitlab-ci. Previously, we were using Fedora 34 with
clang-tools-extra-12.0.1-1.fc34.x86_64.

As Fedora 35 comes along, we need to update our formatting as Fedora 35
comes with version "13.0.0~rc1-1.fc35".
An alternative would be to freeze on version 12, but that has different
problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it
would be cumbersome for our developers which are on Fedora 35 to use a
clang that they cannot easily install).

The (differently painful) solution is to reformat from time to time, as we
switch to a new Fedora (and thus clang) version.
Usually we would expect that such a reformatting brings minor changes.
But this time, the changes are huge. That is mentioned in the release
notes [1] as

  Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353)

[1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
2021-11-29 09:31:09 +00:00
Thomas Haller
989a6911ba libnm: always finalize direct properties in NMSetting base class
Certain properties need to release memory when destroying the NMSetting.
For "direct" properties, we have all the information we need to do that
generically in the NMSetting base class. In practice, this only concerns
string properties.

See _finalize_direct() in "nm-setting.c".

However, if the NMSetting base class takes care of freeing the strings,
then the subclasses must not also unref the variable (to avoid double free).
Previously, subclasses had to opt-in for the base class to indicate that
they are fine with that.

Now, let the base class always handle it. We only need to make sure that
classes that implement direct string properties don't also try to free
the values during destruction.
2021-11-04 20:25:19 +01:00
Vojtech Bubela
0ed099374d libnm: fix crash in _nm_ip_route_validate_all for invalid route
backtrace from coredump, NetworkManager-1.30.6-1.fc34

  #0  verify
      (setting=0x55d081fe8690, connection=<optimized out>, error=0x7ffe0fa06870)
      at libnm-core/nm-setting-ip-config.c:5249
  #1  0x000055d081ab98d4 in verify
      (setting=0x55d081fe8690, connection=0x55d0820a2b80, error=0x7ffe0fa06870)
      at libnm-core/nm-setting-ip4-config.c:119
  #2  0x000055d081aa3d54 in _nm_connection_verify
      (connection=0x55d0820a2b80, error=0x7ffe0fa068c0)
      at libnm-core/nm-connection.c:1441
  #3  0x000055d081aa78ec in nm_connection_normalize
      (connection=0x55d0820a2b80, parameters=0x0, modified=0x0, error=0x7ffe0fa06de8)
      at libnm-core/nm-connection.c:1688
  #4  0x000055d081aa81f4 in _nm_connection_replace_settings
      (connection=0x55d0820a2b80, new_settings=<optimized out>, parse_flags=_NM_SETTING_PARSE_FLAGS_LAST, error=0x7ffe0fa06de8) at libnm-core/nm-connection.c:432
  #5  0x000055d081aa83a6 in _nm_simple_connection_new_from_dbus
      (dict=0x55d082089950, error=0x7ffe0fa06de8, parse_flags=_NM_SETTING_PARSE_FLAGS_LAST) at libnm-core/nm-simple-connection.c:77
  #6  0x000055d081bbf942 in settings_connection_update
      (self=0x55d081fdd9f0, is_update2=1, context=0x7fc06c021dd0, new_settings=0x55d082089950, flags=NM_SETTINGS_UPDATE2_FLAG_TO_DISK)
      at src/core/settings/nm-settings-connection.c:1637
  #7  0x000055d081bbfb09 in impl_settings_connection_update2
      (obj=0x55d081fdd9f0, interface_info=<optimized out>, method_info=<optimized out>, connection=<optimized out>, sender=<optimized out>, invocation=0x7fc06c021dd0, parameters=0x55d0820f5e60) at src/core/settings/nm-settings-connection.c:1796
  #8  0x00007fc08a9db482 in call_in_idle_cb.lto_priv () at /lib64/libgio-2.0.so.0

Fixes: bb6c2d7371 ('libnm: ensure stable behavior in _nm_ip_route_attribute_validate_all()')
2021-10-08 15:10:19 +02:00
Thomas Haller
f1fee9fe27 libnm: add out_is_modified argument to from_dbus_fcn() to detect modification
Note that most implementations use g_object_set(), and it's not
easy to detect modification. In those cases, we assume that modification
happened -- just like also the GObject setter will emit a notification
(as none of our properties use G_PARAM_EXPLICIT_NOTIFY).
2021-08-02 10:01:03 +02:00
Thomas Haller
77421ba1be libnm: use macros function arguments for NMSettInfoPropertType
These functions tend to have many arguments. They are also quite som
boilerplate to implement the hundereds of properties we have, while
we want that properties have common behaviors and similarities.

Instead of repeatedly spelling out the function arguments, use a macro.

Advantages:

- the usage of a _NM_SETT_INFO_PROP_*_FCN_ARGS  macro signals that this
  is an implementation of a property. You can now grep for these macros
  to find all implementation. That was previously rather imprecise, you
  could only `git grep '\.to_dbus_fcn'` to find the uses, but not the
  implementations.
  As the goal is to keep properties "similar", there is a desire to
  reduce the number of similar implementations and to find them.

- changing the arguments now no longer will require you to go through
  all implementations. At least not, if you merely add an argument that
  has a reasonable default behavior and does not require explicit
  handling by most implementation.

- it's convenient to be able to patch the argument list to let the
  compiler help to reason about something. For example, the
  "connection_dict" argument to from_dbus_fcn() is usually unused.
  If you'd like to find who uses it, rename the parameter, and
  review the (few) compiler errors.

- it does save 573 LOC of boilerplate with no actual logic or useful
  information. I argue, that this simplifies the code and review, by
  increasing the relative amount of actually meaningful code.

Disadvantages:

- the user no longer directly sees the argument list. They would need
  cscope/ctags or an IDE to jump to the macro definition and conveniently
  see all arguments.

Also use _nm_nil, so that clang-format interprets this as a function
parameter list. Otherwise, it formats the function differently.
2021-08-02 10:01:03 +02:00
Thomas Haller
593cb57eb6 all: rename nm_utils_strdict_*() to nm_strdict_*() 2021-08-02 09:26:48 +02:00
Thomas Haller
4c3aac899e all: unify and rename strv helper API
Naming is important, because the name of a thing should give you a good
idea what it does. Also, to find a thing, it needs a good name in the
first place. But naming is also hard.

Historically, some strv helper API was named as nm_utils_strv_*(),
and some API had a leading underscore (as it is internal API).

This was all inconsistent. Do some renaming and try to unify things.

We get rid of the leading underscore if this is just a regular
(internal) helper. But not for example from _nm_strv_find_first(),
because that is the implementation of nm_strv_find_first().

  - _nm_utils_strv_cleanup()                 -> nm_strv_cleanup()
  - _nm_utils_strv_cleanup_const()           -> nm_strv_cleanup_const()
  - _nm_utils_strv_cmp_n()                   -> _nm_strv_cmp_n()
  - _nm_utils_strv_dup()                     -> _nm_strv_dup()
  - _nm_utils_strv_dup_packed()              -> _nm_strv_dup_packed()
  - _nm_utils_strv_find_first()              -> _nm_strv_find_first()
  - _nm_utils_strv_sort()                    -> _nm_strv_sort()
  - _nm_utils_strv_to_ptrarray()             -> nm_strv_to_ptrarray()
  - _nm_utils_strv_to_slist()                -> nm_strv_to_gslist()
  - nm_utils_strv_cmp_n()                    -> nm_strv_cmp_n()
  - nm_utils_strv_dup()                      -> nm_strv_dup()
  - nm_utils_strv_dup_packed()               -> nm_strv_dup_packed()
  - nm_utils_strv_dup_shallow_maybe_a()      -> nm_strv_dup_shallow_maybe_a()
  - nm_utils_strv_equal()                    -> nm_strv_equal()
  - nm_utils_strv_find_binary_search()       -> nm_strv_find_binary_search()
  - nm_utils_strv_find_first()               -> nm_strv_find_first()
  - nm_utils_strv_make_deep_copied()         -> nm_strv_make_deep_copied()
  - nm_utils_strv_make_deep_copied_n()       -> nm_strv_make_deep_copied_n()
  - nm_utils_strv_make_deep_copied_nonnull() -> nm_strv_make_deep_copied_nonnull()
  - nm_utils_strv_sort()                     -> nm_strv_sort()

Note that no names are swapped and none of the new names existed
previously. That means, all the new names are really new, which
simplifies to find errors due to this larger refactoring. E.g. if
you backport a patch from after this change to an old branch, you'll
get a compiler error and notice that something is missing.
2021-07-29 10:26:50 +02:00
Thomas Haller
c1157d73ad libnm: add from_dbus_fcn for direct NMSettingIPConfig.gateway property 2021-07-23 17:02:04 +02:00
Thomas Haller
6d07afaa8d libnm: implement special setter for direct string property for ip address
This is a normalization employed by NMSettingIPConfig.gateway.

Also rework NMSettingIPConfig.set_property() to no longer assert against
valid input. We want to pass there untrusted strings from D-Bus,
asserting is a horrible idea. Instead, either normalize the string or
keep the invalid text that will be rejected by verify().
2021-07-23 17:02:03 +02:00
Thomas Haller
a9ef71eb4a libnm: pass full property meta data to from_dbus_fcn() hook 2021-07-16 13:31:59 +02:00
Thomas Haller
bb5c89e017 libnm: refactor to_dbus_fcn() for "ipv6.dns" property
The goal is to get rid of gprop_to_dbus_fcn() uses.

Note that there is a change in behavior. The "dns" GPtrArray in
NMSettingIPConfig is never NULL (the default of the boxed property),
thus the previous code always serialized the property, even the
empty list.

Now, empty dns properties are omitted from D-Bus.
2021-07-16 13:31:59 +02:00
Thomas Haller
b756e058ac libnm: implement "direct" properties for compare_fcn() 2021-07-16 13:31:58 +02:00
Thomas Haller
243459dc3a libnm: refactor NMSettingClass.compare_property() to NMSettInfoPropertType.compare_fcn()
NMSettingClass.compare_property() will be dropped.
2021-07-16 13:31:58 +02:00
Thomas Haller
7e7d2d173a libnm: add compare_fcn() to property meta data
So far, we only have NMSettingClass.compare_property() hook.
The ugliness is that this hook is per-setting, when basically
all implementations only compare one property.

It feels cleaner to have a per-property hook and call that consistently.

In step one, we give all properties (the same) compare_fcn() implementation,
which delegates to the existing NMSettingClass.compare_property().
In a second step, this will be untangled.

There is one problem with this approach: NMSettInfoPropertType grows by
one pointer size, and we have potentially many such types. That should
be addressed by unifying types in the future.
2021-07-16 13:31:57 +02:00
Thomas Haller
d8292d462b libnm: pass around property_info instead of property_idx in NMSetting API
Various NMSetting API would accept a property_idx parameter. Together
with the NMSettInfoSetting instance, this was useful to find the actual
NMSettInfoProperty instance.

The idea was, to provide the most of the functionality. That is, if you
might need the property_idx too, you had it -- after all, the
property_info you could lookup yourself.

However,

- literally zero users care about the property_idx. The care about
  the property_info.

- if the user really, really required the property_idx, then it
  is a given that it can be easily computed by

     (property_info - sett_info->property_infos)
2021-07-16 13:31:57 +02:00
Thomas Haller
56241f328f libnm: always initialize default values for "direct" properties
We encode the default value "direct" properties in the GParamSpec.
But we also avoid CONSTRUCT properties, because they have an overhead
and they are generally odd for the settings.

So up to now, it was cumbersome to explicitly set the default value,
but it was also error prone.

Avoid that by always initializing the default value for our "direct"
properties.
2021-07-12 13:56:39 +02:00
Thomas Haller
102a1f5c31 libnm: use _nm_setting_property_define_direct_string() 2021-07-12 13:56:33 +02:00
Thomas Haller
233776c2c7 libnm: use _nm_setting_property_define_direct_boolean()
There is a new way how to implement default boolean properties.
Use it.
2021-07-12 13:56:31 +02:00
Thomas Haller
03dc5ad391 libnm: let NMSettingIPConfigPrivate be tracked by subclasses
For our property meta data handling we require that all the meta data is
associated with one GType.

NMSettingIPConfig is a parent class of NMSettingIP[46]Config. Note that
we already have _nm_sett_info_property_override_create_array_ip_config()
because the meta data must be initialized together at one place.

We will require that we can find the offset for properties based on one
offset per type. That is cumbersome, if NMSettingIPConfigPrivate is
private itself.

Simplify that, by internally sharing NMSettingIPConfigPrivate and let
the subclasses embed the private data in their own private data.

Optimally we would simply embed the private struct as field into
NMSettingIPConfig. But that would be an ABI change as that struct
was public before 1.32. Don't change ABI for now, so we have to
awkwardly place it into the subclasses private data.
2021-07-12 13:56:31 +02:00
Beniamino Galvani
cb5960cef7 all: add a new ipv{4,6}.required-timeout property
Add a new property to specify the minimum time interval in
milliseconds for which dynamic IP configuration should be tried before
the connection succeeds.

This property is useful for example if both IPv4 and IPv6 are enabled
and are allowed to fail. Normally the connection succeeds as soon as
one of the two address families completes; by setting a required
timeout for e.g. IPv4, one can ensure that even if IP6 succeeds
earlier than IPv4, NetworkManager waits some time for IPv4 before the
connection becomes active.
2021-07-05 15:15:44 +02:00
Thomas Haller
47cdcb3ce2 libnm: fix memleak setting "ipv[46].dhcp-iaid" property
Fixes: 56a1a5426a ('all: add ipvX.dhcp-iaid properties')
2021-07-01 11:49:18 +02:00
Thomas Haller
668c90dbb8 libnm: fix crash in nm_ip_routing_rule_from_string()
import gi
    gi.require_version("NM", "1.0")
    from gi.repository import NM

    r = NM.IPRoutingRule.from_string('priority 10 type blackhole', NM.IPRoutingRuleAsStringFlags.AF_INET)
    r.to_string(NM.IPRoutingRuleAsStringFlags.NONE)

    r = NM.IPRoutingRule.from_string('priority 10 blackhole', NM.IPRoutingRuleAsStringFlags.AF_INET)
    r.to_string(NM.IPRoutingRuleAsStringFlags.NONE)

    r= NM.IPRoutingRule.from_string('priority 10 bogus', NM.IPRoutingRuleAsStringFlags.AF_INET)
    # CRASH

Fixes: e922404990 ('libnm,core: support "prohibit"/"blackhole"/"unreachable" type routing rules')
2021-06-30 17:13:23 +02:00
Thomas Haller
a3eb2c7026 libnm: use _nm_setting_property_define_string() for string NMSetting properties 2021-06-23 12:47:40 +02:00
Thomas Haller
22edf34ba3 libnm: use _nm_setting_property_define_boolean() for boolean NMSetting properties 2021-06-23 12:47:38 +02:00
Thomas Haller
69597a67c1 libnm: add and use NM_SETT_INFO_PROPERT_TYPE_DBUS() macro 2021-06-23 12:13:40 +02:00
Thomas Haller
c161439b73 libnm: let all property types implement to_dbus_fcn() handler
If a property can be converted to D-Bus, then always set the
to_dbus_fcn() handler. The only caller of to_dbus_fcn() is
property_to_dbus(), so this means that property_to_dbus()
has no more default implementation and always delegates to
to_dbus_fcn().

The code is easier to understand if all properties implement
to_dbus_fcn() the same way.

Also, there is supposed to be a split between NMSettInfoProperty (info about
the property) and NMSettInfoPropertType (the type). The idea is that
each property (obviously) requires its distinct NMSettInfoProperty, but
they can share a common type implementation.
With NMSettInfoPropertType.gprop_to_dbus_fcn that is often violated because
many properties that implement NMSettInfoPropertType.gprop_to_dbus_fcn
require a special type implementation. As such, gprop_to_dbus_fcn should
be part of the property info and not the property type. The first step towards
that is unifying all properties to use to_dbus_fcn().
2021-06-23 12:13:39 +02:00
Thomas Haller
06296f7c3c libnm: belatedly export nm_ip_routing_rule_[gs]et_uid_range() symbols 2021-06-21 10:00:20 +02:00
Thomas Haller
eea912dfb3 libnm: expose nm_ip_address_dup(), nm_ip_route_dup() API in libnm
This fixes commit 21c8a6b20e ('libnm-core, all: merge IPv4 and IPv6
address/route types'), which introduced this API but didn't export it
in the library. In practice this API is thus only usable since 1.32.0.

(cherry picked from commit 05f2a0b024)
2021-06-15 19:13:27 +02:00
Thomas Haller
8ba66f8ec9 trivial: improve code comments 2021-05-26 12:07:11 +02:00
Yuri Chornoivan
5ff6cf575a libnm: don't use defined strings in translated strings in "nm-setting-ip-config.c"
[thaller@redhat.com: reworked patch by Yuri]

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/800
2021-03-30 17:54:26 +02:00
Thomas Haller
e922404990 libnm,core: support "prohibit"/"blackhole"/"unreachable" type routing rules 2021-03-23 14:19:39 +01:00
Thomas Haller
972d1ba046 libnm,core: support "uidrange" parameter for routing rules 2021-03-23 14:19:38 +01:00
Thomas Haller
ba72d5a7e7 libnm: use binary search to lookup D-Bus info for routing-rules 2021-03-23 14:19:38 +01:00
Thomas Haller
945612cc5d all: use nm_net_aux_rtnl_rtntype_{n2a,a2n}() helpers 2021-03-23 14:19:38 +01:00
Thomas Haller
9dc84b32b0 build: move "shared/nm-{glib-aux,log-null,log-core}" to "src/libnm-{glib-aux,log-null,log-core}" 2021-02-24 12:48:20 +01:00
Thomas Haller
fdf9614ba7 build: move "libnm-core/" to "src/" and split it
"libnm-core/" is rather complicated. It provides a static library that
is linked into libnm.so and NetworkManager. It also contains public
headers (like "nm-setting.h") which are part of public libnm API.

Then we have helper libraries ("libnm-core/nm-libnm-core-*/") which
only rely on public API of libnm-core, but are themself static
libraries that can be used by anybody who uses libnm-core. And
"libnm-core/nm-libnm-core-intern" is used by libnm-core itself.

Move "libnm-core/" to "src/". But also split it in different
directories so that they have a clearer purpose.

The goal is to have a flat directory hierarchy. The "src/libnm-core*/"
directories correspond to the different modules (static libraries and set
of headers that we have). We have different kinds of such modules because
of how we combine various code together. The directory layout now reflects
this.
2021-02-18 19:46:51 +01:00