Commit 44eb50d1 authored by Dan Winship's avatar Dan Winship

dhcp: update system-dhcp code from upstream

parents af525e8a e30b0714
......@@ -99,6 +99,7 @@ libsystemd_dhcp_la_SOURCES = \
dhcp-manager/systemd-dhcp/src/shared/fileio.c \
dhcp-manager/systemd-dhcp/src/shared/strv.h \
dhcp-manager/systemd-dhcp/src/shared/strv.c \
dhcp-manager/systemd-dhcp/src/shared/unaligned.h \
dhcp-manager/systemd-dhcp/src/shared/utf8.h \
dhcp-manager/systemd-dhcp/src/shared/utf8.c \
dhcp-manager/systemd-dhcp/src/systemd/sd-dhcp-lease.h \
......
......@@ -389,7 +389,7 @@ nm_dhcp_systemd_get_lease_ip_configs (const char *iface,
return NULL;
path = get_leasefile_path (iface, uuid, FALSE);
r = sd_dhcp_lease_load (path, &lease);
r = sd_dhcp_lease_load (&lease, path);
if (r == 0) {
ip4_config = lease_to_ip4_config (lease, NULL, 0, FALSE, NULL);
if (ip4_config)
......@@ -457,7 +457,7 @@ bound4_handle (NMDhcpSystemd *self)
add_requests_to_options (options, dhcp4_requests);
sd_dhcp_lease_save (lease, priv->lease_file);
client_id = sd_dhcp_client_get_client_id(priv->client4, &type, &client_id_len);
sd_dhcp_client_get_client_id(priv->client4, &type, &client_id, &client_id_len);
if (client_id)
_save_client_id (self, type, client_id, client_id_len);
......@@ -576,7 +576,7 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last
goto error;
}
sd_dhcp_lease_load (priv->lease_file, &lease);
sd_dhcp_lease_load (&lease, priv->lease_file);
if (last_ip4_address)
inet_pton (AF_INET, last_ip4_address, &last_addr);
......
......@@ -68,12 +68,12 @@ sd_event_source_unref (sd_event_source *s)
}
int
sd_event_source_set_name(sd_event_source *s, const char *name)
sd_event_source_set_description(sd_event_source *s, const char *description)
{
if (!s)
return -EINVAL;
g_source_set_name_by_id (s->id, name);
g_source_set_name_by_id (s->id, description);
return 0;
}
......
......@@ -35,7 +35,7 @@
struct sd_dhcp_route {
struct in_addr dst_addr;
struct in_addr gw_addr;
uint8_t dst_prefixlen;
unsigned char dst_prefixlen;
};
struct sd_dhcp_lease {
......
......@@ -28,31 +28,37 @@
#include <string.h>
#include "sparse-endian.h"
#include "unaligned.h"
#include "util.h"
#include "dhcp6-internal.h"
#include "dhcp6-protocol.h"
#define DHCP6_OPTION_HDR_LEN 4
#define DHCP6_OPTION_IA_NA_LEN 12
#define DHCP6_OPTION_IA_TA_LEN 4
typedef struct DHCP6Option {
be16_t code;
be16_t len;
uint8_t data[];
} _packed_ DHCP6Option;
static int option_append_hdr(uint8_t **buf, size_t *buflen, uint16_t optcode,
size_t optlen) {
DHCP6Option *option = (DHCP6Option*) *buf;
assert_return(buf, -EINVAL);
assert_return(*buf, -EINVAL);
assert_return(buflen, -EINVAL);
if (optlen > 0xffff || *buflen < optlen + DHCP6_OPTION_HDR_LEN)
if (optlen > 0xffff || *buflen < optlen + sizeof(DHCP6Option))
return -ENOBUFS;
(*buf)[0] = optcode >> 8;
(*buf)[1] = optcode & 0xff;
(*buf)[2] = optlen >> 8;
(*buf)[3] = optlen & 0xff;
option->code = htobe16(optcode);
option->len = htobe16(optlen);
*buf += DHCP6_OPTION_HDR_LEN;
*buflen -= DHCP6_OPTION_HDR_LEN;
*buf += sizeof(DHCP6Option);
*buflen -= sizeof(DHCP6Option);
return 0;
}
......@@ -104,8 +110,8 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
ia_hdr = *buf;
ia_buflen = *buflen;
*buf += DHCP6_OPTION_HDR_LEN;
*buflen -= DHCP6_OPTION_HDR_LEN;
*buf += sizeof(DHCP6Option);
*buflen -= sizeof(DHCP6Option);
memcpy(*buf, &ia->id, len);
......@@ -123,7 +129,7 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
*buf += sizeof(addr->iaaddr);
*buflen -= sizeof(addr->iaaddr);
ia_addrlen += DHCP6_OPTION_HDR_LEN + sizeof(addr->iaaddr);
ia_addrlen += sizeof(DHCP6Option) + sizeof(addr->iaaddr);
}
r = option_append_hdr(&ia_hdr, &ia_buflen, ia->type, len + ia_addrlen);
......@@ -134,23 +140,23 @@ int dhcp6_option_append_ia(uint8_t **buf, size_t *buflen, DHCP6IA *ia) {
}
static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *opt,
size_t *optlen) {
static int option_parse_hdr(uint8_t **buf, size_t *buflen, uint16_t *optcode, size_t *optlen) {
DHCP6Option *option = (DHCP6Option*) *buf;
uint16_t len;
assert_return(buf, -EINVAL);
assert_return(opt, -EINVAL);
assert_return(optcode, -EINVAL);
assert_return(optlen, -EINVAL);
if (*buflen < 4)
if (*buflen < sizeof(DHCP6Option))
return -ENOMSG;
len = (*buf)[2] << 8 | (*buf)[3];
len = be16toh(option->len);
if (len > *buflen)
return -ENOMSG;
*opt = (*buf)[0] << 8 | (*buf)[1];
*optcode = be16toh(option->code);
*optlen = len;
*buf += 4;
......@@ -194,7 +200,7 @@ int dhcp6_option_parse_ia(uint8_t **buf, size_t *buflen, uint16_t iatype,
switch (iatype) {
case DHCP6_OPTION_IA_NA:
if (*buflen < DHCP6_OPTION_IA_NA_LEN + DHCP6_OPTION_HDR_LEN +
if (*buflen < DHCP6_OPTION_IA_NA_LEN + sizeof(DHCP6Option) +
sizeof(addr->iaaddr)) {
r = -ENOBUFS;
goto error;
......@@ -216,7 +222,7 @@ int dhcp6_option_parse_ia(uint8_t **buf, size_t *buflen, uint16_t iatype,
break;
case DHCP6_OPTION_IA_TA:
if (*buflen < DHCP6_OPTION_IA_TA_LEN + DHCP6_OPTION_HDR_LEN +
if (*buflen < DHCP6_OPTION_IA_TA_LEN + sizeof(DHCP6Option) +
sizeof(addr->iaaddr)) {
r = -ENOBUFS;
goto error;
......
......@@ -109,16 +109,16 @@ bool net_match_config(const struct ether_addr *match_mac,
const char *dev_type,
const char *dev_name) {
if (match_host && !condition_test_host(match_host))
if (match_host && !condition_test(match_host))
return 0;
if (match_virt && !condition_test_virtualization(match_virt))
if (match_virt && !condition_test(match_virt))
return 0;
if (match_kernel && !condition_test_kernel_command_line(match_kernel))
if (match_kernel && !condition_test(match_kernel))
return 0;
if (match_arch && !condition_test_architecture(match_arch))
if (match_arch && !condition_test(match_arch))
return 0;
if (match_mac && (!dev_mac || memcmp(match_mac, dev_mac, ETH_ALEN)))
......
......@@ -29,7 +29,7 @@
#if 0 /* NM_IGNORED */
#include "udev.h"
#include "condition-util.h"
#include "condition.h"
bool net_match_config(const struct ether_addr *match_mac,
const char *match_path,
......
......@@ -42,7 +42,7 @@
#include "dhcp-lease-internal.h"
#include "sd-dhcp-client.h"
#define MAX_CLIENT_ID_LEN 32
#define MAX_CLIENT_ID_LEN 64 /* Arbitrary limit */
#define MAX_MAC_ADDR_LEN INFINIBAND_ALEN
struct sd_dhcp_client {
......@@ -231,20 +231,25 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client, const uint8_t *addr,
return 0;
}
const uint8_t *sd_dhcp_client_get_client_id(sd_dhcp_client *client,
uint8_t *type,
size_t *len) {
int sd_dhcp_client_get_client_id(sd_dhcp_client *client, uint8_t *type,
const uint8_t **data, size_t *data_len) {
assert_return(client, NULL);
assert_return(type, NULL);
assert_return(len, NULL);
assert_return(client, -EINVAL);
assert_return(type, -EINVAL);
assert_return(data, -EINVAL);
assert_return(data_len, -EINVAL);
if (!client->client_id_len)
return NULL;
*type = 0;
*data = NULL;
*data_len = 0;
if (client->client_id_len) {
*type = client->client_id.raw.type;
*data = client->client_id.raw.data;
*data_len = client->client_id_len -
sizeof (client->client_id.raw.type);
}
*type = client->client_id.raw.type;
*len = client->client_id_len - 1; /* -1 for sizeof(type) */
return (const uint8_t *) &client->client_id.raw.data;
return 0;
}
int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type,
......@@ -269,7 +274,7 @@ int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type,
break;
}
if (client->client_id_len == data_len + 1 &&
if (client->client_id_len == data_len + sizeof (client->client_id.raw.type) &&
client->client_id.raw.type == type &&
memcmp(&client->client_id.raw.data, data, data_len) == 0)
return 0;
......@@ -283,7 +288,7 @@ int sd_dhcp_client_set_client_id(sd_dhcp_client *client, uint8_t type,
client->client_id.raw.type = type;
memcpy(&client->client_id.raw.data, data, data_len);
client->client_id_len = data_len + 1; /* +1 for sizeof(type) */
client->client_id_len = data_len + sizeof (client->client_id.raw.type);
if (need_restart && client->state != DHCP_STATE_STOPPED)
sd_dhcp_client_start(client);
......@@ -814,8 +819,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
if (r < 0)
goto error;
r = sd_event_source_set_name(client->timeout_resend,
"dhcp4-resend-timer");
r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
if (r < 0)
goto error;
......@@ -892,8 +896,7 @@ static int client_initialize_io_events(sd_dhcp_client *client,
if (r < 0)
goto error;
r = sd_event_source_set_name(client->receive_message,
"dhcp4-receive-message");
r = sd_event_source_set_description(client->receive_message, "dhcp4-receive-message");
if (r < 0)
goto error;
......@@ -923,8 +926,7 @@ static int client_initialize_time_events(sd_dhcp_client *client) {
r = sd_event_source_set_priority(client->timeout_resend,
client->event_priority);
r = sd_event_source_set_name(client->timeout_resend,
"dhcp4-resend-timer");
r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
if (r < 0)
goto error;
......@@ -1035,7 +1037,7 @@ static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer,
if (client->client_id_len) {
r = dhcp_lease_set_client_id(lease,
(uint8_t *) &client->client_id,
(uint8_t *) &client->client_id.raw,
client->client_id_len);
if (r < 0)
return r;
......@@ -1102,7 +1104,7 @@ static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack,
if (client->client_id_len) {
r = dhcp_lease_set_client_id(lease,
(uint8_t *) &client->client_id,
(uint8_t *) &client->client_id.raw,
client->client_id_len);
if (r < 0)
return r;
......@@ -1254,8 +1256,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
if (r < 0)
return r;
r = sd_event_source_set_name(client->timeout_expire,
"dhcp4-lifetime");
r = sd_event_source_set_description(client->timeout_expire, "dhcp4-lifetime");
if (r < 0)
return r;
......@@ -1282,8 +1283,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
if (r < 0)
return r;
r = sd_event_source_set_name(client->timeout_t2,
"dhcp4-t2-timeout");
r = sd_event_source_set_description(client->timeout_t2, "dhcp4-t2-timeout");
if (r < 0)
return r;
......@@ -1309,8 +1309,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) {
if (r < 0)
return r;
r = sd_event_source_set_name(client->timeout_t1,
"dhcp4-t1-timer");
r = sd_event_source_set_description(client->timeout_t1, "dhcp4-t1-timer");
if (r < 0)
return r;
......@@ -1355,8 +1354,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
if (r < 0)
goto error;
r = sd_event_source_set_name(client->timeout_resend,
"dhcp4-resend-timer");
r = sd_event_source_set_description(client->timeout_resend, "dhcp4-resend-timer");
if (r < 0)
goto error;
} else if (r == -ENOMSG)
......
......@@ -36,6 +36,7 @@
#include "mkdir.h"
#endif
#include "fileio.h"
#include "unaligned.h"
#include "in-addr-util.h"
#include "dhcp-protocol.h"
......@@ -212,14 +213,11 @@ sd_dhcp_lease *sd_dhcp_lease_unref(sd_dhcp_lease *lease) {
}
static void lease_parse_u32(const uint8_t *option, size_t len, uint32_t *ret, uint32_t min) {
be32_t val;
assert(option);
assert(ret);
if (len == 4) {
memcpy(&val, option, 4);
*ret = be32toh(val);
*ret = unaligned_read_be32((be32_t*) option);
if (*ret < min)
*ret = min;
......@@ -231,14 +229,11 @@ static void lease_parse_s32(const uint8_t *option, size_t len, int32_t *ret) {
}
static void lease_parse_u16(const uint8_t *option, size_t len, uint16_t *ret, uint16_t min) {
be16_t val;
assert(option);
assert(ret);
if (len == 2) {
memcpy(&val, option, 2);
*ret = be16toh(val);
*ret = unaligned_read_be16((be16_t*) option);
if (*ret < min)
*ret = min;
......@@ -322,23 +317,6 @@ static int lease_parse_in_addrs_pairs(const uint8_t *option, size_t len, struct
return lease_parse_in_addrs_aux(option, len, ret, ret_size, 2);
}
static int class_prefixlen(uint8_t msb_octet, uint8_t *ret) {
if (msb_octet < 128)
/* Class A */
*ret = 8;
else if (msb_octet < 192)
/* Class B */
*ret = 16;
else if (msb_octet < 224)
/* Class C */
*ret = 24;
else
/* Class D or E -- no subnet mask */
return -ERANGE;
return 0;
}
static int lease_parse_routes(const uint8_t *option, size_t len, struct sd_dhcp_route **routes,
size_t *routes_size, size_t *routes_allocated) {
......@@ -360,8 +338,10 @@ static int lease_parse_routes(const uint8_t *option, size_t len, struct sd_dhcp_
while (len >= 8) {
struct sd_dhcp_route *route = *routes + *routes_size;
int r;
if (class_prefixlen(*option, &route->dst_prefixlen) < 0) {
r = in_addr_default_prefixlen((struct in_addr*) option, &route->dst_prefixlen);
if (r < 0) {
log_error("Failed to determine destination prefix length from class based IP, ignoring");
continue;
}
......@@ -716,7 +696,7 @@ finish:
return r;
}
int sd_dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
int sd_dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
_cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
_cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
*server_address = NULL, *next_server = NULL,
......
......@@ -597,8 +597,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
if (r < 0)
goto error;
r = sd_event_source_set_name(client->timeout_resend,
"dhcp6-resend-timer");
r = sd_event_source_set_description(client->timeout_resend, "dhcp6-resend-timer");
if (r < 0)
goto error;
......@@ -621,8 +620,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
if (r < 0)
goto error;
r = sd_event_source_set_name(client->timeout_resend_expire,
"dhcp6-resend-expire-timer");
r = sd_event_source_set_description(client->timeout_resend_expire, "dhcp6-resend-expire-timer");
if (r < 0)
goto error;
}
......@@ -994,8 +992,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
if (r < 0)
return r;
r = sd_event_source_set_name(client->receive_message,
"dhcp6-receive-message");
r = sd_event_source_set_description(client->receive_message, "dhcp6-receive-message");
if (r < 0)
return r;
......@@ -1043,8 +1040,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
if (r < 0)
return r;
r = sd_event_source_set_name(client->lease->ia.timeout_t1,
"dhcp6-t1-timeout");
r = sd_event_source_set_description(client->lease->ia.timeout_t1, "dhcp6-t1-timeout");
if (r < 0)
return r;
......@@ -1068,8 +1064,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
if (r < 0)
return r;
r = sd_event_source_set_name(client->lease->ia.timeout_t2,
"dhcp6-t2-timeout");
r = sd_event_source_set_description(client->lease->ia.timeout_t2, "dhcp6-t2-timeout");
if (r < 0)
return r;
......@@ -1092,8 +1087,7 @@ static int client_start(sd_dhcp6_client *client, enum DHCP6State state)
if (r < 0)
return r;
r = sd_event_source_set_name(client->timeout_resend,
"dhcp6-resend-timeout");
r = sd_event_source_set_description(client->timeout_resend, "dhcp6-resend-timeout");
if (r < 0)
return r;
......
......@@ -22,12 +22,12 @@
#include "config.h"
#include <unistd.h>
#include <sys/sendfile.h>
#include "fileio.h"
#include "util.h"
#include "strv.h"
#include "utf8.h"
#include "ctype.h"
#include "fileio.h"
int write_string_stream(FILE *f, const char *line) {
assert(f);
......@@ -68,7 +68,7 @@ int write_string_file_no_create(const char *fn, const char *line) {
assert(line);
/* We manually build our own version of fopen(..., "we") that
* without O_CREAT */
* works without O_CREAT */
fd = open(fn, O_WRONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return -errno;
......@@ -96,20 +96,10 @@ int write_string_file_atomic(const char *fn, const char *line) {
fchmod_umask(fileno(f), 0644);
errno = 0;
fputs(line, f);
if (!endswith(line, "\n"))
fputc('\n', f);
fflush(f);
if (ferror(f))
r = errno ? -errno : -EIO;
else {
r = write_string_stream(f, line);
if (r >= 0) {
if (rename(p, fn) < 0)
r = -errno;
else
r = 0;
}
if (r < 0)
......@@ -146,77 +136,6 @@ int read_one_line_file(const char *fn, char **line) {
return 0;
}
ssize_t sendfile_full(int out_fd, const char *fn) {
_cleanup_fclose_ FILE *f = NULL;
struct stat st;
int r;
ssize_t s;
size_t n, l;
_cleanup_free_ char *buf = NULL;
assert(out_fd > 0);
assert(fn);
f = fopen(fn, "re");
if (!f)
return -errno;
r = fstat(fileno(f), &st);
if (r < 0)
return -errno;
s = sendfile(out_fd, fileno(f), NULL, st.st_size);
if (s < 0)
if (errno == EINVAL || errno == ENOSYS) {
/* continue below */
} else
return -errno;
else
return s;
/* sendfile() failed, fall back to read/write */
/* Safety check */
if (st.st_size > 4*1024*1024)
return -E2BIG;
n = st.st_size > 0 ? st.st_size : LINE_MAX;
l = 0;
while (true) {
char *t;
size_t k;
t = realloc(buf, n);
if (!t)
return -ENOMEM;
buf = t;
k = fread(buf + l, 1, n - l, f);
if (k <= 0) {
if (ferror(f))
return -errno;
break;
}
l += k;
n *= 2;
/* Safety check */
if (n > 4*1024*1024)
return -E2BIG;
}
r = write(out_fd, buf, l);
if (r < 0)
return -errno;
return (ssize_t) l;
}