Commit graph

470 commits

Author SHA1 Message Date
Thomas Haller
1f08b01714 platform: cleanup nm_platform_link_get_address() to return-early
Avoid nested if-blocks, and instead check conditions and return early.
2018-02-15 16:08:00 +01:00
Thomas Haller
0d53e093e6 platform: fix handling secondary addresses during nm_platform_ip4_address_sync()
Although IFA_F_TEMPORARY is numerically equal to IFA_F_SECONDARY,
their meaning is different. One applies to IPv6 temporary addresses,
and the other to IPv4 secondary addresses.

During _addr_array_clean_expired() we want to ignore and clear
IPv6 temporary addresses, but not IPv4 secondary addresses.

Fixes: f2c4720bca
2018-02-09 21:08:07 +01:00
Thomas Haller
3e9e51f1dd core: distinguish between IFA_F_SECONDARY and IFA_F_TEMPORARY
While the numerical values of IFA_F_SECONDARY and IFA_F_TEMPORARY
are identical, their meaning is not.

IFA_F_SECONDARY is only relevant for IPv4 addresses, while
IFA_F_TEMPORARY is only relevant for IPv6 addresses.

IFA_F_TEMPORARY is automatically set by kernel for the addresses
that it generates as part of IFA_F_MANAGETEMPADDR. It cannot be
actively set by user-space.

IFA_F_SECONDARY is automatically set by kernel depending on the order
in which the addresses for the same subnet are added.

This essentially reverts 8b4f11927 (core: avoid IFA_F_TEMPORARY alias for
IFA_F_SECONDARY).
2018-02-09 21:07:57 +01:00
Thomas Haller
7e208c1d28 platform: rework nm_platform_ip6_address_sync() to fix address order
We want to add addresses in a particular order so that source address
selection works.

Note that @known_addresses contains the desired addresses in order of
least-important first, while @plat_addresses contains them in opposite
order. Previously, this inverted order was not considered, and we
essentially ended up removing and re-adding all addresses every time.

Fix that. While at it, get rid of the O(n^2) runtime complexity, and
make it O(n) by iterating both lists simultaneously.
2018-02-09 17:40:01 +01:00
Thomas Haller
f2c4720bca platform: clear temporary addresses early during nm_platform_ip6_address_sync()
Temporary addresses (RFC4941) are not handled by NetworkManager directly, but by
kernel. If they are in the @known_addresses list, clear them out early.
They shall be ignored.
2018-02-09 17:40:01 +01:00
Thomas Haller
d5a51a1ad2 platform: modifiy @known_addresses list in nm_platform_ip6_address_sync()
Often, we want in API that an input argument is read-only and not modified
by the function call. Not modifying input arguments is a good
convention.

However, in this case there are only two callers, and both clearly do
not care whether the @known_addresses array will be modified.

Clear out addresses that are already expired and enforce that there are
no duplicate addresses. Basically, use @known_addresses for bookkeeping
which addresses are to be ignored.
2018-02-09 17:40:01 +01:00
Thomas Haller
7f223cb827 platform: refactor initial cleanup of know-addresses list in nm_platform_ip4_address_sync()
We do a pre-run that constructs an index of all addresses and drops
addresses that are already expired.

Move this code to a separate function, it will be reused for IPv6.

Also, note that nm_platform_ip4_address_sync() has only 2 callers. Both
callers make sure to not pass duplicate known addresses, because the
addresses also come from a cache. Make that a requirement and assert
against unique addresses. If we would allow duplicate addresses, we would
have to handle them in a defined way (like, dropping the ones with lower
priority). That would be more complicated, and since no caller is
supposed to provide duplicate addresses, don't bother but assert.
2018-02-09 17:40:01 +01:00
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
Thomas Haller
06b968a820 platform: add nm_platform_refresh_all() API
Add a function that allows to re-request all objects of a certain type.
Usually, the cache is supposed to keep itself in a consistent state and
this function is not useful.

It is however useful during testing and debugging to explicitly reload
an object type.

If you ever think to need this function in non-testing code, then
something else is probably wrong with the cache implementation.
2018-02-09 17:40:01 +01:00
Thomas Haller
54b2e8d679 platform: allow omitting output arguments for nm_utils_lifetime_get()
At various places we don't need the output argument. Allow to omit it,
which cleans up the caller.
2018-02-07 13:42:24 +01:00
Thomas Haller
d2871e453f platform: reorder printing address flags in nm_platform_addr_flags2str()
Print the "tentative" flags as last. Most other flags, have more the character of
a user configured attribute, while "tentative" reflects the current state of the address.

