dhcp: drop unused functions and macros

This removes the legacy functions for manipulating DHCP message.

This also moves the definition of struct sd_dhcp_message into
dhcp-message.h.
This commit is contained in:
Yu Watanabe
2026-05-22 23:06:53 +09:00
parent f1c47ab00b
commit db8f504afb
14 changed files with 29 additions and 1105 deletions
@@ -10,7 +10,6 @@
#include "dhcp-client-id-internal.h"
#include "dhcp-message.h"
#include "dhcp-option.h"
struct sd_dhcp_lease {
unsigned n_ref;
+13 -1
View File
@@ -4,9 +4,21 @@
#include "sd-dhcp-client-id.h"
#include "sd-forward.h"
#include "sparse-endian.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);
+15 -51
View File
@@ -5,72 +5,36 @@
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 "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
@@ -7,7 +7,7 @@
#include "sd-dhcp-server.h"
#include "dhcp-option.h"
#include "dhcp-message.h"
#include "network-common.h"
#include "sd-forward.h"
#include "sparse-endian.h"
-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
@@ -10,7 +10,6 @@
#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 "dns-resolver-internal.h"
#include "in-addr-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"
-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;
}