[systemd-devel] [PATCH 07/24] sd-dhcp6-client: Add DHCPv6 IAID functionality
Patrik Flykt
patrik.flykt at linux.intel.com
Fri Jun 13 06:44:57 PDT 2014
Create structures describing Identity Association IDentifiers and
IPv6 lease addresses.
[tomegun: initialize the IAID when client is started. Base this off of the
predictable udev names, if available, as these satisfy the requirement of
the IAID, and base it off the mac addres otherwise, as that is the best we
have.]
---
src/libsystemd-network/dhcp6-internal.h | 32 +++++++++++++
src/libsystemd-network/sd-dhcp6-client.c | 80 ++++++++++++++++++++++++++++++++
2 files changed, 112 insertions(+)
diff --git a/src/libsystemd-network/dhcp6-internal.h b/src/libsystemd-network/dhcp6-internal.h
index 52283d7..1cdb912 100644
--- a/src/libsystemd-network/dhcp6-internal.h
+++ b/src/libsystemd-network/dhcp6-internal.h
@@ -22,6 +22,38 @@
***/
#include <net/ethernet.h>
+#include <netinet/in.h>
+
+#include "sparse-endian.h"
+#include "sd-event.h"
+#include "list.h"
+
+typedef struct DHCP6Address DHCP6Address;
+
+struct DHCP6Address {
+ LIST_FIELDS(DHCP6Address, addresses);
+
+ struct {
+ struct in6_addr address;
+ be32_t lifetime_preferred;
+ be32_t lifetime_valid;
+ } _packed_;
+};
+
+struct DHCP6IA {
+ uint16_t type;
+ struct {
+ be32_t id;
+ be32_t lifetime_t1;
+ be32_t lifetime_t2;
+ } _packed_;
+ sd_event_source *timeout_t1;
+ sd_event_source *timeout_t2;
+
+ LIST_HEAD(DHCP6Address, addresses);
+};
+
+typedef struct DHCP6IA DHCP6IA;
#define log_dhcp6_client(p, fmt, ...) log_meta(LOG_DEBUG, __FILE__, __LINE__, __func__, "DHCPv6 CLIENT: " fmt, ##__VA_ARGS__)
diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c
index a785fa6..f61442b 100644
--- a/src/libsystemd-network/sd-dhcp6-client.c
+++ b/src/libsystemd-network/sd-dhcp6-client.c
@@ -22,10 +22,14 @@
#include <errno.h>
#include <string.h>
+#include "udev.h"
+#include "udev-util.h"
+#include "virt.h"
#include "siphash24.h"
#include "util.h"
#include "refcnt.h"
+#include "network-internal.h"
#include "sd-dhcp6-client.h"
#include "dhcp6-protocol.h"
#include "dhcp6-internal.h"
@@ -43,6 +47,7 @@ struct sd_dhcp6_client {
int index;
struct ether_addr mac_addr;
icmp6_nd *ra;
+ DHCP6IA ia_na;
sd_dhcp6_client_cb_t cb;
void *userdata;
@@ -101,6 +106,11 @@ static int client_initialize(sd_dhcp6_client *client)
{
assert_return(client, -EINVAL);
+ client->ia_na.timeout_t1 =
+ sd_event_source_unref(client->ia_na.timeout_t1);
+ client->ia_na.timeout_t2 =
+ sd_event_source_unref(client->ia_na.timeout_t2);
+
client->state = DHCP6_STATE_STOPPED;
return 0;
@@ -116,10 +126,70 @@ static sd_dhcp6_client *client_stop(sd_dhcp6_client *client, int error) {
return client;
}
+static int client_ensure_iaid(sd_dhcp6_client *client) {
+ const char *name = NULL;
+ uint64_t id;
+
+ assert(client);
+
+ if (client->ia_na.id)
+ return 0;
+
+ if (detect_container(NULL) <= 0) {
+ /* not in a container, udev will be around */
+ _cleanup_udev_unref_ struct udev *udev;
+ _cleanup_udev_device_unref_ struct udev_device *device;
+ char ifindex_str[2 + DECIMAL_STR_MAX(int)];
+
+ udev = udev_new();
+ if (!udev)
+ return -ENOMEM;
+
+ sprintf(ifindex_str, "n%d", client->index);
+ device = udev_device_new_from_device_id(udev, ifindex_str);
+ if (!device)
+ return -errno;
+
+ if (udev_device_get_is_initialized(device) <= 0)
+ /* not yet ready */
+ return -EBUSY;
+
+ name = net_get_name(device);
+ }
+
+ if (name)
+ siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes);
+ else
+ /* fall back to mac address if no predictable name available */
+ siphash24((uint8_t*)&id, &client->mac_addr, ETH_ALEN,
+ HASH_KEY.bytes);
+
+ /* fold into 32 bits */
+ client->ia_na.id = (id & 0xffffffff) ^ (id >> 32);
+
+ return 0;
+}
+
+static int client_start(sd_dhcp6_client *client)
+{
+ int r;
+
+ assert_return(client, -EINVAL);
+ assert_return(client->event, -EINVAL);
+ assert_return(client->index > 0, -EINVAL);
+
+ r = client_ensure_iaid(client);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
static void dhcp6_receive_router_advertisment(icmp6_nd *nd, int event,
void *userdata)
{
sd_dhcp6_client *client = userdata;
+ int r;
if (event < 0) {
log_dhcp6_client(client, "Router Advertisment failed with "
@@ -136,6 +206,9 @@ static void dhcp6_receive_router_advertisment(icmp6_nd *nd, int event,
case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
+ r = client_start(client);
+ if (r < 0)
+ client_stop(client, r);
break;
}
}
@@ -158,6 +231,10 @@ int sd_dhcp6_client_start(sd_dhcp6_client *client)
assert_return(client->event, -EINVAL);
assert_return(client->index > 0, -EINVAL);
+ r = client_initialize(client);
+ if (r < 0)
+ return r;
+
r = icmp6_nd_new(&nd);
if (r < 0)
return r;
@@ -240,6 +317,7 @@ sd_dhcp6_client *sd_dhcp6_client_ref(sd_dhcp6_client *client) {
sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) {
if (client && REFCNT_DEC(client->n_ref) <= 0) {
+ log_dhcp6_client(client, "UNREF");
client_initialize(client);
@@ -272,6 +350,8 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret)
client->n_ref = REFCNT_INIT;
+ client->ia_na.type = DHCP6_OPTION_IA_NA;
+
client->index = -1;
/* initialize DUID */
--
1.9.1
More information about the systemd-devel
mailing list