Commit graph

218 commits

Author SHA1 Message Date
Lennart Poettering a114066685 sd-network: fix up assertion chaos
assert_return() should only be used to validate user-facing parameters and
state, assert() should be used for checking our own internal state and
parameters.
2016-05-26 15:34:42 +02:00
Lennart Poettering 2f8e763376 sd-network: rename "index" field of the various clients to "ifindex"
A field "index" is not particularly precise and also might conflict with libc's
index() function definition. Also, pretty much everywhere else we call this
concept "ifindex", including in networkd, the primary user of these libraries.
Hence, let's fix this up and call this "ifindex" everywhere here too.
2016-05-26 15:34:42 +02:00
Lennart Poettering 16f0b479ca sd-dhcp: shorten NUL initialization a bit 2016-05-26 15:34:42 +02:00
Lennart Poettering 004845d18e sd-network: unify packet processing logic a bit
Let's always check for errno being EAGAIN/EINTR the same way, and always log if
we receive weirdly short packets.
2016-05-26 15:34:41 +02:00
Tom Gundersen cf447cb62d libsystemd-network: use recv(..., 0) instead of read(...) (#3317)
According to recv(2) these should be the same, but that is not true.
Passing a buffer of length 0 to read is defined to be a noop according
to read(2), but passing a buffer of length 0 to recv will discard the
pending pacet.

We can easily hit this as we allocate our buffer size depending on
the size of the incoming packet (using FIONREAD). As pointed out in
issue #3299 simply sending an empty UDP packet to the DHCP client
port will trigger a busy loop in networkd as we are polling on the
socket but never discarding the empty packet.

This reverts ad5ae47a0d but fixes the
same issue.
2016-05-22 00:00:32 +03:00
sadag ad5ae47a0d sd-dhcp-client: fix busy loop reading zero sized DHCP UDP packets. (#3299) 2016-05-20 14:26:27 +03:00
Zbigniew Jędrzejewski-Szmek d7df2fd317 dh-dhcp{,6}-client: change the semantics of DUID setting
Both versions of the code are changed to allow the caller to override
DUID using simple rules: duid type and value may be specified, in
which case the caller is responsible to providing the contents,
or just duid type may be specified as DUID_TYPE_EN, in which case we
we fill in the values. In the future more support for other types may
be added, e.g. DUID_TYPE_LLT.

There still remains and ugly discrepancy between dhcp4 and dhcp6 code:
dhcp6 has sd_dhcp6_client_set_duid and sd_dhcp6_client_set_iaid and
requires client->state to be DHCP6_STATE_STOPPED, while dhcp4 has
sd_dhcp_client_set_iaid_duid and will reconfigure the client if it
is not stopped. This commit doesn't touch that part.

This addresses #3127 § 2.
2016-05-03 12:12:04 -04:00
Zbigniew Jędrzejewski-Szmek f7a92d1a7e sd-dhcp: change uint8_t *duid to const void* 2016-05-03 12:08:56 -04:00
Zbigniew Jędrzejewski-Szmek 4b558378a7 sd-dhcp{,6}-client: use standard indentation for functions args 2016-05-03 11:52:44 -04:00
Vinay Kulkarni 413708d106 DHCP DUID, IAID configuration options 2016-03-30 16:33:55 -07:00
Zbigniew Jędrzejewski-Szmek afec45395f Revert "DHCP DUID and IAID configurability" 2016-03-21 18:24:24 -04:00
Vinay Kulkarni c83321e6d4 DHCP DUID and IAID configurability 2016-03-09 21:58:44 -08:00
Thomas Haller 3587161ade core: avoid compiler warning when compiling with -fexceptions
Initialize auto variables with cleanup attribute, otherwise we
get a compiler warning with -fexceptions.

    ./configure CFLAGS='-Wmaybe-uninitialized -fexceptions -O2'
2016-02-27 13:40:50 +01:00
Lennart Poettering 32d2064523 libsystemd-network: sd-event uses 64bit priorities, expose them in the APIs as such 2016-02-20 22:42:29 +01:00
Lennart Poettering ccf8635435 libsystemd-network: don't abbreviate "callback" as "cb" needlessly
This isn't an excercise in creating APIs that are hard to understand, hence
let's call a callback a callback.
2016-02-20 22:42:29 +01:00
Lennart Poettering 4edc2c9b6b networkd: FIONREAD is not reliable on some sockets
Fixes: #2457
2016-02-16 13:06:55 +01:00
Beniamino Galvani 8ecdcb5525 dhcp: assert the success of sd_event_now()
The function must never fail.
2016-02-15 16:20:55 +01:00
Beniamino Galvani 1d1a3e0afb dhcp: delay restarts after NAKs
The server might answer to a DHCPREQUEST with a NAK and currently the
client restarts the configuration process immediately.  It was
observed that this can easily generate loops in which the network is
flooded with DISCOVER,OFFER,REQUEST,NAK sequences.

RFC 2131 only states that "if the client receives a DHCPNAK message,
the client restarts the configuration process" without further
details.

Add a delay with exponential backoff between retries after NAKs to
limit the number of requests and cap the delay to 30 minutes.
2016-02-02 15:23:34 +01:00
Beniamino Galvani 22805d9207 dhcp: make DHCP_OPTION_* enum public
libsystemd-network provides the public function
sd_dhcp_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 dhcp-protocol.h. Move the enum definition to the
public header sd-dhcp-client.h and properly namespace values.
2016-01-20 17:25:16 +01:00
Lennart Poettering 3cd03457bd util-lib: update dns_name_to_wire_format() to optionally generate DNSSEC canonical names
We'll need this later when putting together RR serializations to
checksum.
2015-12-02 22:50:11 +01:00
Lennart Poettering 4afd3348c7 tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.

With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.

The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).

This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.

Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:

       #define _cleanup_(function) __attribute__((cleanup(function)))

Or similar, to make the gcc feature easier to use.

Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.

See #2008.
2015-11-27 19:19:36 +01:00
Martin Pitt 827661914a Merge pull request #2029 from teg/network-fixes
Network fixes
2015-11-27 10:20:18 +01:00
Lennart Poettering dc477e7385 dns-domain: simplify dns_name_is_root() and dns_name_is_single_label()
Let's change the return value to bool. If we encounter an error while
parsing, return "false" instead of the actual parsing error, after all
the specified hostname does not qualify for what the function is
supposed to test.

Dealing with the additional error codes was always cumbersome, and
easily misused, like for example in the DHCP code.

Let's also rename the functions from dns_name_root() to
dns_name_is_root(), to indicate that this function checks something and
returns a bool. Similar for dns_name_is_signal_label().
2015-11-25 22:00:07 +01:00
Tom Gundersen f693e9b38f sd-dhcp: parse error message in DECLINE or NAK
If a client sends a DECLINE or a server sends a NAK, they can include
a string with a message to explain the error. Parse this and print it
at debug level.
2015-11-25 18:30:31 +01:00
Tom Gundersen 0d43d2fcb7 libsystemd-network: clean up recv functions 2015-11-25 18:30:31 +01:00
Beniamino Galvani 23873e25aa libsystemd-network: add support for "Client FQDN" DHCP option
This adds support for the Client Fully Qualified Domain Name (FQDN)
option [RFC 4702] to libsystemd-network. The option can be used to
exchange information about a DHCPv4 client's fully qualified domain
name and about responsibility for updating the DNS RR related to the
client's address assignment.

Other popular DHCP clients (dhclient, dhcpcd) support this option and
it would be useful to have it in networkd too.
2015-11-17 15:06:01 +01:00
Jan Engelhardt a8eaaee72a doc: correct orthography, word forms and missing/extraneous words 2015-11-06 13:45:21 +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 07630cea1f util-lib: split our string related calls from util.[ch] into its own file string-util.[ch]
There are more than enough calls doing string manipulations to deserve
its own files, hence do something about it.

This patch also sorts the #include blocks of all files that needed to be
updated, according to the sorting suggestions from CODING_STYLE. Since
pretty much every file needs our string manipulation functions this
effectively means that most files have sorted #include blocks now.

Also touches a few unrelated include files.
2015-10-24 23:05:02 +02:00
Tom Gundersen ed19c567e5 libsystemd-network: limit accuracy of timestamps in logging
We don't care about timestamps down to the last usec, round to the closest sec
as that will be plenty for debugging purposes.
2015-10-11 15:04:15 +02:00
David Herrmann 0374814250 sd-dhcp: fix namespacing
Prefix all constants with SD_DHCP_CLIENT_* to avoid namespacing
conflicts.
2015-09-22 14:46:21 +02:00
Lennart Poettering fbcd420aa4 dhcp: don't underflow in lease time calculations
Don't underflow when calculating lease time.
2015-08-27 01:59:43 +02:00
Lennart Poettering 4b7b5abb78 dhcp: NTP servers should be requested by networkd but not implicitly by sd-dhcp
The library so far always requested the NTP servers. This might be
unnecessary in some uses, hence let's move the request into networkd
instead.
2015-08-27 01:17:30 +02:00
Lennart Poettering 0339cd7707 dhcp: clean up dhcp4 lease object
a) drop handling of obsolete or unused DHCP options time_offset,
   mtu_aging_timeout, policy filter, mdr, ttl, ip forwarding settings.
   Should this become useful one day we can readd support for this.

