Commit graph

749 commits

Author SHA1 Message Date
Lennart Poettering ae2a15bc14 macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the
pointer to NULL. This is useful as an explicit concept of passing
ownership of a memory area between pointers.

This takes inspiration from Rust:

https://doc.rust-lang.org/std/option/enum.Option.html#method.take

and was suggested by Alan Jenkins (@sourcejedi).

It drops ~160 lines of code from our codebase, which makes me like it.
Also, I think it clarifies passing of ownership, and thus helps
readability a bit (at least for the initiated who know the new macro)
2018-03-22 20:21:42 +01:00
Lennart Poettering 57027d0356 dhcp-server: don't propagate erros up the event loop
If we can't send a message this is no reason to completely abort the
event handler.

Issue identified by Nandor Han <nandor.han@ge.com>, Sebastian Reichel
<sebastian.reichel@collabora.co.uk>.

Replaces: #8525
2018-03-21 20:32:52 +01:00
Lennart Poettering cfcbb13583 dhcp-sever: check properly for invalid fds
We generally just compare for negativity, not for equlity to -1, let's
do so here too.
2018-03-21 20:30:29 +01:00
Lennart Poettering c3922c0c1c dhcp_server_handle_message: don't pretend there was a difference between return code 0 or 1
We ignore the difference anyway, hence let's systematically return 0.
2018-03-21 20:29:43 +01:00
Lennart Poettering 6408ba5fa9 dhcp-server: reduce level of indentation a bit
Less indentation is good, let's do that where it's easy.
2018-03-21 20:29:07 +01:00
Lennart Poettering 6e741541ed dhcp-server: introduce log_dhcp_server_errno()
Sometimes we want to print the error number, hence do so properly, and
avoid to use strerror() which is not reentrant.
2018-03-21 20:28:01 +01:00
Lennart Poettering 0f01c1f918 dhcp-server: don't assign sendmsg() return value to "int"
The type is "ssize_t", not "int", let's be accurate about that, as these
types are different on some archs.

