Commit graph

79 commits

Author SHA1 Message Date
Thomas Haller
7459548f23 core: return remaining lifetime from nm_utils_lifetime_get()
nm_utils_lifetime_get() already has so many arguments.
Essentially, the function returned %TRUE if and only if the
lifetime was greater then zero.

Combine the return value and the output argument for the lifetime.

It also matches better the function name: to get the lifetime.
2018-02-09 17:40:01 +01:00
Beniamino Galvani
e91f1a7d2a dns: introduce routing domains
Similarly to what systemd-resolved does, introduce the concept of
"routing" domain, which is a domain in the search list that is used
only to decide the interface over which a query must be forwarded, but
is not used to complete unqualified host names. Routing domains are
those starting with a tilde ('~') before the actual domain name.

Domains without the initial tilde are used both for completing
unqualified names and for the routing decision.
2018-01-12 13:42:08 +01:00
Thomas Haller
3ee8de20c4 all: include "nm-utils/nm-hash-utils.h" by default
Next we will use siphash24() instead of the glib version g_direct_hash() or
g_str_hash(). Hence, the "nm-utils/nm-hash-utils.h" header becomes very
fundamental and will be needed basically everywhere.

Instead of requiring the users to include them, let it be included via
"nm-default.h" header.
2017-11-16 11:49:51 +01:00
Thomas Haller
cd271d5cb1 core: add nm_utils_sysctl_ip_conf_is_path() util 2017-10-24 16:05:40 +02:00
Thomas Haller
32b3eb1181 core: merge IPv4 and IPv6 implementation of nm_utils_ip4_property_path()
and nm_utils_ip6_property_path().

Also, rename to nm_utils_sysctl_ip_conf_path().
2017-10-24 16:05:40 +02:00
Thomas Haller
6e01238a40 core: don't use static buffer for nm_utils_ip4_property_path()
and nm_utils_ip6_property_path(). The API with static buffers
looks a bit nicer. But I think they are dangerous, because
we tend to pass the buffer down several layers of the stack, and
it's not immediately clear, that we don't overwrite the static
buffer again (which we probably did not, but it's hard to verify
that there is no bug there).
2017-10-24 16:04:46 +02:00
Thomas Haller
de4742333a core: add option to pass ownership of file descriptor to nm_utils_fd_get_contents()
In many scenarios, we have no use for the file descriptor
after nm_utils_fd_get_contents(). We just want to read it
and close it.

API wise, it would be nice that the get_contents() function never
closes the passed in fd and it's always responsibility of the caller.

However, that costs an additional dup() syscall that could
be avoided, if we allow the function to (optionally) close
the file descriptor.
2017-10-19 15:49:58 +02:00
Thomas Haller
2f56de7492 all: add helper functions for nm_hash_update*()
By using a macro, we don't cast all the types to guint. Instead,
we use their native types directly. Hence, we don't need
nm_hash_update_uint64() nor nm_hash_update_ptr().
Also, for types smaller then guint like char, we save hashing
the all zero bytes.
2017-10-18 13:29:22 +02:00
Thomas Haller
0e9e35e309 all: refactor hashing by introducing NMHashState
The privious NM_HASH_* macros directly operated on a guint value
and were thus close to the actual implementation.

Replace them by adding a NMHashState struct and accessors to
update the hash state. This hides the implementation better
and would allow us to carry more state. For example, we could
switch to siphash24() transparently.

For now, we still do a form basically djb2 hashing, albeit with
differing start seed.

Also add nm_hash_str() and nm_str_hash():

- nm_hash_str() is our own string hashing implementation

- nm_str_hash() is our own string implementation, but with a
  GHashFunc signature, suitable to pass it to g_hash_table_new().
  Also, it has this name in order to remind you of g_str_hash(),
  which it is replacing.
2017-10-18 13:05:00 +02:00
Thomas Haller
281d2d9fad shared: split random and hash utils
"nm-utils/nm-shared-utils.h" shall contain utility function without other
dependencies. It is intended to be used by other projects as-is.

nm_utils_random_bytes() requires getrandom() and a HAVE_GETRANDOM configure
check. That makes it more cumbersome to re-use "nm-shared-utils.h", in
cases where you don't care about nm_utils_random_bytes().

