1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-26 08:55:40 +03:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Colin Foster
779bd4111a test_dhcp_client: fix missed function signature changes 2024-10-24 15:02:03 -05:00
Colin Foster
b60caa0018 network/dhcpv4: add ability to use BOOTP
BOOTP can be used to sign a static IP to clients. Instead of using the
four message exchange, and Option 53 (DHCP Message Type) there is only a
two message exchange.

Add the following network option to enable BOOTP:

[DHCPv4]
Bootp=yes

This will allow a two message request / reply sequence that doesn't
require DHCP message types.
2024-10-24 14:47:40 -05:00
10 changed files with 82 additions and 29 deletions

View File

@ -14,7 +14,7 @@
#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
int dhcp_message_init(
int bootp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
@ -25,9 +25,6 @@ int dhcp_message_init(
size_t optlen,
size_t *optoffset) {
size_t offset = 0;
int r;
assert(IN_SET(op, BOOTREQUEST, BOOTREPLY));
assert(chaddr || hlen == 0);
@ -51,6 +48,28 @@ int dhcp_message_init(
message->xid = htobe32(xid);
message->magic = htobe32(DHCP_MAGIC_COOKIE);
return 0;
}
int dhcp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint8_t type,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr,
size_t optlen,
size_t *optoffset) {
size_t offset = 0;
int r;
r = bootp_message_init(message, op, xid, type, arp_type, hlen,
chaddr, optlen, &offset);
if (r < 0)
return r;
r = dhcp_option_append(message, optlen, &offset, 0,
SD_DHCP_OPTION_MESSAGE_TYPE, 1, &type);
if (r < 0)

View File

@ -6,6 +6,17 @@
#include "dhcp-protocol.h"
int bootp_message_init(
DHCPMessage *message,
uint8_t op,
uint32_t xid,
uint8_t type,
uint16_t arp_type,
uint8_t hlen,
const uint8_t *chaddr,
size_t optlen,
size_t *optoffset);
int dhcp_message_init(
DHCPMessage *message,
uint8_t op,

View File

@ -65,7 +65,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
fuzz_setup_logging();
r = sd_dhcp_client_new(&client, false);
r = sd_dhcp_client_new(&client, false, false);
assert_se(r >= 0);
assert_se(client);

View File

@ -105,6 +105,7 @@ struct sd_dhcp_client {
int socket_priority;
bool socket_priority_set;
bool ipv6_acquired;
bool bootp;
};
static const uint8_t default_req_opts[] = {
@ -793,9 +794,16 @@ static int client_message_init(
if (!packet)
return -ENOMEM;
r = dhcp_message_init(&packet->dhcp, BOOTREQUEST, client->xid, type,
client->arp_type, client->hw_addr.length, client->hw_addr.bytes,
optlen, &optoffset);
if (client->bootp) {
r = bootp_message_init(&packet->dhcp, BOOTREQUEST, client->xid, type,
client->arp_type, client->hw_addr.length, client->hw_addr.bytes,
optlen, &optoffset);
} else {
r = dhcp_message_init(&packet->dhcp, BOOTREQUEST, client->xid, type,
client->arp_type, client->hw_addr.length, client->hw_addr.bytes,
optlen, &optoffset);
}
if (r < 0)
return r;
@ -825,14 +833,16 @@ static int client_message_init(
if (client->request_broadcast || client->arp_type != ARPHRD_ETHER)
packet->dhcp.flags = htobe16(0x8000);
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
SD_DHCP_OPTION_CLIENT_IDENTIFIER,
client->client_id.size,
client->client_id.raw);
if (r < 0)
return r;
if (!client->bootp) {
/* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
SD_DHCP_OPTION_CLIENT_IDENTIFIER,
client->client_id.size,
client->client_id.raw);
if (r < 0)
return r;
}
/* RFC2131 section 3.5:
in its initial DHCPDISCOVER or DHCPREQUEST message, a
@ -1509,13 +1519,15 @@ static int client_parse_message(
}
r = dhcp_option_parse(message, len, dhcp_lease_parse_options, lease, &error_message);
if (r < 0)
if (r == -ENOMSG && client->bootp)
log_dhcp_client(client, "No DHCP options found in bootp message, continuing.");
else if (r < 0)
return log_dhcp_client_errno(client, r, "Failed to parse DHCP options, ignoring: %m");
switch (client->state) {
case DHCP_STATE_SELECTING:
if (r == DHCP_ACK) {
if (!client->rapid_commit)
if (!client->rapid_commit && !client->bootp)
return log_dhcp_client_errno(client, SYNTHETIC_ERRNO(ENOMSG),
"received unexpected ACK, ignoring.");
if (!lease->rapid_commit)
@ -1532,8 +1544,11 @@ static int client_parse_message(
if (lease->lifetime == 0 && client->fallback_lease_lifetime > 0)
lease->lifetime = client->fallback_lease_lifetime;
} else
return log_dhcp_client_errno(client, SYNTHETIC_ERRNO(ENOMSG),
"received unexpected message, ignoring.");
if (client->bootp)
log_dhcp_client(client, "Ignoring return value %d for bootp message", r);
else
return log_dhcp_client_errno(client, SYNTHETIC_ERRNO(ENOMSG),
"received unexpected message, ignoring.");
break;
@ -1561,6 +1576,9 @@ static int client_parse_message(
lease->next_server = message->siaddr;
lease->address = message->yiaddr;
if (client->bootp)
lease->lifetime = USEC_INFINITY;
if (lease->address == 0 ||
lease->server_address == 0 ||
lease->lifetime == 0)
@ -1601,7 +1619,7 @@ static int client_handle_offer_or_rapid_ack(sd_dhcp_client *client, DHCPMessage
dhcp_lease_unref_and_replace(client->lease, lease);
if (client->lease->rapid_commit) {
if (client->lease->rapid_commit || client->bootp) {
log_dhcp_client(client, "ACK");
return SD_DHCP_CLIENT_EVENT_IP_ACQUIRE;
}
@ -2007,7 +2025,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, s
if (r < 0)
return 0; /* invalid message, let's ignore it */
if (client->lease->rapid_commit)
if (client->lease->rapid_commit || client->bootp)
/* got a successful rapid commit */
return client_enter_bound(client, r);
@ -2405,7 +2423,7 @@ static sd_dhcp_client *dhcp_client_free(sd_dhcp_client *client) {
DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client, sd_dhcp_client, dhcp_client_free);
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) {
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize, int bootp) {
const uint8_t *opts;
size_t n_opts;
int r;
@ -2428,6 +2446,7 @@ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) {
.max_discover_attempts = UINT64_MAX,
.max_request_attempts = 5,
.ip_service_type = -1,
.bootp = !!bootp,
};
/* NOTE: this could be moved to a function. */
if (anonymize) {

View File

@ -49,7 +49,7 @@ static void test_request_basic(sd_event *e) {
printf("* %s\n", __func__);
/* Initialize client without Anonymize settings. */
r = sd_dhcp_client_new(&client, false);
r = sd_dhcp_client_new(&client, false, false);
assert_se(r >= 0);
assert_se(client);
@ -109,7 +109,7 @@ static void test_request_anonymize(sd_event *e) {
printf("* %s\n", __func__);
/* Initialize client with Anonymize settings. */
r = sd_dhcp_client_new(&client, true);
r = sd_dhcp_client_new(&client, true, false);
assert_se(r >= 0);
assert_se(client);
@ -282,7 +282,7 @@ static void test_discover_message(sd_event *e) {
if (verbose)
printf("* %s\n", __func__);
r = sd_dhcp_client_new(&client, false);
r = sd_dhcp_client_new(&client, false, false);
assert_se(r >= 0);
assert_se(client);
@ -499,7 +499,7 @@ static void test_addr_acq(sd_event *e) {
if (verbose)
printf("* %s\n", __func__);
r = sd_dhcp_client_new(&client, false);
r = sd_dhcp_client_new(&client, false, false);
assert_se(r >= 0);
assert_se(client);

View File

@ -1481,7 +1481,7 @@ static int dhcp4_configure(Link *link) {
if (link->dhcp_client)
return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured.");
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize, link->network->dhcp_send_bootp);
if (r < 0)
return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m");

View File

@ -243,6 +243,7 @@ DHCPv4.QuickAck, config_parse_bool,
DHCPv4.RequestOptions, config_parse_dhcp_request_options, AF_INET, 0
DHCPv4.Anonymize, config_parse_bool, 0, offsetof(Network, dhcp_anonymize)
DHCPv4.SendHostname, config_parse_dhcp_send_hostname, AF_INET, 0
DHCPv4.Bootp, config_parse_bool, 0, offsetof(Network, dhcp_send_bootp)
DHCPv4.Hostname, config_parse_hostname, 0, offsetof(Network, dhcp_hostname)
DHCPv4.Label, config_parse_dhcp_label, 0, offsetof(Network, dhcp_label)
DHCPv4.RequestBroadcast, config_parse_tristate, 0, offsetof(Network, dhcp_broadcast)
@ -612,6 +613,7 @@ DHCP.UseDomainName, config_parse_use_domains,
DHCP.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_use_routes)
DHCP.Anonymize, config_parse_bool, 0, offsetof(Network, dhcp_anonymize)
DHCP.SendHostname, config_parse_dhcp_send_hostname, AF_UNSPEC, 0
DHCP.Bootp, config_parse_bool, 0, offsetof(Network, dhcp_send_bootp)
DHCP.Hostname, config_parse_hostname, 0, offsetof(Network, dhcp_hostname)
DHCP.RequestBroadcast, config_parse_tristate, 0, offsetof(Network, dhcp_broadcast)
DHCP.CriticalConnection, config_parse_tristate, 0, offsetof(Network, dhcp_critical)

View File

@ -405,6 +405,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.dhcp_broadcast = -1,
.dhcp_ipv6_only_mode = -1,
.dhcp_6rd_prefix_route_type = RTN_UNREACHABLE,
.dhcp_send_bootp = false,
.dhcp6_use_address = true,
.dhcp6_use_pd_prefix = true,

View File

@ -179,6 +179,7 @@ struct Network {
OrderedHashmap *dhcp_client_send_vendor_options;
char *dhcp_netlabel;
NFTSetContext dhcp_nft_set_context;
bool dhcp_send_bootp;
/* DHCPv6 Client support */
bool dhcp6_use_address;

View File

@ -165,7 +165,7 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client);
/* NOTE: anonymize parameter is used to initialize PRL memory with different
* options when using RFC7844 Anonymity Profiles */
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize);
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize, int bootp);
int sd_dhcp_client_attach_event(
sd_dhcp_client *client,