Previously, we would log
    secondary,tentative
and
    tentative,mngtmpaddr,noprefixroute

Print the "tenative" flag last. This way, the flag that commonly
will flip by kernel's decision, is consistently printed last.
2018-02-07 13:41:52 +01:00
Thomas Haller
8b4f119272 core: avoid IFA_F_TEMPORARY alias for IFA_F_SECONDARY
IFA_F_SECONDARY and IFA_F_TEMPORARY have the same numerical values,
and are synonymous. Consistently use IFA_F_SECONDARY.
2018-02-07 13:37:12 +01:00
Lubomir Rintel
8a46b25cfa all: require glib 2.40
RHEL 7.1 and Ubuntu 14.04 LTS both have this.

https://bugzilla.gnome.org/show_bug.cgi?id=792323
2018-01-18 11:45:36 +01:00
Beniamino Galvani
da4c9e51a0 ip-tunnel: add support for tunnel flags
Implement support for IP tunnel flags. Currently only some IPv6 tunnel
flags are supported. Example:

 # nmcli connection add type ip-tunnel mode ip6ip6 \
   ip-tunnel.flags ip6-ign-encap-limit,ip6-use-orig-tclass \
   ifname abc ip-tunnel.parent ens8 ipv4.method disabled \
   ipv6.method manual ipv6.address ::8888 remote ::42

 # ip -d l
  61: abc@ens8: <NOARP,UP,LOWER_UP> mtu 1460 qdisc noqueue ...
    link/tunnel6 :: brd ::42 promiscuity 0
    ip6tnl ip6ip6 remote ::42 local :: dev ens8 encaplimit none
    hoplimit 0 tclass inherit ...

https://bugzilla.gnome.org/show_bug.cgi?id=791846
2018-01-05 18:25:08 +01:00
Beniamino Galvani
ffc04e178b platform: fix ipv6 address synchronization
The @keep_link_local logic was wrong: when set to TRUE we must not
delete addresses and when set to FALSE we must delete addresses only
if they are unknown.

Also, ignore link-local addresses when comparing positions.

Fixes: 19d6d54b6f
2017-12-15 09:56:00 +01:00
Beniamino Galvani
19d6d54b6f platform: improve ipv6 addresses synchronization
nm_platform_ip6_address_sync() must take care not only of adding
missing addresses and removing unknown addresses, but also of the
order in which they are added. The order is important because it
determines which address is preferred by kernel.

Since we can only add addresses at the top of the list, in order to
change the position of an address we must first remove it and then
re-add it in the right position.
2017-12-12 15:17:10 +01:00
Thomas Haller
62d4dba74b platform: assert() for valid item in nm_platform_link_get_all()
Coverity thinks that item might be NULL, but actually it
cannot. Unclear how to avoid the false positive.
2017-12-12 11:15:38 +01:00
Thomas Haller
27e8fffdb8 platform: fix crash hashing NMPlatformTfilter and NMPlatformQdisc
@kind might be NULL. There are 3 forms of the hash-update functions for
string: str(), str0(), and strarr().

- str0() is when the string might be NULL.
- str() does not allow the string to be NULL
- strarr() is like str(), except it adds a G_STATIC_ASSERT()
  that the argument is a C array.

The reason why a difference between str() and str0() exists, is
because str0() hashes NULL different from a "" or any other string.
This has an overhead, because it effectively must hash another bit
of information that tells whether a string was passed or not.

The reason is, that hashing a tupple of two strings should always
yield a different hash value, even for "aa",""; "a","a"; "","aa",
where naive concatentation would yield identical hash values in all
three cases.

Fixes: e75fc8279b
2017-12-12 11:15:38 +01:00
Thomas Haller
fe3d7209e7 platform: fix TC to-string/hash/cmp functions to include the action
Also add a define NM_PLATFORM_ACTION_KIND_SIMPLE. It makes the
uses of "simple" grepable.
2017-12-11 11:08:41 +01:00
Lubomir Rintel
b0fd3ecbaf platform: add support for traffic filters 2017-12-11 11:08:41 +01:00
Lubomir Rintel
ff9f27eb12 platform: add support for queueing disciplines 2017-12-11 10:52:22 +01:00
Thomas Haller
7573594a21 platform: merge nm_platform_*_delete() delete functions
It only makes sense to call delete() with NMPObjects that
we obtained from the platform cache. Otherwise, if we didn't
get it from the cache in the first place, we wouldn't know
what to delete.