Given that we don't actually care about the return value reall, drop
the whole assignment, just check if negative.
2018-03-21 20:27:16 +01:00
Yu Watanabe dace710c4a dhcp4: introduce new option 'duid-only' for ClientIdentifier= (#8350)
This makes users can configure DHCPv4 client with ClientIdentifier=duid-only.
If set so, then DHCP client sends only DUID as the client identifier.
This may not be RFC compliant, but some setups require this.

Closes #7828.
2018-03-12 17:18:07 +01:00
Lennart Poettering 62d74c78b5 coccinelle: add reallocarray() coccinelle script
Let's systematically make use of reallocarray() whereever we invoke
realloc() with a product of two values.
2018-03-02 12:39:07 +01:00
Lennart Poettering 4c9cb12c05 logind: fd 0 is a valid fd 2018-02-26 18:39:32 +01:00
Beniamino Galvani 6afe9046d5 libsystemd-network: fix endianness in ARP BPF filter (#8255)
Commit f11cba7479 ("libsystemd-network: fix unaligned loads (issue #7654)")
changed the way in which the MAC address is read to use native endiannes:

 htobe32(*((uint32_t *)x)  ->  unaligned_read_ne32(x)

This is wrong because loads done with BPF_LD + BPF_ABS are big-endian, as it
can be seen for the ethertype and arp-operation loads above in the
filter.  Also, the same commit changed:

 htobe32(*((unsigned int *)x) -> unaligned_read_be32(x)

in _bind_raw_socket(), which is the correct form.

The commit broke IPv4LL in presence of loops, as the sender now considers its
own packets as conflicting.

Fixes: f11cba7479
2018-02-22 18:09:33 +01:00
Zbigniew Jędrzejewski-Szmek 99f1d3fc50 sd-dhcp6: fix check if serverid is set
Ever since the initial implementation in 631bbe7129,
client_parse_message() was supposed to check that the message contains exactly
one serverid. The check that no more than one is given was implemented
correctly, but the check that at least one is given was not. Simplify the whole
thing by making dhcp6_lease_get_serverid() return an error if the id is not
set, and do not require the arguments to be present if the contents of the id
are not needed.
2018-02-15 10:04:02 +01:00
Zbigniew Jędrzejewski-Szmek 21a9905c7a libsystemd-network: unitialized return value
r will *usually* be set, but for some packets it might not.

CID #1385308.
2018-02-15 09:20:36 +01:00
Zbigniew Jędrzejewski-Szmek 87ac8d998f tree-wide: use "cannot" instead of "can not"
This is the usual spelling, and a bit shorter.
2018-02-08 10:34:52 +01:00
Evgeny Vereshchagin ce691f31aa test-lldp: use memcmp to compare bytes (#8054)
Technically, `data` is a sequence of bytes without a trailing zero,
so the use of `memcmp` seems to be logical here. Besides, this helps get
around a bug that makes `asan` report the false positive mentioned in
 #8052.

Closes #8052.
2018-01-31 09:54:37 +01:00
Patrik Flykt 10a0f27bfd sd-dhcp6-client: Fix DHCPv6 client file descriptor handling
The DHCPv6 client will set its state to DHCP6_STATE_STOPPED if
an error occurs or when receiving an Information Reply DHCPv6
message. Once in DHCP6_STATE_STOPPED, the DHCPv6 client needs
to be restarted by calling sd_dhcp6_client_start().

As of pull request #7796 client_reset() no longer closes the
network socket, thus a call to sd_dhcp6_client_start() needs to
check whether the file descriptor already exists in order not to
create a new one. Likewise, a call to sd_dhcp6_client_unref()
must now close the network socket as client_reset() is not
closing it.

Reported by asavah and Yu Watanabe.
2018-01-23 13:42:12 +01:00
Lennart Poettering 47925d7f32
Merge pull request #7943 from yuwata/fix-chase_symlinks
fs-util: use `_cleanup_close_` attribute
2018-01-22 12:42:24 +01:00
Susant Sahani fb5c821664 networkd: DHCPv6 client allow to configure Rapid Commit (#6930)
The DHCPv6 client can obtain configuration parameters from a
DHCPv6 server through a rapid two-message exchange solicit and reply).
When the rapid commit option is enabled by both the DHCPv6 client and
the DHCPv6 server, the two-message exchange is used, rather than the default
four-method exchange (solicit, advertise, request, and reply). The two-message
exchange provides faster client configuration and is beneficial in environments
in which networks are under a heavy load.

Closes #5845
2018-01-22 17:09:18 +09:00
Yu Watanabe 1eeddba492 sd-dhcp6-client: do not refer uninitialized variable
Fixes CID #1385308.
2018-01-21 19:27:30 +09:00
Susant Sahani 8cdc46e7ba networkd: ignore Static Routes option when Classless Static Routes is given (#7807)
When the DHCP server returns both a Classless Static Routes
option and a Static Routes option, the DHCP client MUST ignore the
Static Routes option.

Closes #7792
2018-01-20 08:42:45 +09:00
Zbigniew Jędrzejewski-Szmek c3de717e51
Merge pull request #7675 from shawnl/unaligned
Issue #7654 (unaligned loads on sparc64)
2018-01-20 10:00:14 +11:00
Yu Watanabe 976fade6c1 dhcp6: fix warnings by clang with -Waddress-of-packed-member
This fixes the following warnings:
```
[194/1521] Compiling C object 'src/libsystemd-network/systemd-network@sta/dhcp6-option.c.o'.
../../git/systemd/src/libsystemd-network/dhcp6-option.c:110:25: warning: taking address of packed member 'id' of class or structure 'ia_na' may result in an unaligned pointer value [-Waddress-of-packed-member]
                iaid = &ia->ia_na.id;
                        ^~~~~~~~~~~~
../../git/systemd/src/libsystemd-network/dhcp6-option.c:115:25: warning: taking address of packed member 'id' of class or structure 'ia_ta' may result in an unaligned pointer value [-Waddress-of-packed-member]
                iaid = &ia->ia_ta.id;
                        ^~~~~~~~~~~~
2 warnings generated.
```
2018-01-17 01:53:03 +09:00
Patrik Flykt ecf07fb748 sd-dhcp6-client: Use offsetof() instead of sizeof()
The slightly modified review comments say that "...in theory
offsetof(DHCP6Option, data) is nicer than sizeof(DHCP6Option)
because the former removes alignment artifacts. In this
specific case there are no alignment whitespaces hence it's
fine, but out of a matter of principle offsetof() is preferred
over sizeof() in cases like this..."
2018-01-15 18:00:33 +02:00
Patrik Flykt 3c03564911 dhcp6: Fix valgrind nitpick about returned test case value
Calling dhcp6_option_parse_address() will always return a value
< 0 on error even though lt_valid remains unset. This is more
than valgrind can safely detect, but let's fix the valgrind
nitpick anyway.

While fixing, use UINT32_MAX instead of ~0 on the same line.
2018-01-15 18:00:33 +02:00
Yu Watanabe 5a45fde547 test: free memory
Fixes #7882.
2018-01-15 23:37:02 +09:00
Lennart Poettering 38edb7674b
Merge pull request #7582 from pfl/dhcp6_prefix_delegation
DHCPv6 prefix delegation
2018-01-15 12:02:37 +01:00
Lennart Poettering dccca82b1a log: minimize includes in log.h
log.h really should only include the bare minimum of other headers, as
it is really pulled into pretty much everything else and already in
itself one of the most basic pieces of code we have.

Let's hence drop inclusion of:

1. sd-id128.h because it's entirely unneeded in current log.h
2. errno.h, dito.
3. sys/signalfd.h which we can replace by a simple struct forward
   declaration
4. process-util.h which was needed for getpid_cached() which we now hide
   in a funciton log_emergency_level() instead, which nicely abstracts
   the details away.
5. sys/socket.h which was needed for struct iovec, but a simple struct
   forward declaration suffices for that too.

Ultimately this actually makes our source tree larger (since users of
the functionality above must now include it themselves, log.h won't do
that for them), but I think it helps to untangle our web of includes a
tiny bit.

(Background: I'd like to isolate the generic bits of src/basic/ enough
so that we can do a git submodule import into casync for it)
2018-01-11 14:44:31 +01:00
Hans-Christian Noren Egtvedt 8a2b193a55 sd-radv: avoid redefinition of struct in6_addr
Both netinet/icmp6.h and linux/in6.h will define struct in6_addr, and in
user space we want to use the netinet/icmp6.h variant.

Fixes build problem:
In file included from src/libsystemd-network/sd-radv.c:23:0:
/home/hegtvedt/work/os/product/sunrise/root/_build/v2/include/linux/in6.h:30:8:
error: redefinition of 'struct in6_addr'
2018-01-10 12:32:03 +01:00
Patrik Flykt de661ccec7 radv: Allow setting MTU in all cases
Setting MTU is allowed in all cases and the MTU value will be
announced in the subsequent Router Advertisements.
2018-01-04 15:22:44 +02:00
Patrik Flykt 34c169c462 radv: Add function to remove prefixes
As DHCPv6 leases may expire at some point, the delegated prefixes
have to be removed. Add a prefix removal function to the Router
Advertisement handling code.
2018-01-04 15:22:44 +02:00
Patrik Flykt d601b56687 radv: Add prefixes with dynamically updated lifetimes
Add a boolean that indicates whether the prefixes will always exist
or if they will time out after the assigned valid lifetime. In the
latter case calculate the expiry times for both preferred and valid
lifetimes for the prefixes, and decrease the remaining lifetimes
each time when a Router Advertisement is sent.

Should the prefix be updated, re-calculate the prefix lifetime. When
updating, update the existing entry, if any, with the lifetimes of
the added entry as the existing entry has its lifetimes set
according to its previously calculated expiry times.
2018-01-04 15:22:44 +02:00
Patrik Flykt 652bf04254 dhcp6: Add function to iterate and fetch delegated prefixes
Add a function for fetching the next delegated prefix and another
one to reset the iteration to the first prefix.
2018-01-04 15:22:44 +02:00
Patrik Flykt 819c56f6fa dhpc6: Add PD and PD Prefix tests
Add tests for IA PD and PD Prefix options.
2018-01-04 15:22:44 +02:00
Patrik Flykt b47fb949b3 dhcp6: Compute the minimum lifetimes for T1 and T2
Compute one set of minimum lifetimes for T1 and T2, i.e. the smaller
ones assigned to IA NA and IA PD. The lifetimes should be the same,
see RFC 7550 for details.
2018-01-04 15:22:44 +02:00
Patrik Flykt 7c3de8f8cf dhcp6: Add functionality to request DHCPv6 IA PD
Add a function to request IA Prefix Delegation when the DHCPv6
client is started and PD options to DHCPv6 messages.
2018-01-04 15:22:44 +02:00
Patrik Flykt c77e3db19e dhcp6: Add function appending an IA PD to the DHCP6 message
Add function that appends an IA PD option and any number of IA PD
Prefix options.
2018-01-04 15:22:44 +02:00
Patrik Flykt 69b4399748 dhcp6: Ensure IAID is applied to both IA NA and IA PD
Update the code to set IAID for both IA NA and IA PD options.
2018-01-04 15:22:44 +02:00
Patrik Flykt dce6563fc6 dhcp6: Handle a received IA PD option
Parse the received IA PD option and verify its IAID.
2018-01-04 15:22:44 +02:00
Patrik Flykt f8ad4dd45d dhcp6: Parse IA PD and PD Prefix options
Parse IA PD options and the prefixes in one or more PD Prefix
options. As the PD option contains identical data as the IA NA
option, re-use the same general data structures and sub-option
parsing logic. Similar to IA NA addresses, PD and associated
prefixes are stored in the address list of the IA PD lease.

An IA sub-option Status code will affect the IA NA and IA PD
option in question and cause those options to be ignored. A
Status code option in an IA Address or IA PD Prefix option
affects only that IA Address or Prefix.
2018-01-04 15:22:44 +02:00
Patrik Flykt df296124df test-dhcp6-client: Add Status option test
Add Status option tests to verify that options with differently
placed Status options are processed correctly.
2018-01-04 15:22:43 +02:00
Patrik Flykt 0dfe2a4b56 dhcp6: Fix IA Address option parsing
Factor out IA Address option parsing and fix it so that all conditions
are checked before a new address is allocated and added to the address
list. Note also that the IA Address option can contain a nested Status
option. If the status in anything else than zero, the DHCPv6 server is
communicating an error condition and the address cannot be used.
Status option nesting is clarified in RFC 7550, Section 4.1.

The IA Address option is included as a typedef so that the lifetimes
can be inspected before allocating a new address and the option length
needed is easily available.
2018-01-04 15:22:43 +02:00
Patrik Flykt c6b4f32a50 dhcp6: Add function for DHCPv6 Status option
Factor out code to parse a DHCPv6 Status option using a common
function.
2018-01-04 15:22:43 +02:00
Patrik Flykt 3bc424a3cc dhcp6: Sanitize DHCPv6 IA option parsing
Sanitize code for parsing DHCPv6 IA NA and TA options and their
nested Status options so that the options can be fully and
properly ignored should they not be conformant to the specification.

Do this by defining a proper DHCP6Option structure and sending that
structure to the parsing function. The parsing function will then
not manipulate either any option data pointers or their lengths in
order to iterate over the current option. Needless to say, this
affects a few files including the test program.
2018-01-04 15:22:43 +02:00
Patrik Flykt e0026dcbd2 dhcp6: Name structs in DHCP6IA
Name structs containing IA NA with ID and T1 and T2 lifetimes and
IA TA containing only the ID so that the structs can be expressed
properly.
2018-01-04 15:22:43 +02:00
Patrik Flykt 7ac6c26a22 dhcp6: Fix DHCPv6 client file descriptor and event handling (#7796)
Close DHCPv6 client socket file descriptor when
sd_dhcp6_client_stop() is called and not when client_reset() is
called. If left in client_reset(), any internal temporary stopping
of the DHCPv6 client with client_stop() will call client_reset()
after which the DHCPv6 client will not be able to receive any further
DHCPv6 messages.

Similarly, client_start() needs to enable events for the DHCPv6
socket file descriptor since a call to client_stop() will call
client_reset() which will remove it from the main loop. Events should
be turned off when no DHCPv6 messages are expected.
2018-01-04 11:02:52 +01:00
Lennart Poettering 5022f08a23 core,udev,networkd: add ConditionKernelVersion=
This adds a simple condition/assert/match to the service manager, to
udev's .link handling and to networkd, for matching the kernel version
string.

In this version we only do fnmatch() based globbing, but we might want
to extend that to version comparisons later on, if we like, by slightly
extending the syntax with ">=", "<=", ">", "<" and "==" expressions.
2017-12-26 17:39:44 +01:00
Shawn Landden f11cba7479 libsystemd-network: fix unaligned loads (issue #7654)
should fix https://github.com/systemd/systemd/issues/7654

Also fix up mistaken endianness swap. BPF is native endianness.
2017-12-18 13:02:07 -08:00
Lennart Poettering 0d53667334 tree-wide: use __fsetlocking() instead of fxyz_unlocked()
Let's replace usage of fputc_unlocked() and friends by __fsetlocking(f,
FSETLOCKING_BYCALLER). This turns off locking for the entire FILE*,
instead of doing individual per-call decision whether to use normal
calls or _unlocked() calls.

This has various benefits:

1. It's easier to read and easier not to forget

2. It's more comprehensive, as fprintf() and friends are covered too
   (as these functions have no _unlocked() counterpart)

3. Philosophically, it's a bit more correct, because it's more a
   property of the file handle really whether we ever pass it on to another
   thread, not of the operations we then apply to it.

This patch reworks all pieces of codes that so far used fxyz_unlocked()
calls to use __fsetlocking() instead. It also reworks all places that
use open_memstream(), i.e. use stdio FILE* for string manipulations.

Note that this in some way a revert of 4b61c87511.
2017-12-14 10:42:25 +01:00
Thomas Haller dbcb4a900e tree-wide: use STRLEN() to allocate buffer of constant size
Using strlen() to declare a buffer results in a variable-length array,
even if the compiler likely optimizes it to be a compile time constant.

When building with -Wvla, certain versions of gcc complain about such
buffers. Compiling with -Wvla has the advantage of preventing variably
length array, which defeat static asserts that are implemented by
declaring an array of negative length.
2017-12-13 11:12:19 +01:00
Zbigniew Jędrzejewski-Szmek 224b0e7ad0 Add set/hashmap helpers for non-trivial freeing and use where straighforward
A macro is needed because otherwise we couldn't ensure type safety.
Some simple tests are included.
No functional change intended.
2017-11-28 21:30:30 +01:00