From 8eb9058dc1f99a5eb9b8726a978fcc0720837a10 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 26 Aug 2015 19:19:32 +0200 Subject: dhcp,network: implement RFC 4833 (DHCP Timezone option) This one is simply to add: encode the tzdata timezone in the DHCP options and optionally make use of it. --- src/network/networkd-dhcp4.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 1b2ff7c769..7fce389fa2 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -624,6 +624,11 @@ int dhcp4_configure(Link *link) { return r; } + /* Always acquire the timezone */ + r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_NEW_TZDB_TIMEZONE); + if (r < 0) + return r; + if (link->network->dhcp_sendhost) { _cleanup_free_ char *hostname = NULL; const char *hn = NULL; -- cgit v1.2.3-54-g00ecf From 4b7b5abb785c03cb880e3a8a7a724d7de6e9cf3b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 27 Aug 2015 01:14:20 +0200 Subject: 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. --- src/libsystemd-network/sd-dhcp-client.c | 1 - src/libsystemd-network/test-dhcp-client.c | 5 +---- src/network/networkd-dhcp4.c | 6 +++++- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 29c67f23be..008b21c3b8 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -105,7 +105,6 @@ static const uint8_t default_req_opts[] = { DHCP_OPTION_HOST_NAME, DHCP_OPTION_DOMAIN_NAME, DHCP_OPTION_DOMAIN_NAME_SERVER, - DHCP_OPTION_NTP_SERVER, }; static int client_receive_message_raw(sd_event_source *s, int fd, diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index b2a0d3bb8b..877adc7b0d 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -87,10 +87,7 @@ static void test_request_basic(sd_event *e) assert_se(sd_dhcp_client_set_request_option(client, DHCP_OPTION_DOMAIN_NAME) == -EEXIST); assert_se(sd_dhcp_client_set_request_option(client, - DHCP_OPTION_DOMAIN_NAME_SERVER) - == -EEXIST); - assert_se(sd_dhcp_client_set_request_option(client, - DHCP_OPTION_NTP_SERVER) == -EEXIST); + DHCP_OPTION_DOMAIN_NAME_SERVER) == -EEXIST); assert_se(sd_dhcp_client_set_request_option(client, DHCP_OPTION_PAD) == -EINVAL); diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 7fce389fa2..ea1b18a0e5 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -624,7 +624,11 @@ int dhcp4_configure(Link *link) { return r; } - /* Always acquire the timezone */ + /* Always acquire the timezone and NTP*/ + r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_NTP_SERVER); + if (r < 0) + return r; + r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_NEW_TZDB_TIMEZONE); if (r < 0) return r; -- cgit v1.2.3-54-g00ecf From 39745a5afb24ba0c877122aa1967d58f02c5435f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 27 Aug 2015 01:18:10 +0200 Subject: networkd: fix indentation --- src/network/networkd-dhcp4.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index ea1b18a0e5..3082bf8ac8 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -607,10 +607,10 @@ int dhcp4_configure(Link *link) { } if (link->network->dhcp_mtu) { - r = sd_dhcp_client_set_request_option(link->dhcp_client, - DHCP_OPTION_INTERFACE_MTU); - if (r < 0) - return r; + r = sd_dhcp_client_set_request_option(link->dhcp_client, + DHCP_OPTION_INTERFACE_MTU); + if (r < 0) + return r; } if (link->network->dhcp_routes) { -- cgit v1.2.3-54-g00ecf From 21b80ad126c1aa43bd6d0e5a7ed3cc7c0f10efb0 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 27 Aug 2015 02:12:27 +0200 Subject: networkd: optionally push dhcp timezone into timedated --- src/network/networkd-dhcp4.c | 12 +++++++++ src/network/networkd-link.c | 61 ++++++++++++++++++++++++++++++++++++++++---- src/network/networkd-link.h | 1 + 3 files changed, 69 insertions(+), 5 deletions(-) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 3082bf8ac8..4e2936f637 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -482,6 +482,18 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { } } + if (link->network->dhcp_timezone) { + const char *tz = NULL; + + (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); + + if (tz) { + r = link_set_timezone(link, tz); + if (r < 0) + log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); + } + } + if (!link->network->dhcp_critical) { r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); if (r < 0) { diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 5b7ebfb79d..371136a11e 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -781,7 +781,7 @@ static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userd static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { _cleanup_link_unref_ Link *link = userdata; - int r; + const sd_bus_error *e; assert(m); assert(link); @@ -789,15 +789,15 @@ static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) return 1; - r = sd_bus_message_get_errno(m); - if (r > 0) - log_link_warning_errno(link, r, "Could not set hostname: %m"); + e = sd_bus_message_get_error(m); + if (e) + log_link_warning_errno(link, sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message); return 1; } int link_set_hostname(Link *link, const char *hostname) { - int r = 0; + int r; assert(link); assert(link->manager); @@ -832,6 +832,57 @@ int link_set_hostname(Link *link, const char *hostname) { return 0; } +static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { + _cleanup_link_unref_ Link *link = userdata; + const sd_bus_error *e; + + assert(m); + assert(link); + + if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) + return 1; + + e = sd_bus_message_get_error(m); + if (e) + log_link_warning_errno(link, sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message); + + return 1; +} + +int link_set_timezone(Link *link, const char *timezone) { + int r; + + assert(link); + assert(link->manager); + assert(timezone); + + log_link_debug(link, "Setting system timezone: '%s'", timezone); + + if (!link->manager->bus) { + log_link_info(link, "Not connected to system bus, ignoring timezone."); + return 0; + } + + r = sd_bus_call_method_async( + link->manager->bus, + NULL, + "org.freedesktop.timedate1", + "/org/freedesktop/timedate1", + "org.freedesktop.timedate1", + "SetTimezone", + set_timezone_handler, + link, + "sb", + timezone, + false); + if (r < 0) + return log_link_error_errno(link, r, "Could not set timezone: %m"); + + link_ref(link); + + return 0; +} + static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { _cleanup_link_unref_ Link *link = userdata; int r; diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h index 2dcbbda607..0625520790 100644 --- a/src/network/networkd-link.h +++ b/src/network/networkd-link.h @@ -115,6 +115,7 @@ bool link_has_carrier(Link *link); int link_set_mtu(Link *link, uint32_t mtu); int link_set_hostname(Link *link, const char *hostname); +int link_set_timezone(Link *link, const char *timezone); int ipv4ll_configure(Link *link); int dhcp4_configure(Link *link); -- cgit v1.2.3-54-g00ecf From dce391e78f5d8d5f4feb3eb6a15436de12dacf46 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 27 Aug 2015 12:53:43 +0200 Subject: networkd: properly reset transient hostname when we lose a DHCP lease Previously we were setting the transient hostname again, rather than resetting it. --- src/network/networkd-dhcp4.c | 24 +++++++++++------------- src/network/networkd-link.c | 3 +-- 2 files changed, 12 insertions(+), 15 deletions(-) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index 4e2936f637..a075015c3c 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -270,18 +270,16 @@ static int dhcp_lease_lost(Link *link) { if (link->network->dhcp_hostname) { const char *hostname = NULL; - if (!link->network->hostname) - r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); - else + if (link->network->hostname) hostname = link->network->hostname; + else + (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); - if (r >= 0 || hostname) { - r = link_set_hostname(link, hostname); + if (hostname) { + /* If a hostname was set due to the lease, then unset it now. */ + r = link_set_hostname(link, NULL); if (r < 0) - log_link_error_errno(link, r, - "Failed to set transient hostname to '%s': %m", - hostname); - + log_link_warning_errno(link, r, "Failed to reset transient hostname: %m"); } } @@ -470,12 +468,12 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { if (link->network->dhcp_hostname) { const char *hostname = NULL; - if (!link->network->hostname) - r = sd_dhcp_lease_get_hostname(lease, &hostname); - else + if (link->network->hostname) hostname = link->network->hostname; + else + (void) sd_dhcp_lease_get_hostname(lease, &hostname); - if (r >= 0 || hostname) { + if (hostname) { r = link_set_hostname(link, hostname); if (r < 0) log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 371136a11e..5af82059d8 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -801,9 +801,8 @@ int link_set_hostname(Link *link, const char *hostname) { assert(link); assert(link->manager); - assert(hostname); - log_link_debug(link, "Setting transient hostname: '%s'", hostname); + log_link_debug(link, "Setting transient hostname: '%s'", strna(hostname)); if (!link->manager->bus) { /* TODO: replace by assert when we can rely on kdbus */ -- cgit v1.2.3-54-g00ecf From f6b8196f7cf1e366e15597283bc3d13af72a7eb5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 27 Aug 2015 13:04:33 +0200 Subject: networkd: port many log messages over to newer logging API Let's drop some strerror() invocations, and make use of the easier to use newer logging APIs. --- src/network/networkd-dhcp4.c | 129 +++++++++++++------------------------------ 1 file changed, 37 insertions(+), 92 deletions(-) (limited to 'src/network/networkd-dhcp4.c') diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index a075015c3c..36601367bf 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -39,8 +39,7 @@ static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { - log_link_error(link, "could not set DHCPv4 route: %s", - strerror(-r)); + log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); link_enter_failed(link); } @@ -61,40 +60,25 @@ static int link_set_dhcp_routes(Link *link) { assert(link->dhcp_lease); r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); - if (r < 0 && r != -ENOENT) { - log_link_warning(link, - "DHCP error: could not get gateway: %s", - strerror(-r)); - return r; - } + if (r < 0 && r != -ENOENT) + return log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m"); + if (r >= 0) { struct in_addr address; _cleanup_route_free_ Route *route = NULL; _cleanup_route_free_ Route *route_gw = NULL; r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); - if (r < 0) { - log_link_warning(link, - "DHCP error: could not get address: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "DHCP error: could not get address: %m"); r = route_new_dynamic(&route, RTPROT_DHCP); - if (r < 0) { - log_link_error(link, - "Could not allocate route: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_error_errno(link, r, "Could not allocate route: %m"); r = route_new_dynamic(&route_gw, RTPROT_DHCP); - if (r < 0) { - log_link_error(link, - "Could not allocate route: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_error_errno(link, r, "Could not allocate route: %m"); /* The dhcp netmask may mask out the gateway. Add an explicit * route for the gw host so that we can route no matter the @@ -107,12 +91,8 @@ static int link_set_dhcp_routes(Link *link) { route_gw->metrics = link->network->dhcp_route_metric; r = route_configure(route_gw, link, &dhcp4_route_handler); - if (r < 0) { - log_link_warning(link, - "could not set host route: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "Could not set host route: %m"); link->dhcp4_messages ++; @@ -123,9 +103,7 @@ static int link_set_dhcp_routes(Link *link) { r = route_configure(route, link, &dhcp4_route_handler); if (r < 0) { - log_link_warning(link, - "could not set routes: %s", - strerror(-r)); + log_link_warning_errno(link, r, "Could not set routes: %m"); link_enter_failed(link); return r; } @@ -136,23 +114,15 @@ static int link_set_dhcp_routes(Link *link) { n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); if (n == -ENOENT) return 0; - if (n < 0) { - log_link_warning(link, - "DHCP error: could not get routes: %s", - strerror(-n)); - - return n; - } + if (n < 0) + return log_link_warning_errno(link, n, "DHCP error: could not get routes: %m"); for (i = 0; i < n; i++) { _cleanup_route_free_ Route *route = NULL; r = route_new_dynamic(&route, RTPROT_DHCP); - if (r < 0) { - log_link_error(link, "Could not allocate route: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_error_errno(link, r, "Could not allocate route: %m"); route->family = AF_INET; route->in_addr.in = static_routes[i].gw_addr; @@ -161,12 +131,8 @@ static int link_set_dhcp_routes(Link *link) { route->metrics = link->network->dhcp_route_metric; r = route_configure(route, link, &dhcp4_route_handler); - if (r < 0) { - log_link_warning(link, - "could not set host route: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "Could not set host route: %m"); link->dhcp4_messages ++; } @@ -298,8 +264,7 @@ static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, r = sd_netlink_message_get_errno(m); if (r < 0 && r != -EEXIST) { - log_link_error(link, "could not set DHCPv4 address: %s", - strerror(-r)); + log_link_error_errno(link, r, "Could not set DHCPv4 address: %m"); link_enter_failed(link); } else if (r >= 0) link_rtnl_process_address(rtnl, m, link->manager); @@ -355,45 +320,30 @@ static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { assert(link->network); r = sd_dhcp_client_get_lease(client, &lease); - if (r < 0) { - log_link_warning(link, "DHCP error: no lease %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); sd_dhcp_lease_unref(link->dhcp_lease); link->dhcp4_configured = false; link->dhcp_lease = sd_dhcp_lease_ref(lease); r = sd_dhcp_lease_get_address(lease, &address); - if (r < 0) { - log_link_warning(link, "DHCP error: no address: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "DHCP error: no address: %m"); r = sd_dhcp_lease_get_netmask(lease, &netmask); - if (r < 0) { - log_link_warning(link, "DHCP error: no netmask: %s", - strerror(-r)); - return r; - } + if (r < 0) + return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); if (!link->network->dhcp_critical) { - r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, - &lifetime); - if (r < 0) { - log_link_warning(link, - "DHCP error: no lifetime: %s", - strerror(-r)); - return r; - } + r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); + if (r < 0) + return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); } r = dhcp4_update_address(link, &address, &netmask, lifetime); if (r < 0) { - log_link_warning(link, "could not update IP address: %s", - strerror(-r)); + log_link_warning_errno(link, r, "Could not update IP address: %m"); link_enter_failed(link); return r; } @@ -415,21 +365,21 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { r = sd_dhcp_client_get_lease(client, &lease); if (r < 0) - return log_link_error_errno(link, r, "DHCP error: no lease: %m"); + return log_link_error_errno(link, r, "DHCP error: No lease: %m"); r = sd_dhcp_lease_get_address(lease, &address); if (r < 0) - return log_link_error_errno(link, r, "DHCP error: no address: %m"); + return log_link_error_errno(link, r, "DHCP error: No address: %m"); r = sd_dhcp_lease_get_netmask(lease, &netmask); if (r < 0) - return log_link_error_errno(link, r, "DHCP error: no netmask: %m"); + return log_link_error_errno(link, r, "DHCP error: No netmask: %m"); prefixlen = in_addr_netmask_to_prefixlen(&netmask); r = sd_dhcp_lease_get_router(lease, &gateway); if (r < 0 && r != -ENOENT) - return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m"); + return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); if (r >= 0) log_struct(LOG_INFO, @@ -525,8 +475,7 @@ static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { case DHCP_EVENT_STOP: case DHCP_EVENT_IP_CHANGE: if (link->network->dhcp_critical) { - log_link_error(link, - "DHCPv4 connection considered system critical, ignoring request to reconfigure it."); + log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it."); return; } @@ -563,13 +512,9 @@ static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { break; default: if (event < 0) - log_link_warning(link, - "DHCP error: client failed: %s", - strerror(-event)); + log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); else - log_link_warning(link, - "DHCP unknown event: %d", - event); + log_link_warning(link, "DHCP unknown event: %i", event); break; } -- cgit v1.2.3-54-g00ecf