dhcp: update test and header cleanup (#42256)

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-27 14:23:34 +02:00
committed by GitHub
42 changed files with 219 additions and 1289 deletions
@@ -1,13 +1,11 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <linux/if_ether.h>
#include "sd-dhcp-client-id.h"
#include "dhcp-duid-internal.h"
#include "sd-forward.h"
#include "sparse-endian.h"
#include "dhcp-forward.h"
#include "ether-addr-util.h"
/* RFC 2132 section 9.14: its minimum length is 2.
* Note, its maximum is not mentioend in the RFC. Hence, 255. */
@@ -4,13 +4,11 @@
#include "sd-dhcp-client.h"
#include "dhcp-client-id-internal.h"
#include "dhcp-forward.h"
#include "dhcp-protocol.h"
#include "ether-addr-util.h"
#include "iovec-wrapper.h"
#include "network-common.h"
#include "sd-forward.h"
#include "socket-util.h"
#include "tlv-util.h"
struct sd_dhcp_client {
unsigned n_ref;
+1 -1
View File
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-forward.h"
#include "dhcp-forward.h"
int dhcp_client_send_message(sd_dhcp_client *client, uint8_t type);
+1 -2
View File
@@ -4,9 +4,8 @@
#include "sd-dhcp-duid.h"
#include "sd-id128.h"
#include "dhcp-forward.h"
#include "ether-addr-util.h"
#include "sd-forward.h"
#include "sparse-endian.h"
#define SYSTEMD_PEN 43793
+26
View File
@@ -0,0 +1,26 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-forward.h"
#include "sparse-endian.h"
struct in_pktinfo;
struct iphdr;
struct udphdr;
typedef struct sd_dhcp_client sd_dhcp_client;
typedef struct sd_dhcp_client_id sd_dhcp_client_id;
typedef struct sd_dhcp_duid sd_dhcp_duid;
typedef struct sd_dhcp_lease sd_dhcp_lease;
typedef struct sd_dhcp_message sd_dhcp_message;
typedef struct sd_dhcp_relay sd_dhcp_relay;
typedef struct sd_dhcp_relay_interface sd_dhcp_relay_interface;
typedef struct sd_dhcp_route sd_dhcp_route;
typedef struct sd_dhcp_server sd_dhcp_server;
typedef struct sd_dhcp_server_lease sd_dhcp_server_lease;
typedef struct DHCPMessageHeader DHCPMessageHeader;
typedef struct DHCPRequest DHCPRequest;
typedef struct DHCPServerData DHCPServerData;
typedef struct TLV TLV;
+5 -4
View File
@@ -5,12 +5,13 @@
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include "sd-dhcp-lease.h"
#include "sd-forward.h"
#include <netinet/in.h>
#include "dhcp-client-id-internal.h"
#include "sd-dhcp-lease.h"
#include "dhcp-forward.h"
#include "dhcp-message.h"
#include "dhcp-option.h"
#include "time-util.h"
struct sd_dhcp_lease {
unsigned n_ref;
+1 -1
View File
@@ -3,8 +3,8 @@
#include "alloc-util.h"
#include "ansi-color.h"
#include "arphrd-util.h"
#include "dhcp-message.h"
#include "dhcp-message-dump.h"
#include "dhcp-protocol.h"
#include "dhcp-route.h"
#include "dns-resolver-internal.h"
#include "escape.h"
+1 -1
View File
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "dhcp-message.h"
#include "dhcp-forward.h"
typedef enum {
DUMP_DHCP_MESSAGE_LEGEND = 1 << 0,
-1
View File
@@ -5,7 +5,6 @@
#include "alloc-util.h"
#include "dhcp-client-id-internal.h"
#include "dhcp-message.h"
#include "dhcp-protocol.h"
#include "dhcp-route.h"
#include "dns-def.h"
#include "dns-domain.h"
+14 -4
View File
@@ -1,12 +1,22 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-dhcp-client-id.h"
#include "sd-forward.h"
#include "sparse-endian.h"
#include "dhcp-forward.h"
#include "dhcp-protocol.h"
#include "tlv-util.h"
typedef struct DHCPServerData {
struct in_addr *addr;
size_t size;
} DHCPServerData;
struct sd_dhcp_message {
unsigned n_ref;
DHCPMessageHeader header;
TLV options;
};
typedef struct sd_dhcp_message sd_dhcp_message;
sd_dhcp_message* sd_dhcp_message_ref(sd_dhcp_message *p);
-94
View File
@@ -1,94 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include <netinet/ip.h>
#include "dhcp-network.h"
#include "dhcp-protocol.h"
#include "fd-util.h"
#include "iovec-wrapper.h"
#include "socket-util.h"
int dhcp_network_bind_udp_socket(int ifindex, be32_t address, uint16_t port, int ip_service_type) {
union sockaddr_union src = {
.in.sin_family = AF_INET,
.in.sin_port = htobe16(port),
.in.sin_addr.s_addr = address,
};
_cleanup_close_ int s = -EBADF;
int r;
s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
if (s < 0)
return -errno;
if (ip_service_type >= 0)
r = setsockopt_int(s, IPPROTO_IP, IP_TOS, ip_service_type);
else
r = setsockopt_int(s, IPPROTO_IP, IP_TOS, IPTOS_CLASS_CS6);
if (r < 0)
return r;
r = setsockopt_int(s, SOL_SOCKET, SO_REUSEADDR, true);
if (r < 0)
return r;
r = setsockopt_int(s, SOL_SOCKET, SO_TIMESTAMP, true);
if (r < 0)
return r;
if (ifindex > 0) {
r = socket_bind_to_ifindex(s, ifindex);
if (r < 0)
return r;
}
if (port == DHCP_PORT_SERVER) {
r = setsockopt_int(s, SOL_SOCKET, SO_BROADCAST, true);
if (r < 0)
return r;
if (address == INADDR_ANY) {
/* IP_PKTINFO filter should not be applied when packets are
allowed to enter/leave through the interface other than
DHCP server sits on(BindToInterface option). */
r = setsockopt_int(s, IPPROTO_IP, IP_PKTINFO, true);
if (r < 0)
return r;
}
} else {
r = setsockopt_int(s, IPPROTO_IP, IP_FREEBIND, true);
if (r < 0)
return r;
}
if (bind(s, &src.sa, sizeof(src.in)) < 0)
return -errno;
return TAKE_FD(s);
}
int dhcp_network_send_raw_socket(
int fd,
const union sockaddr_union *link,
const struct iovec_wrapper *iovw) {
/* Do not add assert(fd >= 0) here, as this is also called from fuzz-dhcp-server, and in that case
* fd is negative and this function should fail with negative errno. */
assert(link);
assert(!iovw_isempty(iovw));
struct msghdr mh = {
.msg_name = (struct sockaddr*) &link->sa,
.msg_namelen = sockaddr_ll_len(&link->ll),
.msg_iov = iovw->iovec,
.msg_iovlen = iovw->count,
};
if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0)
return -errno;
return 0;
}
-15
View File
@@ -1,15 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-forward.h"
#include "sparse-endian.h"
int dhcp_network_bind_udp_socket(
int ifindex,
be32_t address,
uint16_t port,
int ip_service_type);
int dhcp_network_send_raw_socket(
int fd,
const union sockaddr_union *link,
const struct iovec_wrapper *iovw);
-367
View File
@@ -1,367 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include <stdio.h>
#include "alloc-util.h"
#include "dhcp-option.h"
#include "dns-domain.h"
#include "hostname-util.h"
#include "memory-util.h"
#include "string-util.h"
#include "utf8.h"
/* Append type-length value structure to the options buffer */
static int dhcp_option_append_tlv(uint8_t options[], size_t size, size_t *offset, uint8_t code, size_t optlen, const void *optval) {
assert(options);
assert(size > 0);
assert(offset);
assert(optlen <= UINT8_MAX);
assert(*offset < size);
if (*offset + 2 + optlen > size)
return -ENOBUFS;
options[*offset] = code;
options[*offset + 1] = optlen;
memcpy_safe(&options[*offset + 2], optval, optlen);
*offset += 2 + optlen;
return 0;
}
static int option_append(uint8_t options[], size_t size, size_t *offset,
uint8_t code, size_t optlen, const void *optval) {
assert(options);
assert(size > 0);
assert(offset);
if (code != SD_DHCP_OPTION_END)
/* always make sure there is space for an END option */
size--;
switch (code) {
case SD_DHCP_OPTION_PAD:
case SD_DHCP_OPTION_END:
if (*offset + 1 > size)
return -ENOBUFS;
options[*offset] = code;
*offset += 1;
break;
case SD_DHCP_OPTION_SIP_SERVER:
if (*offset + 3 + optlen > size)
return -ENOBUFS;
options[*offset] = code;
options[*offset + 1] = optlen + 1;
options[*offset + 2] = 1;
memcpy_safe(&options[*offset + 3], optval, optlen);
*offset += 3 + optlen;
break;
default:
return dhcp_option_append_tlv(options, size, offset, code, optlen, optval);
}
return 0;
}
static int option_length(uint8_t *options, size_t length, size_t offset) {
assert(options);
assert(offset < length);
if (IN_SET(options[offset], SD_DHCP_OPTION_PAD, SD_DHCP_OPTION_END))
return 1;
if (length < offset + 2)
return -ENOBUFS;
/* validating that buffer is long enough */
if (length < offset + 2 + options[offset + 1])
return -ENOBUFS;
return options[offset + 1] + 2;
}
int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t code, size_t *ret_offset) {
int r;
assert(options);
assert(ret_offset);
for (size_t offset = 0; offset < length; offset += r) {
r = option_length(options, length, offset);
if (r < 0)
return r;
if (code == options[offset]) {
*ret_offset = offset;
return r;
}
}
return -ENOENT;
}
int dhcp_option_remove_option(uint8_t *options, size_t length, uint8_t option_code) {
int r;
size_t offset;
assert(options);
r = dhcp_option_find_option(options, length, option_code, &offset);
if (r < 0)
return r;
memmove(options + offset, options + offset + r, length - offset - r);
return length - r;
}
int dhcp_option_append(DHCPMessage *message, size_t size, size_t *offset,
uint8_t overload,
uint8_t code, size_t optlen, const void *optval) {
const bool use_file = overload & DHCP_OVERLOAD_FILE;
const bool use_sname = overload & DHCP_OVERLOAD_SNAME;
int r;
assert(message);
assert(offset);
/* If *offset is in range [0, size), we are writing to ->options,
* if *offset is in range [size, size + sizeof(message->file)) and use_file, we are writing to ->file,
* if *offset is in range [size + use_file*sizeof(message->file), size + use_file*sizeof(message->file) + sizeof(message->sname))
* and use_sname, we are writing to ->sname.
*/
if (*offset < size) {
/* still space in the options array */
r = option_append(message->options, size, offset, code, optlen, optval);
if (r >= 0)
return 0;
else if (r == -ENOBUFS && (use_file || use_sname)) {
/* did not fit, but we have more buffers to try
close the options array and move the offset to its end */
r = option_append(message->options, size, offset, SD_DHCP_OPTION_END, 0, NULL);
if (r < 0)
return r;
*offset = size;
} else
return r;
}
if (use_file) {
size_t file_offset = *offset - size;
if (file_offset < sizeof(message->file)) {
/* still space in the 'file' array */
r = option_append(message->file, sizeof(message->file), &file_offset, code, optlen, optval);
if (r >= 0) {
*offset = size + file_offset;
return 0;
} else if (r == -ENOBUFS && use_sname) {
/* did not fit, but we have more buffers to try
close the file array and move the offset to its end */
r = option_append(message->file, sizeof(message->file), &file_offset, SD_DHCP_OPTION_END, 0, NULL);
if (r < 0)
return r;
*offset = size + sizeof(message->file);
} else
return r;
}
}
if (use_sname) {
size_t sname_offset = *offset - size - use_file*sizeof(message->file);
if (sname_offset < sizeof(message->sname)) {
/* still space in the 'sname' array */
r = option_append(message->sname, sizeof(message->sname), &sname_offset, code, optlen, optval);
if (r >= 0) {
*offset = size + use_file*sizeof(message->file) + sname_offset;
return 0;
} else
/* no space, or other error, give up */
return r;
}
}
return -ENOBUFS;
}
static int parse_options(const uint8_t options[], size_t buflen, uint8_t *overload,
uint8_t *message_type, char **error_message, dhcp_option_callback_t cb,
void *userdata) {
uint8_t code, len;
const uint8_t *option;
size_t offset = 0;
int r;
while (offset < buflen) {
code = options[offset++];
switch (code) {
case SD_DHCP_OPTION_PAD:
continue;
case SD_DHCP_OPTION_END:
return 0;
}
if (buflen < offset + 1)
return -ENOBUFS;
len = options[offset++];
if (buflen < offset + len)
return -EINVAL;
option = &options[offset];
switch (code) {
case SD_DHCP_OPTION_MESSAGE_TYPE:
if (len != 1)
return -EINVAL;
if (message_type)
*message_type = *option;
break;
case SD_DHCP_OPTION_ERROR_MESSAGE:
if (len == 0)
return -EINVAL;
if (error_message) {
_cleanup_free_ char *string = NULL;
r = make_cstring((const char*) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
if (r < 0)
return r;
if (!ascii_is_valid(string))
return -EINVAL;
free_and_replace(*error_message, string);
}
break;
case SD_DHCP_OPTION_OVERLOAD:
if (len != 1)
return -EINVAL;
if (overload)
*overload = *option;
break;
default:
if (cb)
cb(code, len, option, userdata);
}
offset += len;
}
if (offset < buflen)
return -EINVAL;
return 0;
}
int dhcp_option_parse(DHCPMessage *message, size_t len, dhcp_option_callback_t cb, void *userdata, char **ret_error_message) {
_cleanup_free_ char *error_message = NULL;
uint8_t overload = 0;
uint8_t message_type = 0;
int r;
if (!message)
return -EINVAL;
if (len < sizeof(DHCPMessage))
return -EINVAL;
len -= sizeof(DHCPMessage);
r = parse_options(message->options, len, &overload, &message_type, &error_message, cb, userdata);
if (r < 0)
return r;
if (overload & DHCP_OVERLOAD_FILE) {
r = parse_options(message->file, sizeof(message->file), NULL, &message_type, &error_message, cb, userdata);
if (r < 0)
return r;
}
if (overload & DHCP_OVERLOAD_SNAME) {
r = parse_options(message->sname, sizeof(message->sname), NULL, &message_type, &error_message, cb, userdata);
if (r < 0)
return r;
}
if (message_type == 0)
return -ENOMSG;
if (ret_error_message && IN_SET(message_type, DHCP_NAK, DHCP_DECLINE))
*ret_error_message = TAKE_PTR(error_message);
return message_type;
}
int dhcp_option_parse_string(const uint8_t *option, size_t len, char **ret) {
_cleanup_free_ char *string = NULL;
int r;
assert(option);
assert(ret);
if (len <= 0) {
*ret = NULL;
return 0;
}
/* One trailing NUL byte is OK, we don't mind. See:
* https://github.com/systemd/systemd/issues/1337 */
r = make_cstring((const char *) option, len, MAKE_CSTRING_ALLOW_TRAILING_NUL, &string);
if (r < 0)
return r;
if (!string_is_safe(string, STRING_ALLOW_EMPTY|STRING_ALLOW_GLOBS))
return -EINVAL;
*ret = TAKE_PTR(string);
return 0;
}
int dhcp_option_parse_hostname(const uint8_t *option, size_t len, char **ret) {
_cleanup_free_ char *hostname = NULL;
int r;
assert(option);
assert(ret);
r = dhcp_option_parse_string(option, len, &hostname);
if (r < 0)
return r;
if (!hostname) {
*ret = NULL;
return 0;
}
if (!hostname_is_valid(hostname, 0))
return -EINVAL;
r = dns_name_is_valid(hostname);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
*ret = TAKE_PTR(hostname);
return 0;
}
-33
View File
@@ -1,33 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "dhcp-protocol.h"
#include "sd-forward.h"
typedef struct DHCPServerData {
struct in_addr *addr;
size_t size;
} DHCPServerData;
int dhcp_option_append(
DHCPMessage *message,
size_t size,
size_t *offset,
uint8_t overload,
uint8_t code,
size_t optlen,
const void *optval);
int dhcp_option_find_option(uint8_t *options, size_t length, uint8_t wanted_code, size_t *ret_offset);
int dhcp_option_remove_option(uint8_t *options, size_t length, uint8_t option_code);
typedef int (*dhcp_option_callback_t)(uint8_t code, uint8_t len, const void *option, void *userdata);
int dhcp_option_parse(
DHCPMessage *message,
size_t len,
dhcp_option_callback_t cb,
void *userdata,
char **ret_error_message);
int dhcp_option_parse_string(const uint8_t *option, size_t len, char **ret);
int dhcp_option_parse_hostname(const uint8_t *option, size_t len, char **ret);
-117
View File
@@ -1,117 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include <net/if_arp.h>
#include "dhcp-option.h"
#include "dhcp-packet.h"
#include "iovec-util.h"
#include "iovec-wrapper.h"
#include "ip-util.h"
#include "memory-util.h"
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
int bootp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr) {
assert(message);
assert(IN_SET(op, BOOTREQUEST, BOOTREPLY));
assert(chaddr || hlen == 0);
message->op = op;
message->htype = arp_type;
/* RFC2131 section 4.1.1:
The client MUST include its hardware address in the ’chaddr’ field, if
necessary for delivery of DHCP reply messages.
RFC 4390 section 2.1:
A DHCP client, when working over an IPoIB interface, MUST follow the
following rules:
"htype" (hardware address type) MUST be 32 [ARPPARAM].
"hlen" (hardware address length) MUST be 0.
"chaddr" (client hardware address) field MUST be zeroed.
*/
message->hlen = arp_type == ARPHRD_INFINIBAND ? 0 : hlen;
memcpy_safe(message->chaddr, chaddr, message->hlen);
message->xid = htobe32(xid);
message->magic = htobe32(DHCP_MAGIC_COOKIE);
return 0;
}
int dhcp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr,
uint8_t type,
size_t optlen,
size_t *ret_optoffset) {
size_t offset = 0;
int r;
assert(message);
assert(chaddr || hlen == 0);
assert(ret_optoffset);
r = bootp_message_init(message, op, xid, arp_type, hlen, chaddr);
if (r < 0)
return r;
r = dhcp_option_append(message, optlen, &offset, 0,
SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
if (r < 0)
return r;
*ret_optoffset = offset;
return 0;
}
int dhcp_packet_append_ip_headers(
DHCPPacket *packet,
be32_t source_addr,
uint16_t source_port,
be32_t destination_addr,
uint16_t destination_port,
uint16_t len,
int ip_service_type) {
struct iphdr ip;
struct udphdr udp;
int r;
assert(packet);
assert(len > offsetof(DHCPPacket, dhcp));
r = udp_packet_build(
source_addr,
source_port,
destination_addr,
destination_port,
ip_service_type,
&(struct iovec_wrapper) {
.iovec = &IOVEC_MAKE(&packet->dhcp, len - offsetof(DHCPPacket, dhcp)),
.count = 1,
},
&ip,
&udp);
if (r < 0)
return r;
packet->ip = ip;
packet->udp = udp;
return 0;
}
-33
View File
@@ -1,33 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "dhcp-protocol.h"
#include "sd-forward.h"
int bootp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr);
int dhcp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr,
uint8_t type,
size_t optlen,
size_t *ret_optoffset);
int dhcp_packet_append_ip_headers(
DHCPPacket *packet,
be32_t source_addr,
uint16_t source,
be32_t destination_addr,
uint16_t destination,
uint16_t len,
int ip_service_type);
+16 -53
View File
@@ -5,72 +5,35 @@
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "sd-dhcp-protocol.h" /* IWYU pragma: export */
#include "sd-forward.h"
#include "sparse-endian.h"
#include "dhcp-forward.h"
#include "time-util.h"
#include "tlv-util.h"
/* RFC 8925 - IPv6-Only Preferred Option for DHCPv4 3.4.
* MIN_V6ONLY_WAIT: The lower boundary for V6ONLY_WAIT. Value: 300 seconds */
#define MIN_V6ONLY_WAIT_USEC (300U * USEC_PER_SEC)
#define DHCP_MESSAGE_HEADER_DEFINITION \
uint8_t op; \
uint8_t htype; \
uint8_t hlen; \
uint8_t hops; \
be32_t xid; \
be16_t secs; \
be16_t flags; \
be32_t ciaddr; \
be32_t yiaddr; \
be32_t siaddr; \
be32_t giaddr; \
uint8_t chaddr[16]; \
uint8_t sname[64]; \
uint8_t file[128]; \
be32_t magic;
struct DHCPMessageHeader {
DHCP_MESSAGE_HEADER_DEFINITION;
uint8_t op;
uint8_t htype;
uint8_t hlen;
uint8_t hops;
be32_t xid;
be16_t secs;
be16_t flags;
be32_t ciaddr;
be32_t yiaddr;
be32_t siaddr;
be32_t giaddr;
uint8_t chaddr[16];
uint8_t sname[64];
uint8_t file[128];
be32_t magic;
} _packed_;
typedef struct DHCPMessageHeader DHCPMessageHeader;
struct DHCPMessage {
DHCP_MESSAGE_HEADER_DEFINITION;
uint8_t options[];
} _packed_;
typedef struct DHCPMessage DHCPMessage;
assert_cc(sizeof(DHCPMessageHeader) == offsetof(DHCPMessage, options));
struct sd_dhcp_message {
unsigned n_ref;
DHCPMessageHeader header;
TLV options;
};
struct DHCPPacket {
struct iphdr ip;
struct udphdr udp;
DHCPMessage dhcp;
} _packed_;
typedef struct DHCPPacket DHCPPacket;
#define DHCP_IP_SIZE (int32_t)(sizeof(struct iphdr))
#define DHCP_IP_UDP_SIZE (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
#define DHCP_HEADER_SIZE (int32_t)(sizeof(DHCPMessage))
#define DHCP_MIN_MESSAGE_SIZE 576 /* the minimum internet hosts must be able to receive, see RFC 2132 Section 9.10 */
#define DHCP_MIN_OPTIONS_SIZE (DHCP_MIN_MESSAGE_SIZE - DHCP_HEADER_SIZE)
#define DHCP_MIN_PACKET_SIZE (DHCP_MIN_MESSAGE_SIZE + DHCP_IP_UDP_SIZE)
#define DHCP_MAGIC_COOKIE (uint32_t)(0x63825363)
/* The size of BOOTP message. The BOOTP message does not have the magic field, but has the 64-byte
@@ -5,7 +5,6 @@
#include "sd-event.h"
#include "dhcp-message.h"
#include "dhcp-protocol.h"
#include "dhcp-relay-internal.h"
#include "errno-util.h"
#include "ether-addr-util.h"
+4 -4
View File
@@ -1,13 +1,13 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include <netinet/in.h>
#include <sys/uio.h>
#include "sd-dhcp-relay.h"
#include "dhcp-message.h"
#include "ether-addr-util.h"
#include "dhcp-forward.h"
#include "network-common.h"
#include "sd-forward.h"
#include "tlv-util.h"
#define DHCP_RELAY_IFINDEX_UNBOUND (-100)
@@ -5,7 +5,6 @@
#include "sd-event.h"
#include "dhcp-message.h"
#include "dhcp-protocol.h"
#include "dhcp-relay-internal.h"
#include "hashmap.h"
#include "in-addr-util.h"
+1 -1
View File
@@ -2,7 +2,7 @@
#include "sd-dhcp-lease.h"
#include "dhcp-route.h" /* IWYU pragma: keep */
#include "dhcp-route.h"
int sd_dhcp_route_get_destination(sd_dhcp_route *route, struct in_addr *ret) {
assert_return(route, -EINVAL);
+2 -2
View File
@@ -1,9 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-forward.h"
#include <netinet/in.h>
#include "in-addr-util.h" /* IWYU pragma: keep */
#include "dhcp-forward.h" /* IWYU pragma: export */
struct sd_dhcp_route {
struct in_addr dst_addr;
@@ -5,13 +5,13 @@
Copyright © 2013 Intel Corporation. All rights reserved.
***/
#include <netinet/in.h>
#include "sd-dhcp-server.h"
#include "dhcp-option.h"
#include "dhcp-forward.h"
#include "dhcp-message.h"
#include "network-common.h"
#include "sd-forward.h"
#include "sparse-endian.h"
#include "tlv-util.h"
typedef struct sd_dhcp_server {
unsigned n_ref;
@@ -4,10 +4,8 @@
#include "sd-dhcp-server-lease.h"
#include "dhcp-client-id-internal.h"
#include "dhcp-server-internal.h"
#include "dhcp-server-request.h"
#include "dhcp-forward.h"
#include "ether-addr-util.h"
#include "sd-forward.h"
typedef struct sd_dhcp_server_lease {
unsigned n_ref;
+1 -5
View File
@@ -1,13 +1,9 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-dhcp-server-lease.h"
#include "dhcp-client-id-internal.h"
#include "dhcp-message.h"
#include "dhcp-forward.h"
#include "ether-addr-util.h"
#include "sd-forward.h"
#include "sparse-endian.h"
#include "time-util.h"
typedef struct DHCPRequest {
@@ -4,6 +4,7 @@
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "dhcp-server-request.h"
#include "dhcp-server-send.h"
#include "errno-util.h"
#include "fd-util.h"
+1 -3
View File
@@ -1,9 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-forward.h"
#include "dhcp-server-request.h"
#include "dhcp-forward.h"
int dhcp_server_send_reply(
sd_dhcp_server *server,
-6
View File
@@ -5,9 +5,6 @@ libsystemd_network_sources = files(
'dhcp-client-send.c',
'dhcp-message.c',
'dhcp-message-dump.c',
'dhcp-network.c',
'dhcp-option.c',
'dhcp-packet.c',
'dhcp-protocol.c',
'dhcp-relay-downstream.c',
'dhcp-relay-interface.c',
@@ -95,9 +92,6 @@ executables += [
network_test_template + {
'sources' : files('test-dhcp-message.c'),
},
network_test_template + {
'sources' : files('test-dhcp-option.c'),
},
network_test_template + {
'sources' : files('test-dhcp-relay.c'),
},
+1
View File
@@ -26,6 +26,7 @@
#include "socket-util.h"
#include "string-util.h"
#include "time-util.h"
#include "tlv-util.h"
#include "web-util.h"
#define MAX_MAC_ADDR_LEN CONST_MAX(INFINIBAND_ALEN, ETH_ALEN)
+1 -2
View File
@@ -10,8 +10,7 @@
#include "alloc-util.h"
#include "dhcp-client-internal.h"
#include "dhcp-lease-internal.h"
#include "dhcp-option.h"
#include "dhcp-route.h" /* IWYU pragma: keep */
#include "dhcp-route.h"
#include "dns-resolver-internal.h"
#include "in-addr-util.h"
#include "ip-util.h"
+1
View File
@@ -9,6 +9,7 @@
#include "in-addr-util.h"
#include "iovec-util.h"
#include "prioq.h"
#include "tlv-util.h"
static sd_dhcp_relay* dhcp_relay_free(sd_dhcp_relay *relay) {
if (!relay)
@@ -6,7 +6,9 @@
#include "sd-event.h"
#include "alloc-util.h"
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "dhcp-server-request.h"
#include "dns-domain.h"
#include "errno-util.h"
#include "fd-util.h"
-1
View File
@@ -9,7 +9,6 @@
#include "sd-event.h"
#include "alloc-util.h"
#include "dhcp-option.h"
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "dhcp-server-request.h"
+1 -1
View File
@@ -14,7 +14,6 @@
#include "dhcp-client-internal.h"
#include "dhcp-message.h"
#include "dhcp-protocol.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "hashmap.h"
@@ -24,6 +23,7 @@
#include "ip-util.h"
#include "log.h"
#include "set.h"
#include "socket-util.h"
#include "strv.h"
#include "tests.h"
@@ -8,7 +8,6 @@
#include "dhcp-client-id-internal.h"
#include "dhcp-message.h"
#include "dhcp-message-dump.h"
#include "dhcp-protocol.h"
#include "dhcp-route.h"
#include "dns-packet.h"
#include "dns-resolver-internal.h"
-384
View File
@@ -1,384 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <net/if_arp.h>
#include <stdio.h>
#include <string.h>
#include "alloc-util.h"
#include "dhcp-option.h"
#include "dhcp-packet.h"
#include "memory-util.h"
#include "tests.h"
struct option_desc {
uint8_t sname[64];
int snamelen;
uint8_t file[128];
int filelen;
uint8_t options[128];
int len;
bool success;
int filepos;
int snamepos;
int pos;
};
static bool verbose = false;
static struct option_desc option_tests[] = {
{ {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69 }, 7, false, },
{ {}, 0, {}, 0, { 42, 5, 65, 66, 67, 68, 69, 0, 0,
SD_DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 12, true, },
{ {}, 0, {}, 0, { 8, 255, 70, 71, 72 }, 5, false, },
{ {}, 0, {}, 0, { 0x35, 0x01, 0x05, 0x36, 0x04, 0x01, 0x00, 0xa8,
0xc0, 0x33, 0x04, 0x00, 0x01, 0x51, 0x80, 0x01,
0x04, 0xff, 0xff, 0xff, 0x00, 0x03, 0x04, 0xc0,
0xa8, 0x00, 0x01, 0x06, 0x04, 0xc0, 0xa8, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
40, true, },
{ {}, 0, {}, 0, { SD_DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_OFFER,
42, 3, 0, 0, 0 }, 8, true, },
{ {}, 0, {}, 0, { 42, 2, 1, 2, 44 }, 5, false, },
{ {}, 0,
{ 222, 3, 1, 2, 3, SD_DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_NAK }, 8,
{ SD_DHCP_OPTION_OVERLOAD, 1, DHCP_OVERLOAD_FILE }, 3, true, },
{ { 1, 4, 1, 2, 3, 4, SD_DHCP_OPTION_MESSAGE_TYPE, 1, DHCP_ACK }, 9,
{ 222, 3, 1, 2, 3 }, 5,
{ SD_DHCP_OPTION_OVERLOAD, 1,
DHCP_OVERLOAD_FILE|DHCP_OVERLOAD_SNAME }, 3, true, },
};
static const char *dhcp_type(int type) {
switch (type) {
case DHCP_DISCOVER:
return "DHCPDISCOVER";
case DHCP_OFFER:
return "DHCPOFFER";
case DHCP_REQUEST:
return "DHCPREQUEST";
case DHCP_DECLINE:
return "DHCPDECLINE";
case DHCP_ACK:
return "DHCPACK";
case DHCP_NAK:
return "DHCPNAK";
case DHCP_RELEASE:
return "DHCPRELEASE";
default:
return "unknown";
}
}
static void test_invalid_buffer_length(void) {
DHCPMessage message;
assert_se(dhcp_option_parse(&message, 0, NULL, NULL, NULL) == -EINVAL);
assert_se(dhcp_option_parse(&message, sizeof(DHCPMessage) - 1, NULL, NULL, NULL) == -EINVAL);
}
static void test_message_init(void) {
_cleanup_free_ DHCPMessage *message = NULL;
size_t optlen = 4, optoffset;
size_t len = sizeof(DHCPMessage) + optlen;
uint8_t *magic;
message = malloc0(len);
assert_se(dhcp_message_init(message, BOOTREQUEST, 0x12345678,
ARPHRD_ETHER, ETH_ALEN, (uint8_t[16]){}, DHCP_DISCOVER,
optlen, &optoffset) >= 0);
assert_se(message->xid == htobe32(0x12345678));
assert_se(message->op == BOOTREQUEST);
magic = (uint8_t*)&message->magic;
assert_se(magic[0] == 99);
assert_se(magic[1] == 130);
assert_se(magic[2] == 83);
assert_se(magic[3] == 99);
assert_se(dhcp_option_parse(message, len, NULL, NULL, NULL) >= 0);
}
static DHCPMessage *create_message(uint8_t *options, uint16_t optlen,
uint8_t *file, uint8_t filelen,
uint8_t *sname, uint8_t snamelen) {
DHCPMessage *message;
size_t len = sizeof(DHCPMessage) + optlen;
message = malloc0(len);
assert_se(message);
memcpy_safe(&message->options, options, optlen);
memcpy_safe(&message->file, file, filelen);
memcpy_safe(&message->sname, sname, snamelen);
return message;
}
static void test_ignore_opts(uint8_t *descoption, int *descpos, int *desclen) {
assert(descoption);
assert(descpos);
assert(desclen);
assert_se(*descpos >= 0);
while (*descpos < *desclen) {
switch (descoption[*descpos]) {
case SD_DHCP_OPTION_PAD:
*descpos += 1;
break;
case SD_DHCP_OPTION_MESSAGE_TYPE:
case SD_DHCP_OPTION_OVERLOAD:
*descpos += 3;
break;
default:
return;
}
}
}
static int test_options_cb(uint8_t code, uint8_t len, const void *option, void *userdata) {
struct option_desc *desc = userdata;
uint8_t *descoption = NULL;
int *desclen = NULL, *descpos = NULL;
uint8_t optcode = 0;
uint8_t optlen = 0;
assert_se((!desc && !code && !len) || desc);
if (!desc)
return -EINVAL;
assert_se(code != SD_DHCP_OPTION_PAD);
assert_se(code != SD_DHCP_OPTION_END);
assert_se(code != SD_DHCP_OPTION_MESSAGE_TYPE);
assert_se(code != SD_DHCP_OPTION_OVERLOAD);
while (desc->pos >= 0 || desc->filepos >= 0 || desc->snamepos >= 0) {
if (desc->pos >= 0) {
descoption = &desc->options[0];
desclen = &desc->len;
descpos = &desc->pos;
} else if (desc->filepos >= 0) {
descoption = &desc->file[0];
desclen = &desc->filelen;
descpos = &desc->filepos;
} else if (desc->snamepos >= 0) {
descoption = &desc->sname[0];
desclen = &desc->snamelen;
descpos = &desc->snamepos;
}
assert_se(descoption && desclen && descpos);
if (*desclen)
test_ignore_opts(descoption, descpos, desclen);
if (*descpos < *desclen)
break;
if (*descpos == *desclen)
*descpos = -1;
}
assert_se(descpos);
assert_se(*descpos != -1);
optcode = descoption[*descpos];
optlen = descoption[*descpos + 1];
if (verbose)
printf("DHCP code %2d(%2d) len %2d(%2d) ", code, optcode,
len, optlen);
assert_se(code == optcode);
assert_se(len == optlen);
for (unsigned i = 0; i < len; i++) {
if (verbose)
printf("0x%02x(0x%02x) ",
((uint8_t*) option)[i],
descoption[*descpos + 2 + i]);
assert_se(((uint8_t*) option)[i] == descoption[*descpos + 2 + i]);
}
if (verbose)
printf("\n");
*descpos += optlen + 2;
test_ignore_opts(descoption, descpos, desclen);
if (desc->pos != -1 && desc->pos == desc->len)
desc->pos = -1;
if (desc->filepos != -1 && desc->filepos == desc->filelen)
desc->filepos = -1;
if (desc->snamepos != -1 && desc->snamepos == desc->snamelen)
desc->snamepos = -1;
return 0;
}
static void test_options(struct option_desc *desc) {
uint8_t *options = NULL;
uint8_t *file = NULL;
uint8_t *sname = NULL;
int optlen = 0;
int filelen = 0;
int snamelen = 0;
int buflen = 0;
_cleanup_free_ DHCPMessage *message = NULL;
int res;
if (desc) {
file = &desc->file[0];
filelen = desc->filelen;
if (!filelen)
desc->filepos = -1;
sname = &desc->sname[0];
snamelen = desc->snamelen;
if (!snamelen)
desc->snamepos = -1;
options = &desc->options[0];
optlen = desc->len;
desc->pos = 0;
}
message = create_message(options, optlen, file, filelen,
sname, snamelen);
buflen = sizeof(DHCPMessage) + optlen;
if (!desc) {
assert_se((res = dhcp_option_parse(message, buflen, test_options_cb, NULL, NULL)) == -ENOMSG);
} else if (desc->success) {
assert_se((res = dhcp_option_parse(message, buflen, test_options_cb, desc, NULL)) >= 0);
assert_se(desc->pos == -1 && desc->filepos == -1 && desc->snamepos == -1);
} else
assert_se((res = dhcp_option_parse(message, buflen, test_options_cb, desc, NULL)) < 0);
if (verbose)
printf("DHCP type %s\n", dhcp_type(res));
}
static void test_option_removal(struct option_desc *desc) {
_cleanup_free_ DHCPMessage *message = create_message(&desc->options[0], desc->len, NULL, 0, NULL, 0);
assert_se(dhcp_option_parse(message, sizeof(DHCPMessage) + desc->len, NULL, NULL, NULL) >= 0);
assert_se((desc->len = dhcp_option_remove_option(message->options, desc->len, SD_DHCP_OPTION_MESSAGE_TYPE)) >= 0);
assert_se(dhcp_option_parse(message, sizeof(DHCPMessage) + desc->len, NULL, NULL, NULL) < 0);
}
static uint8_t the_options[64] = {
'A', 'B', 'C', 'D',
160, 2, 0x11, 0x12,
0,
31, 8, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0,
55, 3, 0x51, 0x52, 0x53,
17, 7, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
255
};
static void test_option_set(void) {
_cleanup_free_ DHCPMessage *result = NULL;
size_t offset = 0, len, pos;
result = malloc0(sizeof(DHCPMessage) + 11);
assert_se(result);
result->options[0] = 'A';
result->options[1] = 'B';
result->options[2] = 'C';
result->options[3] = 'D';
assert_se(dhcp_option_append(result, 0, &offset, 0, SD_DHCP_OPTION_PAD,
0, NULL) == -ENOBUFS);
assert_se(offset == 0);
offset = 4;
assert_se(dhcp_option_append(result, 5, &offset, 0, SD_DHCP_OPTION_PAD,
0, NULL) == -ENOBUFS);
assert_se(offset == 4);
assert_se(dhcp_option_append(result, 6, &offset, 0, SD_DHCP_OPTION_PAD,
0, NULL) >= 0);
assert_se(offset == 5);
offset = pos = 4;
len = 11;
while (pos < len && the_options[pos] != SD_DHCP_OPTION_END) {
assert_se(dhcp_option_append(result, len, &offset, DHCP_OVERLOAD_SNAME,
the_options[pos],
the_options[pos + 1],
&the_options[pos + 2]) >= 0);
if (the_options[pos] == SD_DHCP_OPTION_PAD)
pos++;
else
pos += 2 + the_options[pos + 1];
if (pos < len)
assert_se(offset == pos);
}
for (unsigned i = 0; i < 9; i++) {
if (verbose)
printf("%2u: 0x%02x(0x%02x) (options)\n", i, result->options[i],
the_options[i]);
assert_se(result->options[i] == the_options[i]);
}
if (verbose)
printf("%2d: 0x%02x(0x%02x) (options)\n", 9, result->options[9],
(unsigned) SD_DHCP_OPTION_END);
assert_se(result->options[9] == SD_DHCP_OPTION_END);
if (verbose)
printf("%2d: 0x%02x(0x%02x) (options)\n", 10, result->options[10],
(unsigned) SD_DHCP_OPTION_PAD);
assert_se(result->options[10] == SD_DHCP_OPTION_PAD);
for (unsigned i = 0; i < pos - 8; i++) {
if (verbose)
printf("%2u: 0x%02x(0x%02x) (sname)\n", i, result->sname[i],
the_options[i + 9]);
assert_se(result->sname[i] == the_options[i + 9]);
}
if (verbose)
printf ("\n");
}
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_invalid_buffer_length();
test_message_init();
test_options(NULL);
FOREACH_ELEMENT(desc, option_tests)
test_options(desc);
test_option_set();
FOREACH_ELEMENT(desc, option_tests) {
if (!desc->success || desc->snamelen > 0 || desc->filelen > 0)
continue;
test_option_removal(desc);
}
return 0;
}
+2 -2
View File
@@ -5,8 +5,8 @@
#include "sd-event.h"
#include "dhcp-protocol.h"
#include "dhcp-relay-internal.h" /* IWYU pragma: keep */
#include "dhcp-message.h"
#include "dhcp-relay-internal.h"
#include "ether-addr-util.h"
#include "fd-util.h"
#include "hashmap.h"
+125 -129
View File
@@ -12,6 +12,7 @@
#include "dhcp-server-request.h"
#include "fd-util.h"
#include "iovec-util.h"
#include "iovec-wrapper.h"
#include "tests.h"
TEST(basic) {
@@ -61,50 +62,32 @@ TEST(basic) {
ASSERT_OK(sd_dhcp_server_start(server));
}
static int process_one(sd_dhcp_server *server, sd_dhcp_message *message) {
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {};
ASSERT_OK(dhcp_message_build(message, &iovw));
_cleanup_(iovec_done) struct iovec iov = {};
ASSERT_OK(iovw_concat(&iovw, &iov));
return dhcp_server_process_message(server, &iov, /* mh= */ NULL);
}
TEST(dhcp_server_process_message) {
struct {
DHCPMessageHeader header;
struct {
uint8_t code;
uint8_t length;
uint8_t type;
} _packed_ option_type;
struct {
uint8_t code;
uint8_t length;
be32_t address;
} _packed_ option_requested_ip;
struct {
uint8_t code;
uint8_t length;
be32_t address;
} _packed_ option_server_id;
struct {
uint8_t code;
uint8_t length;
uint8_t id[7];
} _packed_ option_client_id;
struct {
uint8_t code;
uint8_t length;
uint8_t hostname[6];
} _packed_ option_hostname;
uint8_t end;
} _packed_ test = {
.header.op = BOOTREQUEST,
.header.htype = ARPHRD_ETHER,
.header.hlen = ETHER_ADDR_LEN,
.header.xid = htobe32(0x12345678),
.header.chaddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
.header.magic = htobe32(DHCP_MAGIC_COOKIE),
.option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE,
.option_type.length = 1,
.option_type.type = DHCP_DISCOVER,
.option_hostname.code = SD_DHCP_OPTION_HOST_NAME,
.option_hostname.length = 6,
.option_hostname.hostname = { 'T', 'E', 'S', 'T', 'H', 'N' },
.end = SD_DHCP_OPTION_END,
static const struct hw_addr_data hw_addr = {
.length = ETH_ALEN,
.ether = {{ 'A', 'B', 'C', 'D', 'E', 'F' }},
};
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL;
ASSERT_OK(dhcp_message_new(&m));
ASSERT_OK(dhcp_message_init_header(
m,
BOOTREQUEST,
0x12345678,
ARPHRD_ETHER,
&hw_addr));
ASSERT_OK(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, "TESTHN"));
struct in_addr address_lo = {
.s_addr = htobe32(INADDR_LOOPBACK),
};
@@ -132,75 +115,74 @@ TEST(dhcp_server_process_message) {
server->socket_fd = TAKE_FD(socket_fd[0]);
ASSERT_OK(sd_dhcp_server_start(server));
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
ASSERT_OK_EQ(process_one(server, m), DHCP_OFFER);
test.end = 0;
/* TODO, shouldn't this fail? */
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
test.end = SD_DHCP_OPTION_END;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
/* Missing Message Type option */
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_ERROR(process_one(server, m), ENODATA);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
test.option_type.code = 0;
test.option_type.length = 0;
test.option_type.type = 0;
ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), ENODATA);
test.option_type.code = SD_DHCP_OPTION_MESSAGE_TYPE;
test.option_type.length = 1;
test.option_type.type = DHCP_DISCOVER;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
/* Invalid op */
m->header.op = 0;
ASSERT_ERROR(process_one(server, m), EBADMSG);
m->header.op = BOOTREQUEST;
test.header.op = 0;
ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
test.header.op = BOOTREQUEST;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
/* Invalid htype */
m->header.htype = 0;
ASSERT_ERROR(process_one(server, m), EBADMSG);
m->header.htype = ARPHRD_ETHER;
/* Neither client ID nor hardware type is set. There is no way to manage the bound lease for the request. */
test.header.htype = 0;
ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
test.header.htype = ARPHRD_ETHER;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
/* Invalid hlen */
m->header.hlen = 0;
ASSERT_ERROR(process_one(server, m), EBADMSG);
m->header.hlen = ETHER_ADDR_LEN;
test.header.hlen = 0;
ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), EBADMSG);
test.header.hlen = ETHER_ADDR_LEN;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
/* DHCPREQUEST (init-reboot) without Requested IP Address option */
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
ASSERT_ERROR(process_one(server, m), ENODATA);
/* DHCPREQUEST (init-boot) without requested IP */
test.option_type.type = DHCP_REQUEST;
ASSERT_ERROR(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), ENODATA);
/* DHCPREQUEST (init-reboot) with an invalid Requested IP Address option */
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(0x12345678)));
ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
test.option_requested_ip.code = SD_DHCP_OPTION_REQUESTED_IP_ADDRESS;
test.option_requested_ip.length = 4;
test.option_requested_ip.address = htobe32(0x12345678);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
test.option_server_id.code = SD_DHCP_OPTION_SERVER_IDENTIFIER;
test.option_server_id.length = 4;
test.option_server_id.address = htobe32(INADDR_LOOPBACK);
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
/* DHCPREQUEST (selecting) with Requested IP address */
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(INADDR_LOOPBACK)));
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 3)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
test.option_server_id.address = htobe32(0x12345678);
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
ASSERT_OK_ZERO(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL));
test.option_server_id.address = htobe32(INADDR_LOOPBACK);
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 4);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 3);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
/* DHCPREQUEST (selecting) with unmatching server address (silently ignored). */
dhcp_message_remove_option(m, SD_DHCP_OPTION_SERVER_IDENTIFIER);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(0x12345678)));
ASSERT_OK_ZERO(process_one(server, m));
dhcp_message_remove_option(m, SD_DHCP_OPTION_SERVER_IDENTIFIER);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, htobe32(INADDR_LOOPBACK)));
test.option_client_id.code = SD_DHCP_OPTION_CLIENT_IDENTIFIER;
test.option_client_id.length = 7;
test.option_client_id.id[0] = 0x01;
test.option_client_id.id[1] = 'A';
test.option_client_id.id[2] = 'B';
test.option_client_id.id[3] = 'C';
test.option_client_id.id[4] = 'D';
test.option_client_id.id[5] = 'E';
test.option_client_id.id[6] = 'F';
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
/* DHCPREQUEST (selecting) with another address */
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 4)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
/* Request the previous address again. */
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 3)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* With client ID */
struct sd_dhcp_client_id client_id = {
.size = 7,
.id.type = 1,
.id.eth.haddr = { 'A', 'B', 'C', 'D', 'E', 'F' },
};
ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* Request a different address with client ID */
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 30)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* add the static lease for the client ID */
ASSERT_OK(sd_dhcp_server_stop(server));
@@ -213,26 +195,35 @@ TEST(dhcp_server_process_message) {
ASSERT_OK(sd_dhcp_server_start(server));
/* discover */
test.option_type.type = DHCP_DISCOVER;
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_OFFER);
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER));
ASSERT_OK_EQ(process_one(server, m), DHCP_OFFER);
/* request neither bound nor static address */
test.option_type.type = DHCP_REQUEST;
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 29);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 29)));
ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
/* request the currently assigned address */
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 30);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 30)));
ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
/* request the new static address */
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 31);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 31)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* release the bound static lease */
test.header.ciaddr = htobe32(INADDR_LOOPBACK + 31);
test.option_type.type = DHCP_RELEASE;
ASSERT_OK_ZERO(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL));
m->header.ciaddr = htobe32(INADDR_LOOPBACK + 31);
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_RELEASE));
ASSERT_OK_ZERO(process_one(server, m));
m->header.ciaddr = 0;
dhcp_message_remove_option(m, SD_DHCP_OPTION_MESSAGE_TYPE);
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST));
/* drop the static lease for the client ID */
ASSERT_OK(sd_dhcp_server_stop(server));
@@ -245,30 +236,35 @@ TEST(dhcp_server_process_message) {
ASSERT_OK(sd_dhcp_server_start(server));
/* request a new non-static address */
test.header.ciaddr = 0;
test.option_type.type = DHCP_REQUEST;
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 29);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 29)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* request address reserved for static lease (unmatching client ID) */
test.option_client_id.id[6] = 'H';
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
client_id.id.eth.haddr[5] = 'H';
dhcp_message_remove_option(m, SD_DHCP_OPTION_CLIENT_IDENTIFIER);
ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
/* request unmatching address */
test.option_client_id.id[6] = 'G';
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 41);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_NAK);
client_id.id.eth.haddr[5] = 'G';
dhcp_message_remove_option(m, SD_DHCP_OPTION_CLIENT_IDENTIFIER);
ASSERT_OK(dhcp_message_append_option_client_id(m, &client_id));
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 41)));
ASSERT_OK_EQ(process_one(server, m), DHCP_NAK);
/* request matching address */
test.option_client_id.id[6] = 'G';
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
/* try again */
test.option_client_id.id[6] = 'G';
test.option_requested_ip.address = htobe32(INADDR_LOOPBACK + 42);
ASSERT_OK_EQ(dhcp_server_process_message(server, &IOVEC_MAKE(&test, sizeof(test)), NULL), DHCP_ACK);
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS);
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, htobe32(INADDR_LOOPBACK + 42)));
ASSERT_OK_EQ(process_one(server, m), DHCP_ACK);
}
TEST(sd_dhcp_server_set_static_lease) {
-6
View File
@@ -76,12 +76,6 @@ typedef enum sd_lldp_multicast_mode_t sd_lldp_multicast_mode_t;
typedef enum sd_ndisc_event_t sd_ndisc_event_t;
typedef struct sd_ipv4ll sd_ipv4ll;
typedef struct sd_dhcp_client sd_dhcp_client;
typedef struct sd_dhcp_lease sd_dhcp_lease;
typedef struct sd_dhcp_route sd_dhcp_route;
typedef struct sd_dhcp_relay sd_dhcp_relay;
typedef struct sd_dhcp_relay_interface sd_dhcp_relay_interface;
typedef struct sd_dhcp_server sd_dhcp_server;
typedef struct sd_dns_resolver sd_dns_resolver;
typedef struct sd_ndisc sd_ndisc;
typedef struct sd_radv sd_radv;
+1
View File
@@ -4,6 +4,7 @@
#include "alloc-util.h"
#include "bus-object.h"
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "hashmap.h"
#include "networkd-dhcp-server-bus.h"
+1
View File
@@ -4,6 +4,7 @@
/* IWYU pragma: always_keep */
#include "conf-parser-forward.h" /* IWYU pragma: export */
#include "dhcp-forward.h" /* IWYU pragma: export */
#include "shared-forward.h" /* IWYU pragma: export */
typedef enum NetDevLocalAddressType NetDevLocalAddressType;
+1
View File
@@ -7,6 +7,7 @@
#include "sd-dhcp6-client.h"
#include "dhcp-lease-internal.h" /* IWYU pragma: keep */
#include "dhcp-server-internal.h"
#include "dhcp-server-lease-internal.h"
#include "dhcp6-lease-internal.h"
#include "extract-word.h"