Hence, the input argument is (almost) always an NMPObject
in the first place. That is different from add(), where
we might create a new specific NMPlatform* instance on the
stack. For add() it makes slightly more sense to have different
functions depending on the type. For delete(), it doesn't.
2017-12-11 10:30:26 +01:00
Lubomir Rintel
93ac0e455b platform/trivial: s/ADDRROUTE/OBJECT/ for the cache lookup
It's going to be useful for other objects that have a type (of course)
and an ifindex.
2017-12-11 10:30:26 +01:00
Thomas Haller
653aab70ac platform: preserve errno in nm_platform_sysctl_get_int_checked()
It's not clear whether free() changes errno. Be sure about it.

https://bugzilla.gnome.org/show_bug.cgi?id=790726
2017-11-24 17:06:42 +01:00
Thomas Haller
93adadbdcb all: use nm_direct_hash() instead of g_direct_hash()
We also do this for libnm, where it causes visible changes
in behavior. But if somebody would rely on the hashing implementation
for hash tables, it would be seriously flawed.
2017-11-16 11:49:52 +01:00
Thomas Haller
b58481b31e all: don't use g_direct_equal() for hash table equality function
GHashTable optimizes a NULL equality function to use direct pointer
comparison. That saves the overhead of calling g_direct_equal().
This is also documented behavior for g_hash_table_new().

While at it, also don't pass g_direct_hash() but use the default
of %NULL. The behavior is the same, but consistently don't use
g_direct_hash().
2017-11-16 11:49:51 +01:00
Thomas Haller
88a40f960c platform: consider RTNH_F_ONLINK onlink flag for IPv4 routes
The "onlink" flag for IPv4 routes is part of the route ID.
Consider it in nm_platform_ip4_route_cmp().

Also, allow configuring the flag when adding a route.

Note that for IPv6, the onlink flag is still ignored.
Pretty much like kernel does.
2017-11-13 11:35:44 +01:00
Thomas Haller
81778f59f2 platform: track all rtm_flags for routes 2017-11-13 11:35:44 +01:00
Thomas Haller
54cbb321e5 platform: return platform error code from nm_platform_link_set_mtu() 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
42cfcf6f23 platform: downgrade warning about failure to set MTU
Setting the MTU failes under regular conditions, for example when
setting the MTU of a master larger then the MTU of the slaves.

Logging a warning it too alarming.
2017-10-23 17:53:22 +02:00
Thomas Haller
1410c6376e platform: don't hash the compare type in nm_platform_ip4_route_hash_update()
We don't need this extra distinguisher. It makes no sense to ever
compare two routes with a different compare-type.

Also, the number of fields that is hashed already differs between each
compare type. If we have a good hashing algorithm, this already suffices
that the hash value looks largely different.
2017-10-18 13:29:22 +02:00
Thomas Haller
f17a20c568 core: refactor hashing to use reduce calls to siphash24_compress()
This makes for example nm_platform_link_hash_update() by roughly 25%
faster.
2017-10-18 13:29:22 +02:00
Thomas Haller
cfe8546df9 all: extend hash functions with an NMHashState argument
We often want to cascade hashing, meaning, to combine the
outcome of various hash functions in a larger hash.

Instead of having each hash function return a guint hash value,
accept a hash state argument. This saves the overhead of initializing
and completing the intermediate hash states.
It also avoids loosing entropy when we reduce the larger hash state
into the intermediate guint hash value.
2017-10-18 13:29:22 +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
ee76b0979f all: use siphash24 for hashing
siphash24() is wildly used by projects nowadays.

It's certainly slower then our djb hashing that we used before.
But quite likely it's fast enough for us, given how wildly it is
used. I think it would be hard to profile NetworkManager to show
that the performance of hash tables is the issue, be it with
djb or siphash24.