b) For subnet mask and broadcast it is not always clear whether 0 or
   255.255.255.255 might be valid, hence maintain a boolean indicating
   validity next to it.

c) serialize/deserialize broadcast address, lifetime, T1 and T2 together
   with the rest of the fields in dhcp_lease_save() and
   dhcp_lease_load().

d) consistently return ENODATA from getter functions for data that is
   missing in the lease.

e) add missing getter calls for broadcast, lifetime, T1, T2.

f) when decoding DHCP options, generate debug messages on parse
   failures, but try to proceed if possible.

g) Similar, when deserializing a lease in dhcp_lease_load(), make sure
   we deal nicely with unparsable fields, to provide upgrade compat.

h) fix some memory allocations
2015-08-27 01:05:13 +02:00
Lennart Poettering 3733eec3e2 dhcp: stop using refcnt.h
No need to invole atomic ops in single-threaded APIs, let's simplify
this.
2015-08-26 21:05:53 +02:00
David Herrmann e6b18ffaea sd-dhcp: don't randomly ref objects
In our API design, getter-functions don't ref objects. Calls like
foo_get_bar() will not ref 'bar'. We never do that and there is no real
reason to do it in single threaded APIs. If you need a ref-count, you
better take it yourself *BEFORE* doing anything else on the parent object
(as this might invalidate your pointer).

