2014-12-10 15:17:34 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright (C) 2014 Intel Corporation. All rights reserved.
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <netinet/ether.h>
|
|
|
|
#include <linux/if.h>
|
|
|
|
|
|
|
|
#include "sd-dhcp6-client.h"
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "network-internal.h"
|
|
|
|
#include "networkd-link.h"
|
|
|
|
|
2015-04-10 13:03:18 +02:00
|
|
|
static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link);
|
|
|
|
|
2015-01-20 18:36:04 +01:00
|
|
|
static int dhcp6_lease_information_acquired(sd_dhcp6_client *client,
|
|
|
|
Link *link) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m,
|
2015-01-20 18:36:04 +01:00
|
|
|
void *userdata) {
|
|
|
|
_cleanup_link_unref_ Link *link = userdata;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0 && r != -EEXIST) {
|
2015-04-10 13:03:18 +02:00
|
|
|
if (link->rtnl_extended_attrs) {
|
|
|
|
log_link_warning(link, "Could not set extended netlink attributes, reverting to fallback mechanism");
|
|
|
|
|
|
|
|
link->rtnl_extended_attrs = false;
|
|
|
|
dhcp6_lease_address_acquired(link->dhcp6_client, link);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:16:17 +02:00
|
|
|
log_link_error_errno(link, r, "Could not set DHCPv6 address: %m");
|
2015-01-20 18:36:04 +01:00
|
|
|
|
|
|
|
link_enter_failed(link);
|
|
|
|
|
|
|
|
} else if (r >= 0)
|
2015-09-22 17:18:20 +02:00
|
|
|
manager_rtnl_process_address(rtnl, m, link->manager);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-28 17:16:12 +02:00
|
|
|
static int dhcp6_address_change(Link *link, struct in6_addr *ip6_addr,
|
2015-10-18 16:59:21 +02:00
|
|
|
uint32_t lifetime_preferred, uint32_t lifetime_valid) {
|
2015-01-20 18:36:04 +01:00
|
|
|
int r;
|
|
|
|
_cleanup_address_free_ Address *addr = NULL;
|
|
|
|
|
2015-09-21 15:53:40 +02:00
|
|
|
r = address_new(&addr);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
addr->family = AF_INET6;
|
|
|
|
memcpy(&addr->in_addr.in6, ip6_addr, sizeof(*ip6_addr));
|
2015-02-02 12:13:17 +01:00
|
|
|
|
|
|
|
addr->flags = IFA_F_NOPREFIXROUTE;
|
2015-10-18 16:59:21 +02:00
|
|
|
addr->prefixlen = 128;
|
2015-01-20 18:36:04 +01:00
|
|
|
|
|
|
|
addr->cinfo.ifa_prefered = lifetime_preferred;
|
|
|
|
addr->cinfo.ifa_valid = lifetime_valid;
|
|
|
|
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
log_link_info(link,
|
2015-10-16 17:07:47 +02:00
|
|
|
"DHCPv6 address "SD_NDISC_ADDRESS_FORMAT_STR"/%d timeout preferred %d valid %d",
|
|
|
|
SD_NDISC_ADDRESS_FORMAT_VAL(addr->in_addr.in6),
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
addr->prefixlen, lifetime_preferred, lifetime_valid);
|
2015-01-20 18:36:04 +01:00
|
|
|
|
2015-10-01 17:31:14 +02:00
|
|
|
r = address_configure(addr, link, dhcp6_address_handler, true);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
|
|
|
log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m");
|
2015-01-20 18:36:04 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
|
|
|
|
int r;
|
|
|
|
sd_dhcp6_lease *lease;
|
|
|
|
struct in6_addr ip6_addr;
|
|
|
|
uint32_t lifetime_preferred, lifetime_valid;
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_get_lease(client, &lease);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
sd_dhcp6_lease_reset_address_iter(lease);
|
|
|
|
|
|
|
|
while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
|
|
|
|
&lifetime_preferred,
|
|
|
|
&lifetime_valid) >= 0) {
|
|
|
|
|
2015-10-18 16:59:21 +02:00
|
|
|
r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid);
|
2015-01-20 18:36:04 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:17:34 +01:00
|
|
|
static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
|
2015-01-20 18:36:04 +01:00
|
|
|
int r;
|
2014-12-10 15:17:34 +01:00
|
|
|
Link *link = userdata;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
assert(link->network);
|
|
|
|
assert(link->manager);
|
|
|
|
|
|
|
|
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(event) {
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_STOP:
|
|
|
|
case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
|
|
|
|
case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
|
2015-09-23 13:52:03 +02:00
|
|
|
log_link_warning(link, "DHCPv6 lease lost");
|
|
|
|
|
|
|
|
link->dhcp6_configured = false;
|
2015-01-20 18:36:04 +01:00
|
|
|
break;
|
|
|
|
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
|
2015-01-20 18:36:04 +01:00
|
|
|
r = dhcp6_lease_address_acquired(client, link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fall through */
|
2015-09-22 14:52:23 +02:00
|
|
|
case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
|
2015-01-20 18:36:04 +01:00
|
|
|
r = dhcp6_lease_information_acquired(client, link);
|
|
|
|
if (r < 0) {
|
|
|
|
link_enter_failed(link);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-09-23 13:52:03 +02:00
|
|
|
link->dhcp6_configured = true;
|
2014-12-10 15:17:34 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (event < 0)
|
2015-09-30 22:16:17 +02:00
|
|
|
log_link_warning_errno(link, event, "DHCPv6 error: %m");
|
2014-12-10 15:17:34 +01:00
|
|
|
else
|
2015-09-30 22:16:17 +02:00
|
|
|
log_link_warning(link, "DHCPv6 unknown event: %d", event);
|
2014-12-10 15:17:34 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-09-23 13:52:03 +02:00
|
|
|
|
2015-09-28 13:38:43 +02:00
|
|
|
link_check_ready(link);
|
2014-12-10 15:17:34 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
int dhcp6_request_address(Link *link) {
|
|
|
|
int r, inf_req;
|
|
|
|
bool running;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
assert(link);
|
|
|
|
assert(link->dhcp6_client);
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &inf_req);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2015-09-23 13:52:03 +02:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
if (!inf_req)
|
|
|
|
return 0;
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_is_running(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
else
|
|
|
|
running = !!r;
|
2015-09-23 13:10:26 +02:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
if (running) {
|
|
|
|
r = sd_dhcp6_client_stop(link->dhcp6_client);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_information_request(link->dhcp6_client, false);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
if (running) {
|
2014-12-10 15:17:35 +01:00
|
|
|
r = sd_dhcp6_client_start(link->dhcp6_client);
|
2015-11-10 15:43:52 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2014-12-10 15:17:35 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-09-23 13:52:03 +02:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
int dhcp6_configure(Link *link) {
|
|
|
|
sd_dhcp6_client *client = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(link);
|
|
|
|
|
|
|
|
r = sd_dhcp6_client_new(&client);
|
|
|
|
if (r < 0)
|
2014-12-10 15:17:35 +01:00
|
|
|
return r;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_attach_event(client, NULL, 0);
|
2014-12-10 15:17:34 +01:00
|
|
|
if (r < 0)
|
2015-09-23 13:10:26 +02:00
|
|
|
goto error;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_information_request(client, true);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
2015-11-10 15:43:52 +01:00
|
|
|
return r;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_mac(client,
|
2014-12-10 15:17:34 +01:00
|
|
|
(const uint8_t *) &link->mac,
|
|
|
|
sizeof (link->mac), ARPHRD_ETHER);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto error;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_index(client, link->ifindex);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto error;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
r = sd_dhcp6_client_set_callback(client, dhcp6_handler, link);
|
2015-09-23 13:10:26 +02:00
|
|
|
if (r < 0)
|
|
|
|
goto error;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
link->dhcp6_client = client;
|
2015-09-23 13:10:26 +02:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
return 0;
|
2014-12-10 15:17:34 +01:00
|
|
|
|
2015-11-10 15:43:52 +01:00
|
|
|
error:
|
|
|
|
sd_dhcp6_client_unref(client);
|
2014-12-10 15:17:34 +01:00
|
|
|
return r;
|
|
|
|
}
|