Certainly with siphash24() it's much harder to exploit the hashing
algorithm to cause worst case hash operations (provided that the
seed is kept private). Does this better resistance against a denial
of service matter for us? Probably not, but let's better be safe then
sorry.

Note that systemd's implementation uses a different seed for each hash
table (at least, after the hash table grows to a certain size).
We don't do that and use only one global seed.
2017-10-18 13:27:02 +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
e71f7775a7 platform: fix comparing parent_ifindex in nm_platform_lnk_macsec_cmp() 2017-10-17 15:04:49 +02:00
Thomas Haller
4a2798434e core: introduce NM_HASH_INIT() to initialize hash seed
Introduce a NM_HASH_INIT() function. It makes the places
where we initialize a hash with a certain seed visually clear.

Also, move them from "shared/nm-utils/nm-shared-utils.h" to
"shared/nm-utils/nm-macros-internal.h". We might want to
have NM_HASH_INIT() non-inline (hence, define it in the
source file).
2017-10-13 12:47:55 +02:00
Thomas Haller
37ffc8bae9 platform: support pref option for IPv6 routes (RTA_PREF)
Support IPv6 router preference (RFC4191) in platform code.
2017-10-12 10:38:19 +02:00
Thomas Haller
5b0745e7bd platform: refactor detecting kernel support
We are going to add another parameter to check. Instead of adding multiple
virtual functions, add a NMPlatformKernelSupportFlags flags enum.
2017-10-12 10:38:19 +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
dccf9f3a61 core: avoid compiler warnings related to cleanup attribute
gcc doesn't consider variables with cleanup attribute as unused.
clang does, and warns about them.

In one case, clang is right, in the other one the warning is bogus.

Fix both.
2017-10-05 14:47:38 +02:00
Thomas Haller
91be4c8c3d core: cleanup handling addr_family in NMDevice
- use nm_utils_addr_family_to_char(). It asserts that the input argument
  is either AF_INET or AF_INET6.
- rename variable @family to @addr_family for consistency.
- when logging addr_family for activation-stage, use v4 or v6 instead
  of numeric AF_INET/AF_INET6.
2017-10-02 13:56:00 +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
7cd04ce014 core: inject route list to delete for nm_platform_ip_route_sync()
Whenever we call a platform operation that reads or writes the netlink
socket, there is the possibility that the cache gets updated, as we
receive netlink events.

It is thus racy, if nm_platform_ip_route_sync() *first* adds routes, and
then obtains a list of routes to delete. The correct approach is to
determine which routes to delete first (and keep it in a list
@routes_prune), and pass that list down to nm_platform_ip_route_sync().

Arguably, this doesn't yet solve every race. For example, NMDevice
calls update_ext_ip_config() during ip4_config_merge_and_apply().
That is good, as it resyncs with platform. However, before calling
nm_ip4_config_commit() it calls other platform operations, like
_commit_mtu(). So, the race is still there.
2017-09-26 19:36:51 +02:00
Thomas Haller
9acf80a979 platform: handle route table RT_TABLE_UNSPEC specially
Kernel does not allow to add a route with table 0 (RT_TABLE_UNSPEC). It
effectively is an alias for the main table. We must consider that when
comparing routes sementically.
2017-09-26 19:31:17 +02:00
Thomas Haller
5819988ac7 platform: cleanup logging for adding link
No need for duplicate log lines

  <debug> [1506146476.8462] platform: link: adding tap tap0 owner 107 group -1
  <debug> [1506146476.8462] platform-linux: link: add tap tap0 owner 107 group -1

Merge them.

Also, for consistency change the logging output for adding generic
interfaces in nm_platform_link_add().
2017-09-25 14:49:44 +02:00
Thomas Haller
03e1cc96a5 core: fix handling IPv6 device-route and use correct route metric
Before commit 6698bf58bb, we would rely on
kernel to add the device-route for manual IPv6 routes. We broke that and now
kernel would still add the device-route, however nm_platform_ip_route_sync()
would delete it immediately after.
That is because previously nm_platform_ip_route_sync() would ignore routes
with rtm_protocol RTPRO_KERNEL. Now, it will sync and delete those too.

Fix that by adding the device-route like we do it for IPv4. This also
fixes an actual issue where the automatically added route always had
route-metric 256. Instead, we now use the metric from ipv6.route-metric
setting.

Fixes: 6698bf58bb
2017-09-19 11:49:29 +02:00