Right now, sd_dhcp?_get_lease() refs the lease it returns. A lot of
code-paths in systemd do not expect this and thus leak the lease
reference. Fix this by changing the API to not ref returned objects.
2015-08-26 12:30:56 +02:00
Thomas Hindoe Paaboel Andersen 144c348883 sd-dhcp-client: readd deleted error check
Seems to have been removed by mistake in:
9021bb9f93
2015-06-17 19:11:18 +02:00
Lennart Poettering 2a1288ff89 util: introduce CMSG_FOREACH() macro and make use of it everywhere
It's only marginally shorter then the usual for() loop, but certainly
more readable.
2015-06-10 19:29:47 +02:00
Lennart Poettering dd168445f8 dhcp: yes, infiniband has the larger MAC address length, but let the compuler figure that out... 2015-04-12 20:15:08 +02:00
Ronny Chevalier 3df3e884ae shared: add random-util.[ch] 2015-04-11 00:11:13 +02:00
Lennart Poettering 3dc055541c Revert "sd-dhcp-client: fix strict aliasing issue"
This reverts commit 6ec8e7c763.

This doesn't fix any issues, just makes the code harder to read.
2015-04-10 19:04:43 +02:00
Shawn Landden 6ec8e7c763 sd-dhcp-client: fix strict aliasing issue 2015-03-14 14:50:45 +01:00
Thomas Hindoe Paaboel Andersen 2eec67acbb remove unused includes
This patch removes includes that are not used. The removals were found with
include-what-you-use which checks if any of the symbols from a header is
in use.
2015-02-23 23:53:42 +01:00
Lennart Poettering a38d99451f Revert "tree-wide: Always use recvmsg with MSG_CMSG_CLOEXEC"
This reverts commit d6d810fbf8.

