X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd-network%2Fsd-dhcp6-client.c;h=a432bbfc488d768c803385a02f2375d71a096c34;hp=4f60578bc3184707767c2bbec0e7b11d6d2fdb31;hb=cfb5b3805759e63dc5e0cae6e92e1df885b5c5b6;hpb=4b4923e65423e60d755841b5b264730e8f3deab3 diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 4f60578bc..a432bbfc4 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -22,11 +22,10 @@ #include #include #include +#include #include "udev.h" #include "udev-util.h" -#include "virt.h" -#include "siphash24.h" #include "util.h" #include "refcnt.h" @@ -35,9 +34,9 @@ #include "dhcp6-protocol.h" #include "dhcp6-internal.h" #include "dhcp6-lease-internal.h" +#include "dhcp-identifier.h" -#define SYSTEMD_PEN 43793 -#define HASH_KEY SD_ID128_MAKE(80,11,8c,c2,fe,4a,03,ee,3e,d6,0c,6f,36,39,14,09) +#define MAX_MAC_ADDR_LEN INFINIBAND_ALEN struct sd_dhcp6_client { RefCount n_ref; @@ -46,11 +45,15 @@ struct sd_dhcp6_client { sd_event *event; int event_priority; int index; - struct ether_addr mac_addr; + uint8_t mac_addr[MAX_MAC_ADDR_LEN]; + size_t mac_addr_len; + uint16_t arp_type; DHCP6IA ia_na; be32_t transaction_id; + usec_t transaction_start; struct sd_dhcp6_lease *lease; int fd; + bool information_request; be16_t *req_opts; size_t req_opts_allocated; size_t req_opts_len; @@ -61,12 +64,8 @@ struct sd_dhcp6_client { sd_event_source *timeout_resend_expire; sd_dhcp6_client_cb_t cb; void *userdata; - - struct duid_en { - uint16_t type; /* DHCP6_DUID_EN */ - uint32_t pen; - uint8_t id[8]; - } _packed_ duid; + struct duid duid; + size_t duid_len; }; static const uint16_t default_req_opts[] = { @@ -133,15 +132,83 @@ int sd_dhcp6_client_set_index(sd_dhcp6_client *client, int interface_index) return 0; } -int sd_dhcp6_client_set_mac(sd_dhcp6_client *client, - const struct ether_addr *mac_addr) +int sd_dhcp6_client_set_mac(sd_dhcp6_client *client, const uint8_t *addr, + size_t addr_len, uint16_t arp_type) { assert_return(client, -EINVAL); - - if (mac_addr) - memcpy(&client->mac_addr, mac_addr, sizeof(client->mac_addr)); + assert_return(addr, -EINVAL); + assert_return(addr_len > 0 && addr_len <= MAX_MAC_ADDR_LEN, -EINVAL); + assert_return(arp_type > 0, -EINVAL); + + if (arp_type == ARPHRD_ETHER) + assert_return(addr_len == ETH_ALEN, -EINVAL); + else if (arp_type == ARPHRD_INFINIBAND) + assert_return(addr_len == INFINIBAND_ALEN, -EINVAL); else - memset(&client->mac_addr, 0x00, sizeof(client->mac_addr)); + return -EINVAL; + + if (client->mac_addr_len == addr_len && + memcmp(&client->mac_addr, addr, addr_len) == 0) + return 0; + + memcpy(&client->mac_addr, addr, addr_len); + client->mac_addr_len = addr_len; + client->arp_type = arp_type; + + return 0; +} + +int sd_dhcp6_client_set_duid(sd_dhcp6_client *client, uint16_t type, uint8_t *duid, + size_t duid_len) +{ + assert_return(client, -EINVAL); + assert_return(duid, -EINVAL); + assert_return(duid_len > 0 && duid_len <= MAX_DUID_LEN, -EINVAL); + + switch (type) { + case DHCP6_DUID_LLT: + if (duid_len <= sizeof(client->duid.llt)) + return -EINVAL; + break; + case DHCP6_DUID_EN: + if (duid_len != sizeof(client->duid.en)) + return -EINVAL; + break; + case DHCP6_DUID_LL: + if (duid_len <= sizeof(client->duid.ll)) + return -EINVAL; + break; + case DHCP6_DUID_UUID: + if (duid_len != sizeof(client->duid.uuid)) + return -EINVAL; + break; + default: + /* accept unknown type in order to be forward compatible */ + break; + } + + client->duid.type = htobe16(type); + memcpy(&client->duid.raw.data, duid, duid_len); + client->duid_len = duid_len + sizeof(client->duid.type); + + return 0; +} + +int sd_dhcp6_client_set_information_request(sd_dhcp6_client *client, + bool enabled) { + assert_return(client, -EINVAL); + + client->information_request = enabled; + + return 0; +} + +int sd_dhcp6_client_get_information_request(sd_dhcp6_client *client, + bool *enabled) { + assert_return(client, -EINVAL); + assert_return(enabled, -EINVAL); + + *enabled = client->information_request; return 0; } @@ -203,6 +270,7 @@ static int client_reset(sd_dhcp6_client *client) { client->fd = safe_close(client->fd); client->transaction_id = 0; + client->transaction_start = 0; client->ia_na.timeout_t1 = sd_event_source_unref(client->ia_na.timeout_t1); @@ -230,13 +298,15 @@ static void client_stop(sd_dhcp6_client *client, int error) { client_reset(client); } -static int client_send_message(sd_dhcp6_client *client) { +static int client_send_message(sd_dhcp6_client *client, usec_t time_now) { _cleanup_free_ DHCP6Message *message = NULL; struct in6_addr all_servers = IN6ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS_INIT; size_t len, optlen = 512; uint8_t *opt; int r; + usec_t elapsed_usec; + be16_t elapsed_time; len = sizeof(DHCP6Message) + optlen; @@ -249,6 +319,11 @@ static int client_send_message(sd_dhcp6_client *client) { message->transaction_id = client->transaction_id; switch(client->state) { + case DHCP6_STATE_INFORMATION_REQUEST: + message->type = DHCP6_INFORMATION_REQUEST; + + break; + case DHCP6_STATE_SOLICITATION: message->type = DHCP6_SOLICIT; @@ -293,7 +368,6 @@ static int client_send_message(sd_dhcp6_client *client) { break; case DHCP6_STATE_STOPPED: - case DHCP6_STATE_RS: case DHCP6_STATE_BOUND: return -EINVAL; } @@ -305,7 +379,18 @@ static int client_send_message(sd_dhcp6_client *client) { return r; r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_CLIENTID, - sizeof(client->duid), &client->duid); + client->duid_len, &client->duid); + if (r < 0) + return r; + + elapsed_usec = time_now - client->transaction_start; + if (elapsed_usec < 0xffff * USEC_PER_MSEC * 10) + elapsed_time = htobe16(elapsed_usec / USEC_PER_MSEC / 10); + else + elapsed_time = 0xffff; + + r = dhcp6_option_append(&opt, &optlen, DHCP6_OPTION_ELAPSED_TIME, + sizeof(elapsed_time), &elapsed_time); if (r < 0) return r; @@ -400,6 +485,12 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, client->timeout_resend = sd_event_source_unref(client->timeout_resend); switch (client->state) { + case DHCP6_STATE_INFORMATION_REQUEST: + init_retransmit_time = DHCP6_INF_TIMEOUT; + max_retransmit_time = DHCP6_INF_MAX_RT; + + break; + case DHCP6_STATE_SOLICITATION: if (client->retransmit_count && client->lease) { @@ -446,7 +537,6 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, break; case DHCP6_STATE_STOPPED: - case DHCP6_STATE_RS: case DHCP6_STATE_BOUND: return 0; } @@ -457,15 +547,14 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, return 0; } - r = client_send_message(client); - if (r >= 0) - client->retransmit_count++; - - - r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now); + r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); if (r < 0) goto error; + r = client_send_message(client, time_now); + if (r >= 0) + client->retransmit_count++; + if (!client->retransmit_time) { client->retransmit_time = client_timeout_compute_random(init_retransmit_time); @@ -486,7 +575,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, client->retransmit_time, 0)); r = sd_event_add_time(client->event, &client->timeout_resend, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), time_now + client->retransmit_time, 10 * USEC_PER_MSEC, client_timeout_resend, client); @@ -498,6 +587,10 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, if (r < 0) goto error; + r = sd_event_source_set_description(client->timeout_resend, "dhcp6-resend-timer"); + if (r < 0) + goto error; + if (max_retransmit_duration && !client->timeout_resend_expire) { log_dhcp6_client(client, "Max retransmission duration %"PRIu64" secs", @@ -505,7 +598,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, r = sd_event_add_time(client->event, &client->timeout_resend_expire, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), time_now + max_retransmit_duration, USEC_PER_SEC, client_timeout_resend_expire, client); @@ -516,6 +609,10 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, client->event_priority); if (r < 0) goto error; + + r = sd_event_source_set_description(client->timeout_resend_expire, "dhcp6-resend-expire-timer"); + if (r < 0) + goto error; } error: @@ -526,47 +623,16 @@ error: } static int client_ensure_iaid(sd_dhcp6_client *client) { - /* name is a pointer to memory in the udev_device struct, so must - have the same scope */ - _cleanup_udev_device_unref_ struct udev_device *device = NULL; - const char *name = NULL; - uint64_t id; + int r; 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; - 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); + r = dhcp_identifier_set_iaid(client->index, client->mac_addr, client->mac_addr_len, &client->ia_na.id); + if (r < 0) + return r; return 0; } @@ -575,12 +641,15 @@ static int client_parse_message(sd_dhcp6_client *client, DHCP6Message *message, size_t len, sd_dhcp6_lease *lease) { int r; - uint8_t *optval, *option = (uint8_t *)(message + 1), *id = NULL; + uint8_t *optval, *option, *id = NULL; uint16_t optcode, status; size_t optlen, id_len; bool clientid = false; be32_t iaid_lease; + option = (uint8_t *)message + sizeof(DHCP6Message); + len -= sizeof(DHCP6Message); + while ((r = dhcp6_option_parse(&option, &len, &optcode, &optlen, &optval)) >= 0) { switch (optcode) { @@ -591,7 +660,7 @@ static int client_parse_message(sd_dhcp6_client *client, return -EINVAL; } - if (optlen != sizeof(client->duid) || + if (optlen != client->duid_len || memcmp(&client->duid, optval, optlen) != 0) { log_dhcp6_client(client, "%s DUID does not match", dhcp6_message_type_to_string(message->type)); @@ -641,6 +710,12 @@ static int client_parse_message(sd_dhcp6_client *client, break; case DHCP6_OPTION_IA_NA: + if (client->state == DHCP6_STATE_INFORMATION_REQUEST) { + log_dhcp6_client(client, "Information request ignoring IA NA option"); + + break; + } + r = dhcp6_option_parse_ia(&optval, &optlen, optcode, &lease->ia); if (r < 0 && r != -ENOMSG) @@ -667,16 +742,21 @@ static int client_parse_message(sd_dhcp6_client *client, } } - if ((r < 0 && r != -ENOMSG) || !clientid) { + if (r == -ENOMSG) + r = 0; + + if (r < 0 || !clientid) { log_dhcp6_client(client, "%s has incomplete options", dhcp6_message_type_to_string(message->type)); return -EINVAL; } - r = dhcp6_lease_get_serverid(lease, &id, &id_len); - if (r < 0) - log_dhcp6_client(client, "%s has no server id", - dhcp6_message_type_to_string(message->type)); + if (client->state != DHCP6_STATE_INFORMATION_REQUEST) { + r = dhcp6_lease_get_serverid(lease, &id, &id_len); + if (r < 0) + log_dhcp6_client(client, "%s has no server id", + dhcp6_message_type_to_string(message->type)); + } return r; } @@ -708,12 +788,15 @@ static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply, return 0; } - if (client->lease) - dhcp6_lease_clear_timers(&client->lease->ia); + if (client->lease) { + dhcp6_lease_clear_timers(&client->lease->ia); + client->lease = sd_dhcp6_lease_unref(client->lease); + } - client->lease = sd_dhcp6_lease_unref(client->lease); - client->lease = lease; - lease = NULL; + if (client->state != DHCP6_STATE_INFORMATION_REQUEST) { + client->lease = lease; + lease = NULL; + } return DHCP6_STATE_BOUND; } @@ -740,7 +823,8 @@ static int client_receive_advertise(sd_dhcp6_client *client, return r; r = dhcp6_lease_get_preference(client->lease, &pref_lease); - if (!client->lease || r < 0 || pref_advertise > pref_lease) { + + if (r < 0 || pref_advertise > pref_lease) { sd_dhcp6_lease_unref(client->lease); client->lease = lease; lease = NULL; @@ -807,6 +891,17 @@ static int client_receive_message(sd_event_source *s, int fd, uint32_t revents, return 0; switch (client->state) { + case DHCP6_STATE_INFORMATION_REQUEST: + r = client_receive_reply(client, message, len); + if (r < 0) + return 0; + + client_notify(client, DHCP6_EVENT_INFORMATION_REQUEST); + + client_start(client, DHCP6_STATE_STOPPED); + + break; + case DHCP6_STATE_SOLICITATION: r = client_receive_advertise(client, message, len); @@ -843,7 +938,6 @@ static int client_receive_message(sd_event_source *s, int fd, uint32_t revents, break; case DHCP6_STATE_STOPPED: - case DHCP6_STATE_RS: return 0; } @@ -872,36 +966,30 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) client->retransmit_time = 0; client->retransmit_count = 0; - switch (state) { - case DHCP6_STATE_STOPPED: - case DHCP6_STATE_RS: - case DHCP6_STATE_SOLICITATION: - - r = client_ensure_iaid(client); - if (r < 0) - return r; - - r = dhcp6_network_bind_udp_socket(client->index, NULL); + if (client->state == DHCP6_STATE_STOPPED) { + time_now = now(clock_boottime_or_monotonic()); + } else { + r = sd_event_now(client->event, clock_boottime_or_monotonic(), + &time_now); if (r < 0) return r; + } - client->fd = r; - - r = sd_event_add_io(client->event, &client->receive_message, - client->fd, EPOLLIN, client_receive_message, - client); - if (r < 0) - return r; + switch (state) { + case DHCP6_STATE_STOPPED: + if (client->state == DHCP6_STATE_INFORMATION_REQUEST) { + client->state = DHCP6_STATE_STOPPED; - r = sd_event_source_set_priority(client->receive_message, - client->event_priority); - if (r < 0) - return r; + return 0; + } + /* fall through */ + case DHCP6_STATE_SOLICITATION: client->state = DHCP6_STATE_SOLICITATION; break; + case DHCP6_STATE_INFORMATION_REQUEST: case DHCP6_STATE_REQUEST: case DHCP6_STATE_RENEW: case DHCP6_STATE_REBIND: @@ -912,10 +1000,6 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) case DHCP6_STATE_BOUND: - r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now); - if (r < 0) - return r; - if (client->lease->ia.lifetime_t1 == 0xffffffff || client->lease->ia.lifetime_t2 == 0xffffffff) { @@ -935,7 +1019,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) r = sd_event_add_time(client->event, &client->lease->ia.timeout_t1, - CLOCK_MONOTONIC, time_now + timeout, + clock_boottime_or_monotonic(), time_now + timeout, 10 * USEC_PER_SEC, client_timeout_t1, client); if (r < 0) @@ -946,6 +1030,10 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) if (r < 0) return r; + r = sd_event_source_set_description(client->lease->ia.timeout_t1, "dhcp6-t1-timeout"); + if (r < 0) + return r; + timeout = client_timeout_compute_random(be32toh(client->lease->ia.lifetime_t2) * USEC_PER_SEC); log_dhcp6_client(client, "T2 expires in %s", @@ -955,7 +1043,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) r = sd_event_add_time(client->event, &client->lease->ia.timeout_t2, - CLOCK_MONOTONIC, time_now + timeout, + clock_boottime_or_monotonic(), time_now + timeout, 10 * USEC_PER_SEC, client_timeout_t2, client); if (r < 0) @@ -966,15 +1054,20 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) if (r < 0) return r; + r = sd_event_source_set_description(client->lease->ia.timeout_t2, "dhcp6-t2-timeout"); + if (r < 0) + return r; + client->state = state; return 0; } client->transaction_id = random_u32() & htobe32(0x00ffffff); + client->transaction_start = time_now; r = sd_event_add_time(client->event, &client->timeout_resend, - CLOCK_MONOTONIC, 0, 0, client_timeout_resend, + clock_boottime_or_monotonic(), 0, 0, client_timeout_resend, client); if (r < 0) return r; @@ -984,6 +1077,10 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state) if (r < 0) return r; + r = sd_event_source_set_description(client->timeout_resend, "dhcp6-resend-timeout"); + if (r < 0) + return r; + return 0; } @@ -997,6 +1094,7 @@ int sd_dhcp6_client_stop(sd_dhcp6_client *client) int sd_dhcp6_client_start(sd_dhcp6_client *client) { int r = 0; + enum DHCP6State state = DHCP6_STATE_SOLICITATION; assert_return(client, -EINVAL); assert_return(client->event, -EINVAL); @@ -1006,7 +1104,44 @@ int sd_dhcp6_client_start(sd_dhcp6_client *client) if (r < 0) return r; - return client_start(client, DHCP6_STATE_SOLICITATION); + r = client_ensure_iaid(client); + if (r < 0) + return r; + + r = dhcp6_network_bind_udp_socket(client->index, NULL); + if (r < 0) + return r; + + client->fd = r; + + r = sd_event_add_io(client->event, &client->receive_message, + client->fd, EPOLLIN, client_receive_message, + client); + if (r < 0) + goto error; + + r = sd_event_source_set_priority(client->receive_message, + client->event_priority); + if (r < 0) + goto error; + + r = sd_event_source_set_description(client->receive_message, + "dhcp6-receive-message"); + if (r < 0) + goto error; + + if (client->information_request) + state = DHCP6_STATE_INFORMATION_REQUEST; + + log_dhcp6_client(client, "Started in %s mode", + client->information_request? "Information request": + "Managed"); + + return client_start(client, state); + +error: + client_reset(client); + return r; } int sd_dhcp6_client_attach_event(sd_dhcp6_client *client, sd_event *event, @@ -1053,7 +1188,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) { + if (client && REFCNT_DEC(client->n_ref) == 0) { client_reset(client); sd_dhcp6_client_detach_event(client); @@ -1070,7 +1205,6 @@ sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) { int sd_dhcp6_client_new(sd_dhcp6_client **ret) { _cleanup_dhcp6_client_unref_ sd_dhcp6_client *client = NULL; - sd_id128_t machine_id; int r; size_t t; @@ -1089,18 +1223,10 @@ int sd_dhcp6_client_new(sd_dhcp6_client **ret) client->fd = -1; /* initialize DUID */ - client->duid.type = htobe16(DHCP6_DUID_EN); - client->duid.pen = htobe32(SYSTEMD_PEN); - - r = sd_id128_get_machine(&machine_id); + r = dhcp_identifier_set_duid_en(&client->duid, &client->duid_len); if (r < 0) return r; - /* a bit of snake-oil perhaps, but no need to expose the machine-id - directly */ - siphash24(client->duid.id, &machine_id, sizeof(machine_id), - HASH_KEY.bytes); - client->req_opts_len = ELEMENTSOF(default_req_opts); client->req_opts = new0(be16_t, client->req_opts_len);