Commit Graph

30 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
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 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
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 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 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
Zbigniew Jędrzejewski-Szmek 53e1b68390 Add SPDX license identifiers to source files under the LGPL
This follows what the kernel is doing, c.f.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
2017-11-19 19:08:15 +01:00
Stefan Agner 8006aa32ee sd-dhcp6-client: Implement FQDN Option (#7309)
Implement DHCPv6 option to exchange information about the Fully
Qualified Domain Name (FQDN) according to RFC 4704.

The RFC 4704 describes two models of operations in section 3,
currently only the second model is supported (DHCPv6 server
updates both the AAAA and the PTR RRs).

The existing DHCP Section Options SendHostname and Hostname are
sent as FQDN to the server. According to section 4.2 sending
only parts of its FQDN is allowed.

Fixes #4682.
2017-11-16 10:07:07 +01:00
Evgeny Vereshchagin 419eaa8f8d sd-network: fix memleak in dhcp6_option_parse_domainname (#5114)
The simplest way to reproduce:
```diff
diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c
index bd289fa..7b0a5ef 100644
--- a/src/libsystemd-network/test-dhcp6-client.c
+++ b/src/libsystemd-network/test-dhcp6-client.c
@@ -168,7 +168,7 @@ static uint8_t msg_advertise[198] = {
         0x00, 0x17, 0x00, 0x10, 0x20, 0x01, 0x0d, 0xb8,
         0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x0b,
-        0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
+        0x01, 0x6c, 0x01, 0x62, 0x00, 0x0a, 0x6e, 0x74,
         0x72, 0x61, 0x00, 0x00, 0x1f, 0x00, 0x10, 0x20,
         0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
@@ -338,9 +338,7 @@ static int test_advertise_option(sd_event *e) {
         assert_se(!memcmp(addrs, &msg_advertise[124], r * 16));

         r = sd_dhcp6_lease_get_domains(lease, &domains);
-        assert_se(r == 1);
-        assert_se(!strcmp("lab.intra", domains[0]));
-        assert_se(domains[1] == NULL);
+        assert_se(r == -ENOENT);

         r = sd_dhcp6_lease_get_ntp_addrs(lease, &addrs);
         assert_se(r == 1);
```

Fixes:
```
=================================================================
==15043==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7f13c8564160 in strdup (/lib64/libasan.so.3+0x5a160)
    #1 0x7f13c7caaf69 in strv_extend src/basic/strv.c:552
    #2 0x55f775787230 in dhcp6_option_parse_domainname src/libsystemd-network/dhcp6-option.c:399
    #3 0x55f775788b96 in dhcp6_lease_set_domains src/libsystemd-network/sd-dhcp6-lease.c:225
    #4 0x55f775774b95 in test_advertise_option src/libsystemd-network/test-dhcp6-client.c:287
    #5 0x55f77577883e in main src/libsystemd-network/test-dhcp6-client.c:759
    #6 0x7f13c7589400 in __libc_start_main (/lib64/libc.so.6+0x20400)

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7f13c8564160 in strdup (/lib64/libasan.so.3+0x5a160)
    #1 0x7f13c7caaf69 in strv_extend src/basic/strv.c:552
    #2 0x55f775787230 in dhcp6_option_parse_domainname src/libsystemd-network/dhcp6-option.c:399
    #3 0x55f775788b96 in dhcp6_lease_set_domains src/libsystemd-network/sd-dhcp6-lease.c:225
    #4 0x55f775781348 in client_parse_message src/libsystemd-network/sd-dhcp6-client.c:807
    #5 0x55f775781ba2 in client_receive_advertise src/libsystemd-network/sd-dhcp6-client.c:895
    #6 0x55f775782453 in client_receive_message src/libsystemd-network/sd-dhcp6-client.c:994
    #7 0x7f13c7e447f4 in source_dispatch src/libsystemd/sd-event/sd-event.c:2268
    #8 0x7f13c7e471b0 in sd_event_dispatch src/libsystemd/sd-event/sd-event.c:2627
    #9 0x7f13c7e47ab3 in sd_event_run src/libsystemd/sd-event/sd-event.c:2686
    #10 0x7f13c7e47c21 in sd_event_loop src/libsystemd/sd-event/sd-event.c:2706
    #11 0x55f77577863c in test_client_solicit src/libsystemd-network/test-dhcp6-client.c:737
    #12 0x55f77577884b in main src/libsystemd-network/test-dhcp6-client.c:760
    #13 0x7f13c7589400 in __libc_start_main (/lib64/libc.so.6+0x20400)

SUMMARY: AddressSanitizer: 8 byte(s) leaked in 2 allocation(s).
```
2017-01-23 21:11:59 -05:00
Lennart Poettering 91ba5ac7d0 Merge pull request #2589 from keszybz/resolve-tool-2
Better support of OPENPGPKEY, CAA, TLSA packets and tests
2016-02-13 11:15:41 +01:00
Zbigniew Jędrzejewski-Szmek 75f32f047c Add memcpy_safe
ISO/IEC 9899:1999 §7.21.1/2 says:
Where an argument declared as size_t n specifies the length of the array
for a function, n can have the value zero on a call to that
function. Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call
shall still have valid values, as described in 7.1.4.

In base64_append_width memcpy was called as memcpy(x, NULL, 0).  GCC 4.9
started making use of this and assumes This worked fine under -O0, but
does something strange under -O3.

This patch fixes a bug in base64_append_width(), fixes a possible bug in
journal_file_append_entry_internal(), and makes use of the new function
to simplify the code in other places.
2016-02-11 13:07:02 -05:00
Daniel Mack b26fa1a2fb tree-wide: remove Emacs lines from all files
This should be handled fine now by .dir-locals.el, so need to carry that
stuff in every file.
2016-02-10 13:41:57 +01:00
Beniamino Galvani 2c1ab8ca9b dhcp: make DHCP6_OPTION_* enum public
libsystemd-network provides the public function
sd_dhcp6_client_set_request_option() to enable the request of a given
DHCP option. However the enum defining such options is defined in the
internal header dhcp6-protocol.h. Move the enum definition to the
public header sd-dhcp6-client.h and properly namespace values.
2016-01-20 17:25:16 +01:00
Lennart Poettering 422baca0f2 dns-domain: rework dns_label_escape() to not imply memory allocation
The new dns_label_escape() call now operates on a buffer passed in,
similar to dns_label_unescape(). This should make decoding a bit faster,
and nicer.
2015-11-25 22:00:07 +01:00
Thomas Hindoe Paaboel Andersen cf0fbc49e6 tree-wide: sort includes
Sort the includes accoding to the new coding style.
2015-11-16 22:09:36 +01:00
Tom Gundersen 93f660da98 sd-dhcp6-client: fix assert in options parsing 2015-10-30 12:32:49 +01:00
Lennart Poettering b5efdb8af4 util-lib: split out allocation calls into alloc-util.[ch] 2015-10-27 13:45:53 +01:00
Lennart Poettering 52efd56a63 tree-wide: we place the opening bracket on the same line as the function name
Let's do this everywhere the same way.
2015-08-27 17:48:24 +02:00
David Herrmann a00458421d sd-dhcp6: fix domainname memleak
strv_extend() does not consume the passed entry, hence, we must properly
free it. Furthermore, we should *not* use strv_consume() as we do greedy
allocations on 'ret'; and greedy-allocations should only be used for short
lived objects or caches.

Fix the domainname parser to properly free temporary storage when done.
2015-08-26 12:37:56 +02:00
Patrik Flykt f96ccab7e0 dhcp6-option: Add helper function for uncompressed domain names
Add a helper function containing a modified version of dns_packet_read_name()
that does not use DnsPacket to extract a string array of domain names from
the provided option data. The domain names are stored uncompressed as defined
in Section 8. of RFC 3315.
2015-08-21 11:23:21 +03:00
Patrik Flykt b553817ccf dhcp6-option: Add helper function for fetching IPv6 addresses
Add a helper function that extracts a block of IPv6 addresses from
the provided option data.
2015-08-21 11:23:21 +03:00
Tom Gundersen c962cb68d5 libsystemd-network: don't use unaligned helpers in _packed_ structs
The compiler will do this for us.
2014-11-05 16:54:22 +01:00
Tom Gundersen 4903a73c37 dhcp6: use unaligned read/write helpers 2014-11-01 15:36:29 +01:00
Patrik Flykt ee3a5027f7 sd-dhcp6-lease: Name the structure containing IAADDR data
With this change the DHCP6_OPTION_IAADDR_LEN define can be removed in
favor of using sizeof(). Using the name of the struct and sizeof()
makes it clearer how much and what data is being copied from the
DHCPv6 message.
2014-10-09 14:57:53 +03:00
Patrik Flykt ed6ee21953 sd-dhcp6-client: Implement Rapid Commit
Add a Rapid Commit option to Solicit messages and expect a Reply to
be received instead of an Advertise. When receiving a DHCPv6 message
from the server in state Solicit, continue testing whether the
message is a Reply. Ease up the message type checking, it's not fatal
if the message is of a wrong type.

Add helper functions to set/get the rapid commit of a lease. See
RFC 3315, sections 17., 17.1.2., 17.1.4. and 18.1.8.
2014-06-26 16:10:47 +03:00
Patrik Flykt c6affce874 sd-dhcp6-client: Add IA Address option parsing
Add functionality to parse DHCPv6 Identity Association for
Non-temporary (IA_NA) and Temporary Addresses (IA_TA) options.
Both of them contain one or more IA Address (IAADDR) options
and optinally a status code option. Only the IA_NA option
contains lease lifetimes. See RFC 3315, sections 22.4., 22.5.,
22.6., 22.13. and appendix B. for details. If the lease
timeouts are not set, use the ones recommended for servers in
section 22.4.

Factor out common code in the form of an option header parsing
helper function.
2014-06-19 15:44:44 +03:00
Patrik Flykt f12ed3bf0b sd-dhcp6-client: Add basic DHCPv6 option handling
Add option appending and parsing. DHCPv6 options are not aligned, thus
the option handling code must be able to handle options starting at
any byte boundary.

Add a test case for the basic option handling.
2014-06-19 15:44:43 +03:00