It's apparently not OK to pass MSG_CMSG_CLOEXEC to recvmsg() of raw
sockets.
2015-02-12 11:45:39 +01:00
Cristian Rodríguez d6d810fbf8 tree-wide: Always use recvmsg with MSG_CMSG_CLOEXEC 2015-02-10 21:20:16 +01:00
Tom Gundersen 5bac523593 sd-dhcp-client: use RFC4361-complient ClientID by default
In addition to the benefits listed in the RFC, this allows DHCP to work also in
case several interfaces share the same MAC address on the same link (IPVLAN).

Note that this will make the ClientID (so probably the assigned IP address)
change on upgrades. If it is desired to avoid that we would have to remember and
write back the ID (which the library supports, but networkd currently does not).
2015-01-22 21:29:42 +01:00
Zbigniew Jędrzejewski-Szmek 1fa2f38f0f Assorted format fixes
Types used for pids and uids in various interfaces are unpredictable.
Too bad.
2015-01-22 01:14:52 -05:00
Tom Gundersen f0c4b1c3fd refcnt: refcnt is unsigned, fix comparisons
This does not make a difference, but the code was confusing.
2015-01-13 23:03:11 +01:00
Veres Lajos f131770b14 tree-wide: spelling fixes
https://github.com/vlajos/misspell_fixer

b6fdeb618c
Thanks to Torstein Husebo <torstein@huseboe.net>.
2014-12-30 20:07:04 -05:00
Tom Gundersen 0411760af1 sd-dhcp-client: log if we fail to set up lease timers 2014-12-11 14:43:09 +01:00
Dan Williams e37f74a6d5 sd-dhcp-lease: load/save client ID
The lease is usually tied to the client ID, so users of the
lease may want to know what client ID it was acquired with.
2014-11-19 00:58:51 +01:00
Dan Williams ba6c0fd630 sd-dhcp-client: allow getting/setting the client ID
The client identifier can be in many different formats, not just
the one that systemd creates from the Ethernet MAC address.  Non-
ethernet interfaces may have different client IDs formats.  Users
may also have custom client IDs that the wish to use to preserve
lease options delivered by servers configured with the existing
client ID.
2014-11-19 00:53:24 +01:00
Dan Williams d8d74ef06f sd-dhcp-client: fix REBOOT state handling
client->secs wasn't getting set in the REBOOT state, causing
an assertion.  REBOOT should work the same way as INIT, per
RFC 2131:

 secs   2  Filled in by client, seconds elapsed since client
           began address acquisition or renewal process.

REBOOT is necessary because some DHCP servers (eg on
home routers) do not hand back the same IP address unless the
'ciaddr' field is filled with that address, which DISCOVER
cannot do per the RFCs.  This leads to multiple leases
on machine reboot or DHCP client restart.
2014-11-18 18:25:43 +01:00
Lennart Poettering 356779df90 sd-event: rename sd_event_source_set_name() to sd_event_source_get_name()
To mirror the recent name change of the concept for sd_bus objects,
follow the same logic for sd_event_source objects, too.
2014-11-04 18:09:19 +01:00
Dan Williams affaa94fc3 sd-dhcp-client: clean up raw socket sd_event_source when creating new UDP socket
The raw socket sd_event_source used for DHCP server solicitations
was simply dropped on the floor when creating the new UDP socket
after a lease has been acquired.  Clean it up properly so we're
not still listening and responding to events on it.
2014-10-31 09:26:27 +01:00
Dan Williams 76253e73f9 sd-dhcp-client: support non-Ethernet hardware addresses
Like Infiniband.  See RFC 4390 section 2.1 for details on DHCP
and Infiniband; chaddr is zeroed, hlen is set to 0, and htype
is set to ARPHRD_INFINIBAND because IB hardware addresses
are 20 bytes in length.
2014-10-11 12:15:54 +02:00
Ruben Kerkhof 6ff8806e1d Fix a few typos in log messages 2014-08-29 09:28:19 -04:00
Tom Gundersen 9021bb9f93 sd-event: name event sources used in libraries
This should help in debugging failing event sources.
2014-08-28 21:19:17 +02:00
Lennart Poettering db73295acc util: never use ether_ntoa(), since it formats with %x, not %02x, which makes ethernet addresses look funny 2014-08-15 13:19:03 +02:00
Tom Gundersen 324f818781 sd-dhcp-client: allow the max dhcp message size to be set to the MTU of the link 2014-08-01 16:34:50 +02:00
Tom Gundersen 615c1467c8 sd-dhcp-client: add support for FORCERENEW
This partially implements RFC3203. Note that we are not fully compliant as we do not
support authentication.
2014-07-28 10:44:51 +02:00
Tom Gundersen 0af03ba57f sd-dhcp-client: listen on UDP socket as soon as a lease is acquired
This is necessary in order to listen for FORCERENEW events.
2014-07-28 10:44:51 +02:00
Tom Gundersen 2d2349cc3e sd-dhcp-client: drop event DHCP_EVENT_NO_LEASE
Keep this internal to the client and simply restart it when NAK is receieved, as
per the RFC.
2014-07-28 10:44:51 +02:00
Tom Gundersen 9fbc25232c sd-dhcp-client: only verify UDP messages
RAW messages are verified by the BPF in the kernel.
2014-07-28 10:44:51 +02:00
Tom Gundersen fa94c34b08 libsystemd-network: use CLOCK_BOOTTIME instead of CLOCK_MONOTONIC where possible
The timeouts in the networking library (DHCP lease timeouts and similar) should not be affected
by suspend. In the cases where CLOCK_BOOTTIME is not implemented, it is still safe to fallback to
CLOCK_MONOTONIC, as the consumers of the library (i.e., networkd) _should_ renew the leases when
coming out of suspend.
2014-07-24 19:02:58 +02:00
Tom Gundersen f5de5b0020 sd-dhcp-client: make request broadcasts opt-in
It appears there is no good way to decide whether or not broadcasts should be enabled,
there is hardware that must have broadcast, and there are networks that only allow
unicast. So we give up and make this configurable.