Split nm_utils_random_bytes() out to a separate file.

Same for hash utils, which depend on nm_utils_random_bytes(). Also, hash
utils will eventually be extended to use siphash24.
2017-10-17 20:02:59 +02:00
Thomas Haller
b01a453ca2 core: add nm_utils_random_bytes() and use getrandom()
Add a new function nm_utils_random_bytes().

This function now preferably uses getrandom() syscall if it is
available.

As fallback, it always tries to fill the buffer from /dev/urandom.
If it cannot, as last fallback it uses GRand, which cannot fail.
Hence, the function always sets some (pseudo) random bytes.

It also returns FALSE if the obtained bytes are possibly not good
randomness.
2017-10-13 12:47:55 +02:00
Thomas Haller
93ea7a5905 shared: move nm_utils_fd_*() from src/ to shared/nm-utils/
The functions are general purpose and independent from NetworkManager core.
Move them to "shared/nm-utils/" so they can be used independently.
2017-10-13 12:47:55 +02:00
Thomas Haller
32142a63e8 ndisc: rename NMNDiscPreference to NMIcmpv6RouterPref
We encounter the same enum in 3 forms:

  - NMNDiscPreference in NetworkManager
  - "enum ndp_route_preference" in <ndp.h>
  - ICMPV6_ROUTER_PREF_* in <linux/icmpv6.h>

Move our enum to nm-core-utils.h, so that it can be used
by platform code as well (platform code should not include
ndisc/nm-ndisc.h).

Also, NMNDiscPreference was not numerically identical to their
native values (meaning: it shuffled the names and numbers).
Make them all numerically equal, so that they can be used in
the same context.
This means, while previously we could compare NMNDiscPreference
directly according to their priority, we now need _preference_to_priority().
On the other hand, we could omit translate_preference() -- but actually,
we still have _route_preference_coerce() because pref comes from libndp
and is thus untrusted. We still have to range check it.
2017-10-12 10:27:27 +02:00
Thomas Haller
5c299454b4 core: rework tracking of gateway/default-route in ip-config
Instead of having 3 properties @gateway, @never_default and @has_gateway
on NMIP4Config/NMIP6Config that determine the default-route, track the
default-route as a regular route.

The gateway setting is the configuration knob for the default-route.
Since an NMIP4Config/NMIP6Config instance only has one gateway property,
it cannot track more then one default-routes (see related bug rh#1445417).
Especially with policy routing, it might be interesting to configure a
default-route in multiple tables.

Also, later it might be interesting to allow adding default-routes as
regular static routes in a connection, so that the user can configure additional
route parameters for the default-route or add default-routes in multiple tables.

With this patch, default-routes now have a rt_source property according to their
origin.

Also, the previous commits of this branch broke handling of the
default-route :) . That should be working now again.
2017-10-10 08:46:47 +02:00
Thomas Haller
cc1ee1d286 all: rework configuring route table support by adding "route-table" setting
We added "ipv4.route-table-sync" and "ipv6.route-table-sync" to not change
behavior for users that configured policy routing outside of NetworkManager,
for example, via a dispatcher script. Users had to explicitly opt-in
for NetworkManager to fully manage all routing tables.

These settings were awkward. Replace them with new settings "ipv4.route-table"
and "ipv6.route-table". Note that this commit breaks API/ABI on the unstable
development branch by removing recently added API.

As before, a connection will have no route-table set by default. This
has the meaning that policy-routing is not enabled and only the main table
will be fully synced. Once the user sets a table, we recognize that and
NetworkManager manages all routing tables.

The new route-table setting has other important uses: analog to
"ipv4.route-metric", it is the default that applies to all routes.
Currently it only works for static routes, not DHCP, SLAAC,
default-route, etc. That will be implemented later.

For static routes, each route still can explicitly set a table, and
overwrite the per-connection setting in "ipv4.route-table" and
"ipv6.route-table".
2017-10-09 22:05:36 +02:00
Thomas Haller
8f1ef161f4 core: refactor parsing resolve.conf
- merge the IPv4 and IPv6 implementations. They are for the most
  part identical. Also, they are independent of NMIP4Config/NMIP6Config.

