Merge pull request #19743 from yuwata/dhcpv4-static-leases
network: dhcp-server: support static lease
This commit is contained in:
@@ -2535,6 +2535,29 @@ IPv6Token=prefixstable:2002:da8:1::</programlisting></para>
|
||||
</variablelist>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>[DHCPServerStaticLease] Section Options</title>
|
||||
<para>The <literal>[DHCPServerStaticLease]</literal> section configures a static DHCP lease to
|
||||
assign a pre-set IPv4 address to a specific device based on its MAC address. This section can be
|
||||
specified multiple times.</para>
|
||||
|
||||
<variablelist class='network-directives'>
|
||||
<varlistentry>
|
||||
<term><varname>MACAddress=</varname></term>
|
||||
|
||||
<listitem><para>The hardware address of a device which should be assigned IPv4 address
|
||||
specified in <varname>Address=</varname>. This key is mandatory.</para></listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><varname>Address=</varname></term>
|
||||
|
||||
<listitem><para>IPv4 address that should be assigned to a device with a hardware address
|
||||
specified in <varname>MACAddress=</varname>. This key is mandatory.</para></listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</refsect1>
|
||||
|
||||
<refsect1>
|
||||
<title>[IPv6SendRA] Section Options</title>
|
||||
<para>The [IPv6SendRA] section contains settings for sending IPv6 Router Advertisements and whether
|
||||
|
||||
@@ -68,6 +68,7 @@ struct sd_dhcp_server {
|
||||
bool emit_router;
|
||||
|
||||
Hashmap *leases_by_client_id;
|
||||
Hashmap *static_leases_by_client_id;
|
||||
DHCPLease **bound_leases;
|
||||
DHCPLease invalid_lease;
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@ static DHCPLease *dhcp_lease_free(DHCPLease *lease) {
|
||||
return mfree(lease);
|
||||
}
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, dhcp_lease_free);
|
||||
|
||||
/* configures the server's address and subnet, and optionally the pool's size and offset into the subnet
|
||||
* the whole pool must fit into the subnet, and may not contain the first (any) nor last (broadcast) address
|
||||
* moreover, the server's own address may be in the pool, and is in that case reserved in order not to
|
||||
@@ -160,6 +162,7 @@ static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) {
|
||||
free(server->servers[i].addr);
|
||||
|
||||
hashmap_free(server->leases_by_client_id);
|
||||
hashmap_free(server->static_leases_by_client_id);
|
||||
|
||||
ordered_set_free(server->extra_options);
|
||||
ordered_set_free(server->vendor_options);
|
||||
@@ -201,6 +204,9 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
|
||||
server->leases_by_client_id = hashmap_new(&dhcp_lease_hash_ops);
|
||||
if (!server->leases_by_client_id)
|
||||
return -ENOMEM;
|
||||
server->static_leases_by_client_id = hashmap_new(&dhcp_lease_hash_ops);
|
||||
if (!server->static_leases_by_client_id)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(server);
|
||||
|
||||
@@ -802,13 +808,55 @@ static int dhcp_server_relay_message(sd_dhcp_server *server, DHCPMessage *messag
|
||||
return -EBADMSG;
|
||||
}
|
||||
|
||||
static int prepare_new_lease(
|
||||
DHCPLease **ret_lease,
|
||||
be32_t address,
|
||||
const DHCPClientId *client_id,
|
||||
const uint8_t chaddr[static ETH_ALEN],
|
||||
be32_t gateway,
|
||||
usec_t expiration) {
|
||||
|
||||
_cleanup_(dhcp_lease_freep) DHCPLease *lease = NULL;
|
||||
|
||||
lease = new(DHCPLease, 1);
|
||||
if (!lease)
|
||||
return -ENOMEM;
|
||||
|
||||
*lease = (DHCPLease) {
|
||||
.address = address,
|
||||
.client_id.length = client_id->length,
|
||||
.gateway = gateway,
|
||||
.expiration = expiration,
|
||||
};
|
||||
lease->client_id.data = memdup(client_id->data, client_id->length);
|
||||
if (!lease->client_id.data)
|
||||
return -ENOMEM;
|
||||
|
||||
memcpy(&lease->chaddr, chaddr, ETH_ALEN);
|
||||
|
||||
*ret_lease = TAKE_PTR(lease);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool static_leases_have_address(sd_dhcp_server *server, be32_t address) {
|
||||
DHCPLease *s;
|
||||
|
||||
assert(server);
|
||||
|
||||
HASHMAP_FOREACH(s, server->static_leases_by_client_id)
|
||||
if (s->address == address)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#define HASH_KEY SD_ID128_MAKE(0d,1d,fe,bd,f1,24,bd,b3,47,f1,dd,6e,73,21,93,30)
|
||||
|
||||
int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
|
||||
size_t length) {
|
||||
int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message, size_t length) {
|
||||
_cleanup_(dhcp_request_freep) DHCPRequest *req = NULL;
|
||||
_cleanup_free_ char *error_message = NULL;
|
||||
DHCPLease *existing_lease;
|
||||
DHCPLease *existing_lease, *static_lease;
|
||||
int type, r;
|
||||
|
||||
assert(server);
|
||||
@@ -832,8 +880,8 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
|
||||
/* this only fails on critical errors */
|
||||
return r;
|
||||
|
||||
existing_lease = hashmap_get(server->leases_by_client_id,
|
||||
&req->client_id);
|
||||
existing_lease = hashmap_get(server->leases_by_client_id, &req->client_id);
|
||||
static_lease = hashmap_get(server->static_leases_by_client_id, &req->client_id);
|
||||
|
||||
switch(type) {
|
||||
|
||||
@@ -841,15 +889,16 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
|
||||
be32_t address = INADDR_ANY;
|
||||
unsigned i;
|
||||
|
||||
log_dhcp_server(server, "DISCOVER (0x%x)",
|
||||
be32toh(req->message->xid));
|
||||
log_dhcp_server(server, "DISCOVER (0x%x)", be32toh(req->message->xid));
|
||||
|
||||
if (!server->pool_size)
|
||||
/* no pool allocated */
|
||||
return 0;
|
||||
|
||||
/* for now pick a random free address from the pool */
|
||||
if (existing_lease)
|
||||
if (static_lease)
|
||||
address = static_lease->address;
|
||||
else if (existing_lease)
|
||||
address = existing_lease->address;
|
||||
else {
|
||||
struct siphash state;
|
||||
@@ -867,8 +916,11 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
|
||||
|
||||
for (i = 0; i < server->pool_size; i++) {
|
||||
if (!server->bound_leases[next_offer]) {
|
||||
address = server->subnet | htobe32(server->pool_offset + next_offer);
|
||||
break;
|
||||
be32_t tmp = server->subnet | htobe32(server->pool_offset + next_offer);
|
||||
if (!static_leases_have_address(server, tmp)) {
|
||||
address = tmp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
next_offer = (next_offer + 1) % server->pool_size;
|
||||
@@ -948,63 +1000,82 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
|
||||
|
||||
/* verify that the requested address is from the pool, and either
|
||||
owned by the current client or free */
|
||||
if (pool_offset >= 0 &&
|
||||
server->bound_leases[pool_offset] == existing_lease) {
|
||||
DHCPLease *lease;
|
||||
usec_t time_now = 0;
|
||||
if (pool_offset >= 0 && static_lease) {
|
||||
_cleanup_(dhcp_lease_freep) DHCPLease *lease = NULL, *old_lease = NULL;
|
||||
usec_t time_now, expiration;
|
||||
|
||||
if (!existing_lease) {
|
||||
lease = new0(DHCPLease, 1);
|
||||
if (!lease)
|
||||
return -ENOMEM;
|
||||
lease->address = address;
|
||||
lease->client_id.data = memdup(req->client_id.data,
|
||||
req->client_id.length);
|
||||
if (!lease->client_id.data) {
|
||||
free(lease);
|
||||
return -ENOMEM;
|
||||
}
|
||||
lease->client_id.length = req->client_id.length;
|
||||
memcpy(&lease->chaddr, &req->message->chaddr,
|
||||
ETH_ALEN);
|
||||
lease->gateway = req->message->giaddr;
|
||||
} else
|
||||
lease = existing_lease;
|
||||
|
||||
r = sd_event_now(server->event,
|
||||
clock_boottime_or_monotonic(),
|
||||
&time_now);
|
||||
if (r < 0) {
|
||||
if (!existing_lease)
|
||||
dhcp_lease_free(lease);
|
||||
r = sd_event_now(server->event, clock_boottime_or_monotonic(), &time_now);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
lease->expiration = req->lifetime * USEC_PER_SEC + time_now;
|
||||
expiration = usec_add(req->lifetime * USEC_PER_SEC, time_now);
|
||||
|
||||
r = prepare_new_lease(&lease, static_lease->address, &req->client_id,
|
||||
req->message->chaddr, req->message->giaddr, expiration);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = server_send_offer_or_ack(server, req, address, DHCP_ACK);
|
||||
if (r < 0) {
|
||||
if (r < 0)
|
||||
/* this only fails on critical errors */
|
||||
log_dhcp_server_errno(server, r, "Could not send ack: %m");
|
||||
return log_dhcp_server_errno(server, r, "Could not send ack: %m");
|
||||
|
||||
if (!existing_lease)
|
||||
dhcp_lease_free(lease);
|
||||
log_dhcp_server(server, "ACK (0x%x)", be32toh(req->message->xid));
|
||||
|
||||
server->bound_leases[pool_offset] = lease;
|
||||
|
||||
old_lease = hashmap_remove(server->leases_by_client_id, &lease->client_id);
|
||||
r = hashmap_put(server->leases_by_client_id, &lease->client_id, lease);
|
||||
if (r < 0)
|
||||
return log_dhcp_server_errno(server, r, "Could not save lease: %m");
|
||||
TAKE_PTR(lease);
|
||||
|
||||
if (server->callback)
|
||||
server->callback(server, SD_DHCP_SERVER_EVENT_LEASE_CHANGED, server->callback_userdata);
|
||||
|
||||
return DHCP_ACK;
|
||||
|
||||
} else if (pool_offset >= 0 && server->bound_leases[pool_offset] == existing_lease) {
|
||||
_cleanup_(dhcp_lease_freep) DHCPLease *new_lease = NULL;
|
||||
usec_t time_now, expiration;
|
||||
DHCPLease *lease;
|
||||
|
||||
r = sd_event_now(server->event, clock_boottime_or_monotonic(), &time_now);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
expiration = usec_add(req->lifetime * USEC_PER_SEC, time_now);
|
||||
|
||||
if (!existing_lease) {
|
||||
r = prepare_new_lease(&new_lease, address, &req->client_id,
|
||||
req->message->chaddr, req->message->giaddr, expiration);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
lease = new_lease;
|
||||
} else {
|
||||
log_dhcp_server(server, "ACK (0x%x)",
|
||||
be32toh(req->message->xid));
|
||||
|
||||
server->bound_leases[pool_offset] = lease;
|
||||
hashmap_put(server->leases_by_client_id,
|
||||
&lease->client_id, lease);
|
||||
|
||||
if (server->callback)
|
||||
server->callback(server, SD_DHCP_SERVER_EVENT_LEASE_CHANGED, server->callback_userdata);
|
||||
|
||||
return DHCP_ACK;
|
||||
existing_lease->expiration = expiration;
|
||||
lease = existing_lease;
|
||||
}
|
||||
|
||||
r = server_send_offer_or_ack(server, req, address, DHCP_ACK);
|
||||
if (r < 0)
|
||||
/* this only fails on critical errors */
|
||||
return log_dhcp_server_errno(server, r, "Could not send ack: %m");
|
||||
|
||||
log_dhcp_server(server, "ACK (0x%x)", be32toh(req->message->xid));
|
||||
|
||||
server->bound_leases[pool_offset] = lease;
|
||||
r = hashmap_put(server->leases_by_client_id, &lease->client_id, lease);
|
||||
if (r < 0)
|
||||
return log_dhcp_server_errno(server, r, "Could not save lease: %m");
|
||||
TAKE_PTR(new_lease);
|
||||
|
||||
if (server->callback)
|
||||
server->callback(server, SD_DHCP_SERVER_EVENT_LEASE_CHANGED, server->callback_userdata);
|
||||
|
||||
return DHCP_ACK;
|
||||
|
||||
} else if (init_reboot) {
|
||||
r = server_send_nak(server, req);
|
||||
if (r < 0)
|
||||
@@ -1397,3 +1468,61 @@ int sd_dhcp_server_set_relay_agent_information(
|
||||
free_and_replace(server->agent_remote_id, remote_id_dup);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_dhcp_server_set_static_lease(
|
||||
sd_dhcp_server *server,
|
||||
const struct in_addr *address,
|
||||
uint8_t *client_id,
|
||||
size_t client_id_size) {
|
||||
|
||||
_cleanup_(dhcp_lease_freep) DHCPLease *lease = NULL, *old = NULL;
|
||||
DHCPClientId c;
|
||||
int r;
|
||||
|
||||
assert_return(server, -EINVAL);
|
||||
assert_return(client_id, -EINVAL);
|
||||
assert_return(client_id_size == ETH_ALEN + 1, -EINVAL);
|
||||
assert_return(!sd_dhcp_server_is_running(server), -EBUSY);
|
||||
|
||||
/* Static lease with an empty or omitted address is a valid entry,
|
||||
* the server removes any static lease with the specified mac address. */
|
||||
if (!address || address->s_addr == 0) {
|
||||
_cleanup_free_ void *data = NULL;
|
||||
|
||||
data = memdup(client_id, client_id_size);
|
||||
if (!data)
|
||||
return -ENOMEM;
|
||||
|
||||
c = (DHCPClientId) {
|
||||
.length = client_id_size,
|
||||
.data = data,
|
||||
};
|
||||
|
||||
old = hashmap_remove(server->static_leases_by_client_id, &c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (static_leases_have_address(server, address->s_addr))
|
||||
return -EEXIST;
|
||||
|
||||
lease = new(DHCPLease, 1);
|
||||
if (!lease)
|
||||
return -ENOMEM;
|
||||
|
||||
*lease = (DHCPLease) {
|
||||
.address = address->s_addr,
|
||||
.client_id.length = client_id_size,
|
||||
.gateway = 0,
|
||||
.expiration = 0,
|
||||
};
|
||||
lease->client_id.data = memdup(client_id, client_id_size);
|
||||
if (!lease->client_id.data)
|
||||
return -ENOMEM;
|
||||
|
||||
r = hashmap_ensure_put(&server->static_leases_by_client_id, &dhcp_lease_hash_ops, &lease->client_id, lease);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
TAKE_PTR(lease);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ sources = files('''
|
||||
networkd-dhcp-common.h
|
||||
networkd-dhcp-server-bus.c
|
||||
networkd-dhcp-server-bus.h
|
||||
networkd-dhcp-server-static-lease.c
|
||||
networkd-dhcp-server-static-lease.h
|
||||
networkd-dhcp-server.c
|
||||
networkd-dhcp-server.h
|
||||
networkd-dhcp4.c
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "ether-addr-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "networkd-dhcp-server-static-lease.h"
|
||||
#include "networkd-network.h"
|
||||
#include "networkd-util.h"
|
||||
|
||||
DEFINE_NETWORK_SECTION_FUNCTIONS(DHCPStaticLease, dhcp_static_lease_free);
|
||||
|
||||
DHCPStaticLease *dhcp_static_lease_free(DHCPStaticLease *static_lease) {
|
||||
if (!static_lease)
|
||||
return NULL;
|
||||
|
||||
if (static_lease->network && static_lease->section)
|
||||
hashmap_remove(static_lease->network->dhcp_static_leases_by_section, static_lease->section);
|
||||
|
||||
network_config_section_free(static_lease->section);
|
||||
free(static_lease->client_id);
|
||||
return mfree(static_lease);
|
||||
}
|
||||
|
||||
static int dhcp_static_lease_new(DHCPStaticLease **ret) {
|
||||
DHCPStaticLease *p;
|
||||
|
||||
assert(ret);
|
||||
|
||||
p = new0(DHCPStaticLease, 1);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
*ret = TAKE_PTR(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lease_new_static(Network *network, const char *filename, unsigned section_line, DHCPStaticLease **ret) {
|
||||
_cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
|
||||
_cleanup_(dhcp_static_lease_freep) DHCPStaticLease *static_lease = NULL;
|
||||
int r;
|
||||
|
||||
assert(network);
|
||||
assert(filename);
|
||||
assert(section_line > 0);
|
||||
assert(ret);
|
||||
|
||||
r = network_config_section_new(filename, section_line, &n);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
static_lease = hashmap_get(network->dhcp_static_leases_by_section, n);
|
||||
if (static_lease) {
|
||||
*ret = TAKE_PTR(static_lease);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = dhcp_static_lease_new(&static_lease);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
static_lease->network = network;
|
||||
static_lease->section = TAKE_PTR(n);
|
||||
r = hashmap_ensure_put(&network->dhcp_static_leases_by_section, &network_config_hash_ops, static_lease->section, static_lease);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = TAKE_PTR(static_lease);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int static_lease_verify(DHCPStaticLease *static_lease) {
|
||||
if (section_is_invalid(static_lease->section))
|
||||
return -EINVAL;
|
||||
|
||||
if (in4_addr_is_null(&static_lease->address))
|
||||
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"%s: DHCP static lease without Address= field configured. "
|
||||
"Ignoring [DHCPServerStaticLease] section from line %u.",
|
||||
static_lease->section->filename, static_lease->section->line);
|
||||
|
||||
/* TODO: check that the address is in the pool. */
|
||||
|
||||
if (static_lease->client_id_size == 0 || !static_lease->client_id)
|
||||
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"%s: DHCP static lease without MACAddress= field configured. "
|
||||
"Ignoring [DHCPServerStaticLease] section from line %u.",
|
||||
static_lease->section->filename, static_lease->section->line);
|
||||
|
||||
assert(static_lease->client_id_size == ETH_ALEN + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void network_drop_invalid_static_leases(Network *network) {
|
||||
DHCPStaticLease *static_lease;
|
||||
|
||||
assert(network);
|
||||
|
||||
HASHMAP_FOREACH(static_lease, network->dhcp_static_leases_by_section)
|
||||
if (static_lease_verify(static_lease) < 0)
|
||||
dhcp_static_lease_free(static_lease);
|
||||
}
|
||||
|
||||
int config_parse_dhcp_static_lease_address(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *lvalue,
|
||||
int ltype,
|
||||
const char *rvalue,
|
||||
void *data,
|
||||
void *userdata) {
|
||||
|
||||
_cleanup_(dhcp_static_lease_free_or_set_invalidp) DHCPStaticLease *lease = NULL;
|
||||
Network *network = userdata;
|
||||
union in_addr_union addr;
|
||||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(lvalue);
|
||||
assert(rvalue);
|
||||
assert(network);
|
||||
|
||||
r = lease_new_static(network, filename, section_line, &lease);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
if (isempty(rvalue)) {
|
||||
lease->address.s_addr = 0;
|
||||
TAKE_PTR(lease);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = in_addr_from_string(AF_INET, rvalue, &addr);
|
||||
if (r < 0) {
|
||||
log_syntax(unit, LOG_WARNING, filename, line, r,
|
||||
"Failed to parse IPv4 address for DHCPv4 static lease, ignoring assignment: %s", rvalue);
|
||||
return 0;
|
||||
}
|
||||
if (in4_addr_is_null(&addr.in)) {
|
||||
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
||||
"IPv4 address for DHCPv4 static lease cannot be the ANY address, ignoring assignment: %s", rvalue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lease->address = addr.in;
|
||||
|
||||
TAKE_PTR(lease);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_parse_dhcp_static_lease_hwaddr(
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *lvalue,
|
||||
int ltype,
|
||||
const char *rvalue,
|
||||
void *data,
|
||||
void *userdata) {
|
||||
|
||||
_cleanup_(dhcp_static_lease_free_or_set_invalidp) DHCPStaticLease *lease = NULL;
|
||||
Network *network = userdata;
|
||||
struct ether_addr hwaddr;
|
||||
uint8_t *c;
|
||||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(lvalue);
|
||||
assert(rvalue);
|
||||
assert(network);
|
||||
|
||||
r = lease_new_static(network, filename, section_line, &lease);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
if (isempty(rvalue)) {
|
||||
lease->client_id = mfree(lease->client_id);
|
||||
lease->client_id_size = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = ether_addr_from_string(rvalue, &hwaddr);
|
||||
if (r < 0) {
|
||||
log_syntax(unit, LOG_WARNING, filename, line, r,
|
||||
"Failed to parse MAC address for DHCPv4 static lease, ignoring assignment: %s", rvalue);
|
||||
return 0;
|
||||
}
|
||||
if (ether_addr_is_null(&hwaddr) || (hwaddr.ether_addr_octet[0] & 0x01)) {
|
||||
log_syntax(unit, LOG_WARNING, filename, line, 0,
|
||||
"MAC address for DHCPv4 static lease cannot be null or multicast, ignoring assignment: %s", rvalue);
|
||||
return 0;
|
||||
}
|
||||
|
||||
c = new(uint8_t, ETH_ALEN + 1);
|
||||
if (!c)
|
||||
return log_oom();
|
||||
|
||||
/* set client id type to 1: Ethernet Link-Layer (RFC 2132) */
|
||||
c[0] = 0x01;
|
||||
memcpy(c + 1, &hwaddr, ETH_ALEN);
|
||||
|
||||
free_and_replace(lease->client_id, c);
|
||||
lease->client_id_size = ETH_ALEN + 1;
|
||||
|
||||
TAKE_PTR(lease);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "conf-parser.h"
|
||||
#include "in-addr-util.h"
|
||||
|
||||
typedef struct Network Network;
|
||||
typedef struct NetworkConfigSection NetworkConfigSection;
|
||||
|
||||
typedef struct DHCPStaticLease {
|
||||
Network *network;
|
||||
NetworkConfigSection *section;
|
||||
|
||||
struct in_addr address;
|
||||
uint8_t *client_id;
|
||||
size_t client_id_size;
|
||||
} DHCPStaticLease;
|
||||
|
||||
DHCPStaticLease *dhcp_static_lease_free(DHCPStaticLease *lease);
|
||||
void network_drop_invalid_static_leases(Network *network);
|
||||
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_dhcp_static_lease_address);
|
||||
CONFIG_PARSER_PROTOTYPE(config_parse_dhcp_static_lease_hwaddr);
|
||||
@@ -9,8 +9,9 @@
|
||||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "networkd-address.h"
|
||||
#include "networkd-dhcp-server.h"
|
||||
#include "networkd-dhcp-server-bus.h"
|
||||
#include "networkd-dhcp-server-static-lease.h"
|
||||
#include "networkd-dhcp-server.h"
|
||||
#include "networkd-link.h"
|
||||
#include "networkd-manager.h"
|
||||
#include "networkd-network.h"
|
||||
@@ -291,6 +292,7 @@ static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
|
||||
int dhcp4_server_configure(Link *link) {
|
||||
bool acquired_uplink = false;
|
||||
sd_dhcp_option *p;
|
||||
DHCPStaticLease *static_lease;
|
||||
Link *uplink = NULL;
|
||||
Address *address;
|
||||
bool bind_to_interface;
|
||||
@@ -439,6 +441,12 @@ int dhcp4_server_configure(Link *link) {
|
||||
return log_link_error_errno(link, r, "Failed to set DHCPv4 option: %m");
|
||||
}
|
||||
|
||||
HASHMAP_FOREACH(static_lease, link->network->dhcp_static_leases_by_section) {
|
||||
r = sd_dhcp_server_set_static_lease(link->dhcp_server, &static_lease->address, static_lease->client_id, static_lease->client_id_size);
|
||||
if (r < 0)
|
||||
return log_link_error_errno(link, r, "Failed to set DHCPv4 static lease for DHCP server: %m");
|
||||
}
|
||||
|
||||
if (!sd_dhcp_server_is_running(link->dhcp_server)) {
|
||||
r = sd_dhcp_server_start(link->dhcp_server);
|
||||
if (r < 0)
|
||||
|
||||
@@ -12,6 +12,7 @@ _Pragma("GCC diagnostic ignored \"-Wimplicit-fallthrough\"")
|
||||
#include "networkd-bridge-fdb.h"
|
||||
#include "networkd-can.h"
|
||||
#include "networkd-dhcp-common.h"
|
||||
#include "networkd-dhcp-server-static-lease.h"
|
||||
#include "networkd-dhcp-server.h"
|
||||
#include "networkd-dhcp4.h"
|
||||
#include "networkd-dhcp6.h"
|
||||
@@ -288,6 +289,8 @@ DHCPServer.PoolSize, config_parse_uint32,
|
||||
DHCPServer.SendVendorOption, config_parse_dhcp_send_option, 0, offsetof(Network, dhcp_server_send_vendor_options)
|
||||
DHCPServer.SendOption, config_parse_dhcp_send_option, 0, offsetof(Network, dhcp_server_send_options)
|
||||
DHCPServer.BindToInterface, config_parse_bool, 0, offsetof(Network, dhcp_server_bind_to_interface)
|
||||
DHCPServerStaticLease.Address, config_parse_dhcp_static_lease_address, 0, 0
|
||||
DHCPServerStaticLease.MACAddress, config_parse_dhcp_static_lease_hwaddr, 0, 0
|
||||
Bridge.Cost, config_parse_uint32, 0, offsetof(Network, cost)
|
||||
Bridge.UseBPDU, config_parse_tristate, 0, offsetof(Network, use_bpdu)
|
||||
Bridge.HairPin, config_parse_tristate, 0, offsetof(Network, hairpin)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "networkd-address.h"
|
||||
#include "networkd-bridge-fdb.h"
|
||||
#include "networkd-dhcp-common.h"
|
||||
#include "networkd-dhcp-server-static-lease.h"
|
||||
#include "networkd-dhcp-server.h"
|
||||
#include "networkd-manager.h"
|
||||
#include "networkd-mdb.h"
|
||||
@@ -241,6 +242,7 @@ int network_verify(Network *network) {
|
||||
network_drop_invalid_routing_policy_rules(network);
|
||||
network_drop_invalid_traffic_control(network);
|
||||
network_drop_invalid_sr_iov(network);
|
||||
network_drop_invalid_static_leases(network);
|
||||
|
||||
network_adjust_dhcp_server(network);
|
||||
|
||||
@@ -414,6 +416,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
|
||||
"DHCPv6\0"
|
||||
"DHCPv6PrefixDelegation\0"
|
||||
"DHCPServer\0"
|
||||
"DHCPServerStaticLease\0"
|
||||
"IPv6AcceptRA\0"
|
||||
"IPv6NDPProxyAddress\0"
|
||||
"Bridge\0"
|
||||
@@ -607,6 +610,7 @@ static Network *network_free(Network *network) {
|
||||
hashmap_free_with_destructor(network->prefixes_by_section, prefix_free);
|
||||
hashmap_free_with_destructor(network->route_prefixes_by_section, route_prefix_free);
|
||||
hashmap_free_with_destructor(network->rules_by_section, routing_policy_rule_free);
|
||||
hashmap_free_with_destructor(network->dhcp_static_leases_by_section, dhcp_static_lease_free);
|
||||
ordered_hashmap_free_with_destructor(network->sr_iov_by_section, sr_iov_free);
|
||||
ordered_hashmap_free_with_destructor(network->tc_by_section, traffic_control_free);
|
||||
|
||||
|
||||
@@ -315,6 +315,7 @@ struct Network {
|
||||
Hashmap *prefixes_by_section;
|
||||
Hashmap *route_prefixes_by_section;
|
||||
Hashmap *rules_by_section;
|
||||
Hashmap *dhcp_static_leases_by_section;
|
||||
OrderedHashmap *tc_by_section;
|
||||
OrderedHashmap *sr_iov_by_section;
|
||||
|
||||
|
||||
@@ -77,6 +77,7 @@ int sd_dhcp_server_set_smtp(sd_dhcp_server *server, const struct in_addr smtp[],
|
||||
|
||||
int sd_dhcp_server_add_option(sd_dhcp_server *server, sd_dhcp_option *v);
|
||||
int sd_dhcp_server_add_vendor_option(sd_dhcp_server *server, sd_dhcp_option *v);
|
||||
int sd_dhcp_server_set_static_lease(sd_dhcp_server *server, const struct in_addr *address, uint8_t *client_id, size_t client_id_size);
|
||||
|
||||
int sd_dhcp_server_set_max_lease_time(sd_dhcp_server *server, uint32_t t);
|
||||
int sd_dhcp_server_set_default_lease_time(sd_dhcp_server *server, uint32_t t);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
[Match]
|
||||
Name=veth-peer
|
||||
|
||||
[Network]
|
||||
Address=10.1.1.1/24
|
||||
DHCPServer=true
|
||||
IPMasquerade=true
|
||||
IPForward=true
|
||||
|
||||
[DHCPServer]
|
||||
PoolOffset=0
|
||||
PoolSize=20
|
||||
EmitDNS=yes
|
||||
DNS=9.9.9.9
|
||||
|
||||
[DHCPServerStaticLease]
|
||||
MACAddress=12:34:56:78:9a:bc
|
||||
Address=10.1.1.2
|
||||
|
||||
[DHCPServerStaticLease]
|
||||
MACAddress=12:34:56:78:9a:bc
|
||||
Address=10.1.1.3
|
||||
|
||||
[DHCPServerStaticLease]
|
||||
Address=10.1.1.4
|
||||
|
||||
[DHCPServerStaticLease]
|
||||
MACAddress=12:34:56:78:9a:bf
|
||||
Address=10.1.1.5
|
||||
@@ -366,6 +366,9 @@ RelayTarget=
|
||||
RelayAgentCircuitId=
|
||||
RelayAgentRemoteId=
|
||||
ServerAddress=
|
||||
[DHCPServerStaticLease]
|
||||
MACAddress=
|
||||
Address=
|
||||
[NextHop]
|
||||
Id=
|
||||
Gateway=
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
[Match]
|
||||
Name=veth99
|
||||
|
||||
[Network]
|
||||
IPv6AcceptRA=no
|
||||
DHCP=ipv4
|
||||
|
||||
[DHCPv4]
|
||||
ClientIdentifier=mac
|
||||
@@ -0,0 +1,17 @@
|
||||
[Match]
|
||||
Name=veth-peer
|
||||
|
||||
[Network]
|
||||
IPv6AcceptRA=no
|
||||
Address=10.1.1.1/24
|
||||
DHCPServer=yes
|
||||
|
||||
[DHCPServer]
|
||||
PoolOffset=0
|
||||
PoolSize=20
|
||||
EmitDNS=yes
|
||||
DNS=9.9.9.9
|
||||
|
||||
[DHCPServerStaticLease]
|
||||
MACAddress=12:34:56:78:9a:bc
|
||||
Address=10.1.1.3
|
||||
@@ -3673,8 +3673,10 @@ class NetworkdDHCPServerTests(unittest.TestCase, Utilities):
|
||||
units = [
|
||||
'25-veth.netdev',
|
||||
'dhcp-client.network',
|
||||
'dhcp-client-static-lease.network',
|
||||
'dhcp-client-timezone-router.network',
|
||||
'dhcp-server.network',
|
||||
'dhcp-server-static-lease.network',
|
||||
'dhcp-server-timezone-router.network']
|
||||
|
||||
def setUp(self):
|
||||
@@ -3709,6 +3711,15 @@ class NetworkdDHCPServerTests(unittest.TestCase, Utilities):
|
||||
self.assertRegex(output, '192.168.5.*')
|
||||
self.assertRegex(output, 'Europe/Berlin')
|
||||
|
||||
def test_dhcp_server_static_lease(self):
|
||||
copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-client-static-lease.network', 'dhcp-server-static-lease.network')
|
||||
start_networkd()
|
||||
self.wait_online(['veth99:routable', 'veth-peer:routable'])
|
||||
|
||||
output = check_output(*networkctl_cmd, '-n', '0', 'status', 'veth99', env=env)
|
||||
print(output)
|
||||
self.assertIn('10.1.1.3 (DHCP4 via 10.1.1.1)', output)
|
||||
|
||||
class NetworkdDHCPServerRelayAgentTests(unittest.TestCase, Utilities):
|
||||
links = [
|
||||
'client',
|
||||
|
||||
Reference in New Issue
Block a user