By default, unicast is used, but if the kernel were to inform us abotu certain
interfaces requiring broadcast, we could change this to opt-in by default in
those cases.
2014-07-15 18:55:31 +02:00
Susant Sahani edb85f0d8d networkd: dhcp add vendor class indentifier option 60
Vendor Class Identifier be used by DHCP clients to identify
their vendor type and configuration. When using this option,
vendors can define their own specific identifier values, such
as to convey a particular hardware or operating system
configuration or other identifying information.

Vendor-specified DHCP options—features that let administrators assign
separate options to clients with similar configuration requirements.
For example, if DHCP-aware clients for example we want to separate
different gateway and option for different set of people
(dev/test/hr/finance) in a org or devices for example web/database
servers or let's say in a embedded device etc and require a different
default gateway or DNS server than the rest of clients.
2014-07-14 11:39:20 +02:00
Michal Sekletar 58587a7a0c dhcp-network: add check for DHCP.chaddr
Check that received DHCP packets actually include our MAC address in
chaddr field. BPF interpreter has 32 bit wide registers but MAC address
is 48 bits long so we have to do check in two steps.
2014-07-07 12:17:55 +02:00
Eugene Yakubovich 4cc7a82c94 networkd: send hostname to dhcp server
Send hostname (option 12) in DISCOVER and REQUEST messages so the
DHCP server could use it to register with dynamic DNS and such.

To opt-out of this behaviour set SendHostname to false in [DHCP]
section of .network file

[tomegun: rebased, made sure a failing set_hostname is a noop and moved
          config from DHCPv4 to DHCP]
2014-07-01 22:02:25 +02:00
Tom Gundersen fb41e6d7c6 sd-dhcp-client: don't fail hard if UDP socket can not be bound
Even if we cannot renew the lease at T1, we will likely succeed at T2, so warn and ignore the failure.

This could happen if for whatever reason the received address is not yet configured, or it has
been lost.
2014-06-29 16:42:43 +02:00
Patrik Flykt 68ceb9df6a sd-dhcp-client/networkd: set lifetimes for IPv4 addresses
Note that /proc/sys/net/ipv4/ip_dynaddr needs to be non-zero.

[tomegun: hook up DHCP renew events to increase the lifetime when necessary]
2014-06-29 15:18:21 +02:00
Tom Gundersen 574cc92888 sd-dhcp-client: return NULL from _unref() like the other sd-* libraries
Let's keep this behavior consistent across our libraries.