- parse the entire file at once. Don't parse it twice, once for the
  name servers and once for the options. This also avoids loading
  /etc/resolv.conf twice, as it would be done before.
2017-10-09 22:05:35 +02:00
Thomas Haller
1348b7c83a shared: move utils NM_UTILS_ENUM2STR*, NM_UTILS_FLAGS2STR*, and nm_utils_to_string_buffer_init()
These are just simple utility functions, not tied to NetworkManager
core. Move them to shared/nm-utils, so that they may be reused
otherwise.
2017-10-05 11:50:31 +02:00
Thomas Haller
5b0f895e19 libnm,core: add TABLE attribute for routes settings
https://bugzilla.redhat.com/show_bug.cgi?id=1436531
2017-09-26 19:39:36 +02:00
Thomas Haller
dd0617547d shared: add NMIPAddr struct 2017-09-18 20:14:09 +02:00
Thomas Haller
944006d485 core: add nm_utils_ip4_address_same_prefix() function 2017-09-13 08:17:31 +02:00
Thomas Haller
315bd0a97d core: add nm_utils_connection_has_default_route()
Taken from "src/nm-default-route-manager.c".
2017-09-08 11:05:04 +02:00
Thomas Haller
c16e871888 core: nm_utils_ip_route_metric_normalize() util
Functions that take and addr_family argument are just nicer
to use at places where we treat IPv4 and IPv6 generically.
2017-09-08 11:05:04 +02:00
Thomas Haller
372f14a6ef platform: add compare functions for routes with different compare semantics
Routes are complicated.

`ip route add` and `ip route append` behaves differently with respect to
determine whether an existing route is idential or not.

Extend the cmp() and hash() functions to have a compare type, that
covers the different semantics.
2017-08-03 18:32:59 +02:00
Thomas Haller
54f8c2ac22 core: add nm_utils_ip6_address_same_prefix_cmp() helper
Useful for sorting/comparing.
2017-07-31 15:13:31 +02:00
Thomas Haller
55e66cc7e6 platform: implement hash function for NMPlatformLnk types 2017-07-05 18:37:39 +02:00
Thomas Haller
6d9c3eab38 platform: let NMPObject implement NMDedupIndexObj 2017-07-05 14:22:10 +02:00
Yuri Chornoivan
0050e8bd34 all: fix typos in documentation, translated strings and comments
https://bugzilla.gnome.org/show_bug.cgi?id=783173
2017-05-28 17:33:37 +02:00
Thomas Haller
951e5f5bf8 core: make dad_counter argument guint32 type
The dad_counter is hashed into the resulting address. Since we
want the hashing to be independent of the architecture, we always
hash 32 bit of dad_counter. Make the dad_counter argument of
type guint32 for consistency.

In practice this has no effect because:
  - for all our (current!) architectues, guint is the same as
    guint32.
  - all callers of nm_utils_ipv6_addr_set_stable_privacy() keep
    their dad-counter argument as guint8, so they never even pass
    numbers larger then 255.
  - nm_utils_ipv6_addr_set_stable_privacy() limits dad_counter
    further against RFC7217_IDGEN_RETRIES.
2017-04-27 16:34:58 +02:00
Thomas Haller
67da0a28db core: move NMIPAddr to nm-core-utils.h 2017-04-27 16:25:20 +02:00
Thomas Haller
d5bcc5826e shared: move NM_UTILS_LOOKUP() macro shared utils 2017-04-05 16:53:06 +02:00
Thomas Haller
0a34ae55a2 core/trivial: rename nm_utils_10pow() to nm_utils_exp10()
nm_utils_exp10() is a better name, because it reminds of the function
exp10() from <math.h> which has a similar purpose (but whose argument
is double, not gint16).
2017-03-24 11:28:57 +01:00
Thomas Haller
25654cfa31 core: add nm_utils_10pow() utils 2017-03-23 19:06:02 +01:00
Thomas Haller
b869d9cc0d device: add spec "driver:" to match devices
Changing the MAC address of devices is known to fail with
certain drivers. Add a device-spec to allow disabling it
for for such devices.