In order to keep the refcounting working, a DONT_DESTROY macro similar
to the one in sd-bus was introduced.
2014-06-24 17:05:39 +02:00
Thomas Hindoe Paaboel Andersen f7926298e9 sd-dhcp-client: check return from dhcp_option_append 2014-06-18 23:21:00 +02:00
Andreas Henriksson 8186d9dda0 sd-dhcp-client: fix invalid free() in client_send_request()
static int client_send_request(...) in
./src/libsystemd-network/sd-dhcp-client.c tries to initialize
"request" by calling client_message_init(...), which has atleast
5 error cases where it can return without that happening.
This leads to the function finishing without "request" being initialized.
2014-06-13 18:57:33 +02:00
Camilo Aguilar 63a070415d sd-dhcp-client: allways request broadcast
On systems which cannot receive unicast packets until its IP stack has been configured
we need to request broadcast packets. We are currently not able to reliably detect when
this is necessary, so set it unconditionally for now.

This is set on all packets, but the DHCP server will only broadcast the packets that are
necessary, and unicast the rest.

For more information please refer to this thread in CoreOS: https://github.com/coreos/bugs/issues/12

[tomegun: rephrased commit message]
2014-06-03 10:55:46 +02:00
Cristian Rodríguez 590b6b9188 Use %m instead of strerror(errno) where appropiate 2014-05-25 11:18:28 +08:00
Tom Gundersen 424a873214 sd-dhcp-client: factor out dhcp packet allocation 2014-05-21 16:55:47 +02:00
Tom Gundersen 23289745d7 sd-dhcp: rely on FIONREAD working
This fallback will anyway never get tested, so rip it out.
2014-05-21 16:31:28 +02:00
Tom Gundersen 04b28be1a3 sd-dhcp: option_append - support falling back to 'sname' and 'file' 2014-05-21 16:09:05 +02:00
Tom Gundersen 20b958bf15 sd-dhcp: refactor dhcp_option_append
Store a pointer to the options in the DHCPMessage struct, and pass
this together with an offset around, rather than a uint8_t**.

This avoids us having to (re)compute the pointer; and changes
dhcp_option_append from adjusting both the pointer to the next
option and the remaining size of the options, to just adjusting
the current offset.

This makes the code a bit simpler to follow IMHO, but there should
be no functional change.
2014-05-20 23:31:22 +02:00
Tom Gundersen 699370c98d sd-dhcp-client: improve logging when stopping client
'Requested by user' was confusing, just drop it.
2014-05-09 14:41:27 +02:00
Tom Gundersen 22fc2420b2 sd-dhcp-client: use asynchronous_close()
close() is a blocking call, which may slow things down measurably when running many dhcp
clients in the same single-threaded main loop. Let's just use the asynchronous version
instead to avoid the problem.
2014-05-06 22:59:22 +02:00
Tom Gundersen 085cabf266 dhcp-network: don't pass ifindex to bind_udp_socket
UDP sockets can anyway not be bound to specific netdev's. The packages would have to be filtered
when received instead.
2014-05-06 21:58:08 +02:00
Umut Tezduyar Lindskog ccfdc9a112 sd-dhcp-client: log positive error number
Log error no for such client_stop(client, DHCP_EVENT_STOP)
2014-04-27 23:41:46 +02:00
Umut Tezduyar Lindskog d9bf4f8c6c libnetworkd: add link local tests
- Also only allow positive ifindex on both dhcp and ipv4ll

[tomegun: the kernel always sets a positive ifindex, but some APIs accept
          ifindex=0 with various meanings, so we should protect against
          accidentally passing ifindex=0 along.]
2014-04-27 23:39:13 +02:00
Tom Gundersen 6317519517 sd-dhcp-client: log ifindex and mac address when starting 2014-04-20 19:20:26 +02:00
Zbigniew Jędrzejewski-Szmek 6e00a80641 test-dhcp-client: unref lease objects to make valgrind happy
Also unref client objects in test code, and initalize logging,
to DEBUG by default.
2014-04-12 10:20:55 -04:00
Tom Gundersen 730b306208 sd-dhcp-client: drop logging about T1/T2
These are redundant now that the REQUEST messages contain the same information.
2014-04-12 11:20:28 +02:00
Tom Gundersen 998d804702 sd-dhcp-client: improve logging
Specify what kind of REQUEST we send, and distinguish between REBOOT and START.
Also log stop reasons as strings rather than numbers.
2014-04-12 01:01:13 +02:00
Tom Gundersen 7739a40b45 sd-dhcp-client: use client_initialiez and client_restart for REBOOT 2014-04-12 00:34:05 +02:00
Tom Gundersen 50d6810ea8 sd-dhcp-client: document message creation a bit more
Also reshuffle some code to make the correspondence with the RFC a bit more
obvious.

Small functional change: fail if we try to send a message from the wrong state.
2014-04-11 22:24:26 +02:00
Tom Gundersen 8a9e761600 sd-dhcp-client: assert that we can only create DISCOVER or REQUEST messages 2014-04-11 22:24:25 +02:00
Tom Gundersen eb105b96d5 sd-dhcp-client: rebind raw socket when resetting transaction id
As we are now filtering the raw socket based on the transaction id, we must
reset the BPF when we reset the transaction id.
2014-04-11 18:02:54 +02:00
Patrik Flykt 781ca7a13f sd-dhcp-client: Add an explicit stop state for the DHCP client
Add an explicit stop state for the DHCP client so that the library
user can issue a stop at any time the callback has been called.
When returning from the callback, check also the stop state and
stop any further DHCP processing.
2014-04-11 10:53:34 +03:00
Patrik Flykt e5b04c8de8 sd-dhcp-client: Add reference counting for DHCP
The DHCP library user can decide to free the DHCP client any time
the callback is called. After the callback has been called, other
computations may still be needed - the best example being a full
restart of the DHCP procedure in case of lease expiry.

Fix this by introducing proper reference counting. Properly handle
a returned NULL from the notify and stop functions if the DHCP
client was freed.
2014-04-11 10:53:10 +03:00
Tom Gundersen 48a4612e6b sd-dhcp-client: recevie_message - verify cmsg_len before reading 2014-04-11 00:52:23 +02:00
Tom Gundersen 7429b07f82 sd-dhcp-client: improve BPF
Try a bit harder to make the kernel drop packets not for us. This should reduce
the number of wakeups from n^2 to n in the number of dhcp clients, which admittedly
only makes a differenc in very extreme cases.
2014-04-06 19:42:11 +02:00
Tom Gundersen 0c79c68d93 sd-dhcp-client: eagerly drop too small packets
If they are too small to fit the IP+UDP+DHCP headers they can be of no use, so
don't waste resources parsing them. This is at the cost of losing some verbosity
in the logging.
2014-04-06 19:42:11 +02:00
Tom Gundersen 3b7ca119fd sd-dhcp-client: move magic cookie into DHCPMessage struct
Also move the checking of it to the main message handler, rather than the
options parser.

Fix a bug, so we now drop the packet if any of the magic bytes don't match.
Before we used to only drop the packet if they were all wrong.
2014-04-06 19:36:05 +02:00
Thomas Hindoe Paaboel Andersen 9f2a50a300 silence warnings 2014-03-27 20:16:49 +01:00
Lennart Poettering 6a0f1f6d5a sd-event: rework API to support CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM, too 2014-03-24 02:58:41 +01:00
Tom Gundersen 4644fee04f sd-dhcp-client/sd-ipv4ll: allow mac address to be updated at any time
If necessary, restart the clients to deal with a changing mac address
at runtime. This will solve the problem of starting clients on bridges
before they have received their final MAC address.
2014-03-21 18:36:32 +01:00
Tom Gundersen 9e64dd7276 sd-dhcp-client: add fallback subnet masks
The DHCP RFC does not require the DHCP server to send a subnet mask, so if it
is missing, let's try to use the default subnet masks based on address class.
In case the class the address belongs to does not have a default subnet mask,
we fail as before.

Also improve logging when handling invalid dhcp messages, and simply ignore them
rather than stop the whole dhcp client.
2014-03-21 17:52:42 +01:00
Tom Gundersen 022446adf9 sd-dhcp-client: make timeout handling a bit more robust
Accept any lease lifetime greater than one second. Server should not
hand out extremely short leases, but let's not be the ones to fail.

Do not fail when arming a timer in the past, but also only arm one such
timer.

Avoid rounding errors when computing the default timeouts, this may be
an issue if we are handed a very short lease.