Related: https://bugzilla.gnome.org/show_bug.cgi?id=777523
2017-03-17 17:40:00 +01:00
Thomas Haller
8a31e66d2c core: add activation-type property to active-connection
It is still unused, but will be useful to mark a connection
whether it is a full activation or assumed.
2017-03-16 18:27:33 +01:00
Beniamino Galvani
ef491acfde core: split nm_utils_format_con_diff_for_audit()
The function will be called to display in audit logs the changes done
to connections.
2017-03-06 10:29:37 +01:00
Thomas Haller
93f7ab2c54 core: consolidate sorting of connections by autoconnect/timestamp
NMPolicy's auto_activate_device() wants to sort by autoconnect-priority,
nm_utils_cmp_connection_by_autoconnect_priority() but fallback to the default
nm_settings_connection_cmp_default(), which includes the timestamp.

Extend nm_settings_connection_cmp_default() to consider the
autoconnect-priority as well. Thus change behavior so that
nm_settings_connection_cmp_default() is the sort order that
auto_activate_device() wants. That makes sense, as
nm_settings_connection_cmp_default() already considered the
ability to autoconnect as first. Hence, it should also honor
the autoconnect priority.

When doing that, rename nm_settings_connection_cmp_default()
to nm_settings_connection_cmp_autoconnect_priority().
2017-02-10 14:43:24 +01:00
Thomas Haller
eb5ceedbba core: add nm_utils_cmp_connection_by_autoconnect_priority_p_with_data() function
Have a proper cmp() function and a wrapper *_p_with_data() that can be
used for g_qsort_with_data().

Thus, establish a naming scheme (*_p_with_data()) for these compare
wrappers that we need all over the place. Note, we also have
nm_strcmp_p_with_data() for the same reason and later more such
functions will follow.
2017-02-10 14:43:24 +01:00
Lubomir Rintel
6404c79e4d core: kill nm_spawn_process()
It's not used anymore. Which is a good thing, because if it was used
we'd have to get rid of the uses.

It did accept a whitespace separated string for an argument, which is
never useful for us; it indicated error either on g_spawn_sync()
failure or an error status code of the program spawned, but only set the
error in the former case which had let to errors.

The would would be a bit nicer place without it.
(But not much)
2017-02-03 18:53:40 +01:00
Thomas Haller
ba1cc6a288 core: refactor evaluation of device's match-spec
Previously, we would have different functions like
  - nm_match_spec_device_type()
  - nm_match_spec_hwaddr()
  - nm_match_spec_s390_subchannels()
  - nm_match_spec_interface_name()
which all would handle one type of match-spec.

So, to get the overall result whether the arguments
match or not, nm_device_spec_match_list() had to stich
them together and iterate the list multiple times.

Refactor the code to have one nm_match_spec_device()
function that gets all relevant paramters.

The upside is:

  - the logic how to evaluate the match-spec is all at one place
    (match_device_eval()) instead of spread over multiple
    functions.

  - It requires iterating the list at most twice. Twice, because
    we do a fast pre-search for "*".

One downside could be, that we have to pass all 4 arguments
for the evaluation, even if the might no be needed. That is,
because "nm-core-utils.c" shall be independend from NMDevice, it
cannot receive a device instance to get the parameters as needed.
As we would add new match-types, the argument list would grow.
However, all arguments are cached and fetching them from the
device's private data is very cheap.

(cherry picked from commit b957403efd)
2017-01-20 21:18:30 +01:00
Thomas Haller
f0d40525df device: support dynamic "connection.stable-id" in form of text-substitution
Usecase: when connecting to a public Wi-Fi with MAC address randomization
("wifi.cloned-mac-address=random") you get on every re-connect a new
IP address due to the changing MAC address.
"wifi.cloned-mac-address=stable" is the solution for that. But that
means, every time when reconnecting to this network, the same ID will
be reused. We want an ID that is stable for a while, but at a later
point a new ID should e generated when revisiting the Wi-Fi network.

Extend the stable-id to become dynamic and support templates/substitutions.
Currently supported is "${CONNECTION}", "${BOOT}" and "${RANDOM}".
Any unrecognized pattern is treated verbaim/untranslated.