Also, don't pass 'time_now' around, as that can be found in the event
object when needed.
2014-03-21 17:52:42 +01:00
Tom Gundersen 58ec2d3e6e sd-dhcp-client: do not reset 'secs' when entering INIT-REBOOT
Also keep start_time in sync, but that shouldn't matter.
2014-03-20 17:07:43 +01:00
Tom Gundersen 6236f49b23 sd-dhcp-client: don't pass around 'secs'
The value is stored in the client object, so get it there when needed.
2014-03-20 17:07:43 +01:00
Patrik Flykt 9d7bf1d58c libsystemd-network: Don't unnecessarily send too long packets
Since the length used by options is known, send packets with no
extra padding.
2014-03-20 10:55:27 +02:00
Patrik Flykt 715c6a9acd libsystemd-network: Prepend hardware type byte to client identifier
Even though client identifiers SHOULD be treated as opaque objects by
DHCP servers, follow the recommendation of a hardware type field with
value 0x01 (ethernet) followed by the hardware address as described in
RFC 2132.
2014-03-20 10:54:31 +02:00
Tom Gundersen 7bf2f43972 sd-dhcp-client: accept infinite lease lifetime
Otherwise we would fail with -EINVAL. Thanks to Brandon Philips
<brandon.philips@coreos.com>, for reporting the bug.
2014-03-19 10:41:29 +01:00
Patrik Flykt 8b1243f72e libsystemd-network: Add Init-Reboot support
Init-Reboot is tried if a client IP address has been given when
the DHCP client is started. In Init-Reboot, start by sending a
broadcast DHCP Request including the supplied client IP address
but without the server identifier. After sending the request,
enter Reboot state.

If a DHCP Ack is received, proceed to Bound state as usual. If a
DHCP Nak is received or the first timeout triggers, start the
address acquisition over from DHCP Init state.

See RFC 2131, sections 4.3.2, 4.4, 4.4.1 and 4.4.2 for details.
2014-03-19 10:52:40 +02:00
Patrik Flykt 0f941add36 libsystemd-network: Restart DHCP acquisition if the lease expires
This causes the DHCP client struct initialization and DHCP client
starting to be factored out into functions of their own.
2014-03-19 10:52:40 +02:00
Lennart Poettering 03e334a1c7 util: replace close_nointr_nofail() by a more useful safe_close()
safe_close() automatically becomes a NOP when a negative fd is passed,
and returns -1 unconditionally. This makes it easy to write lines like
this:

        fd = safe_close(fd);

Which will close an fd if it is open, and reset the fd variable
correctly.

By making use of this new scheme we can drop a > 200 lines of code that
was required to test for non-negative fds or to reset the closed fd
variable afterwards.
2014-03-18 19:31:34 +01:00
Tom Gundersen d23c45bfcc sd-dhcp-client: make sure timers fire immediately
The default slack caused there to be a delay before timers fired. Solve it
by setting timers that should trigger immediately to trigger far in the past.

This brings down the ideal-case dhcp lease acquisition time from about 500ms to
about 50ms (over a veth pair, so no network latency involved).

All the rest of the time (except for ~0.5ms) is spent in the bind() call in,
dhcp_network_bind_raw_socket(). I don't know if there is anything to be done
about that though...
2014-03-18 15:53:32 +01:00
Tom Gundersen 93570bd7ed sd-dhcp-client: use close_nointr_nofail()
We are already assuming the close() will not fail, so make it explicit.
2014-03-11 12:03:46 +01:00
Tom Gundersen 63edaa623b sd-dhcp: generalise ip header generation
This will be needed for sd-dhcp-server.
2014-03-11 12:03:46 +01:00
Tom Gundersen 02ec5cd78a sd-dhcp-client: log the MAC address we are using 2014-03-05 11:07:15 +01:00
Tom Gundersen 8e34a618e2 sd-dhcp-lease: add sd_dhcp_lease_get_next_server() 2014-03-03 17:18:22 +01:00
Tom Gundersen fe8db0c5ee sd-network: add new library
This is similar to sd-login, but exposes the state of networkd rather than logind.

Include it in libsystemd-dhcp and rename it to libsystemd-network.
2014-02-28 01:01:13 +01:00
Renamed from src/libsystemd-dhcp/sd-dhcp-client.c (Browse further)