"$$" is treated special to allow escaping the '$' character. This allows
the user to still embed verbatim '$' characters with the guarantee that
future versions of NetworkManager will still generate the same ID.
Of course, a user could just avoid '$' in the stable-id unless using
it for dynamic substitutions.

Later we might want to add more recognized substitutions. For example, it
could be useful to generate new IDs based on the current time. The ${} syntax
is extendable to support arguments like "${PERIODIC:weekly}".

Also allow "connection.stable-id" to be set as global default value.
Previously that made no sense because the stable-id was static
and is anyway strongly tied to the identity of the connection profile.
Now, with dynamic stable-ids it gets much more useful to specify
a global default.

Note that pre-existing stable-ids don't change and still generate
the same addresses -- unless they contain one of the new ${} patterns.
2017-01-09 14:50:33 +01:00
Thomas Haller
21ae09c1cc core: add assertions for network_id/stable_type
We require a network-id. Assert that it is set.

Also, we encode the stable-id as uint8. Thus, add
an assertion that we don't use more then 254 IDs.

If we ever make use of stable-type 255, we must extend
the encoding to allow for more values. The assertion
is there to catch that.
2017-01-09 14:50:33 +01:00
Thomas Haller
dea3c49ce2 utils: add nm_utils_get_boot_id() util to read "/proc/sys/kernel/random/boot_id" 2017-01-09 14:50:33 +01:00
Thomas Haller
f779c51f87 shared: move nm_utils_strbuf_*() helper to shared/nm-utils 2017-01-05 11:13:17 +01:00
Thomas Haller
1d9bdad1df core: add nm_utils_file_get_contents() and nm_utils_fd_get_contents()
A reimplementation of g_file_get_contents() to overcome two limitations:

 - nm_utils_file_get_contents() accepts a @dirfd argument to open the
   file relative using openat().
 - nm_utils_fd_get_contents() allows to read the content from a file
   filedescriptor.
 - both support a max_length argument, to fail gracefully if we
   get tricked into loading a huge file.
2016-12-13 11:26:58 +01:00
Beniamino Galvani
2e21d78494 core: factor out plugin validation
The new function will be used to validate other plugins we load.
2016-12-06 11:08:11 +01:00
Beniamino Galvani
7ae6e980e4 core: move plugin path reading function to nm-core-utils.c
Even if it's called only from one place, it can be considered an
utility function.
2016-12-06 11:08:11 +01:00
Thomas Haller
7bf4a15504 build: merge "src/tests/Makefile.am" into toplevel Makefile 2016-10-21 17:04:06 +02:00
Beniamino Galvani
21358edc54 core: introduce and use nm_utils_file_set_contents()
In some places we use g_file_set_contents() after a umask() to limit
the permissions of the created file. Unfortunately if the containing
directory has a default ACL the umask will be ignored and the new file
will have a mode equal to the default ACL (since g_file_set_contents()
opens the file with mode 0666).

Calling a chmod() after the file gets created is insecure (see commit
60b7ed3bdc) and so the only solution seems to be to reimplement
g_file_set_contents() and accept a mode as parameter.

We already had similar functions in the tree, consolidate them into a
new generic utility function.

https://bugzilla.gnome.org/show_bug.cgi?id=769702
2016-10-12 14:54:57 +02:00
Thomas Haller
0bdcab100c all: cleanup includes in header files
- don't include "nm-default.h" in header files. Every source file must
  include as first header "nm-default.h", thus our headers get the
  default include already implicitly.

- we don't support compiling NetworkManager itself with a C++ compiler. Remove
  G_BEGIN_DECLS/G_END_DECLS from internal headers. We do however support
  users of libnm to use C++, thus they stay in public headers.

(cherry picked from commit f19aff8909)
2016-08-17 19:51:17 +02:00
Thomas Haller
c9c00ec5c6 rdisc: move public fields from NMRDisc to NMRDiscPrivate
As they are initialized from the constructor of the subclass, the have
to be construct-only GObject properties, which brings some overhead.
2016-07-08 12:08:53 +02:00