summaryrefslogtreecommitdiff
path: root/src/libsystemd-network
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsystemd-network')
-rw-r--r--src/libsystemd-network/dhcp-server-internal.h1
-rw-r--r--src/libsystemd-network/sd-dhcp-server.c26
-rw-r--r--src/libsystemd-network/test-dhcp-server.c9
3 files changed, 31 insertions, 5 deletions
diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h
index cdaa471b62..480da22c01 100644
--- a/src/libsystemd-network/dhcp-server-internal.h
+++ b/src/libsystemd-network/dhcp-server-internal.h
@@ -55,6 +55,7 @@ struct sd_dhcp_server {
int index;
be32_t address;
+ be32_t netmask;
be32_t pool_start;
size_t pool_size;
size_t next_offer;
diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
index 50d9b7fcfc..c224aa1512 100644
--- a/src/libsystemd-network/sd-dhcp-server.c
+++ b/src/libsystemd-network/sd-dhcp-server.c
@@ -51,13 +51,16 @@ int sd_dhcp_server_set_lease_pool(sd_dhcp_server *server, struct in_addr *addres
return 0;
}
-int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address) {
+int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address, unsigned char prefixlen) {
assert_return(server, -EINVAL);
assert_return(address, -EINVAL);
assert_return(address->s_addr, -EINVAL);
+ assert_return(prefixlen <= 32, -ERANGE);
assert_return(server->address == htobe32(INADDR_ANY), -EBUSY);
+ assert_return(server->netmask == htobe32(INADDR_ANY), -EBUSY);
server->address = address->s_addr;
+ server->netmask = htobe32(0xfffffffflu << (32 - prefixlen));
return 0;
}
@@ -150,6 +153,7 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
server->fd_raw = -1;
server->fd = -1;
server->address = htobe32(INADDR_ANY);
+ server->netmask = htobe32(INADDR_ANY);
server->index = ifindex;
server->leases_by_client_id = hashmap_new(client_id_hash_func, client_id_compare_func);
@@ -408,6 +412,16 @@ static int server_send_offer(sd_dhcp_server *server, DHCPRequest *req, be32_t ad
if (r < 0)
return r;
+ r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
+ DHCP_OPTION_SUBNET_MASK, 4, &server->netmask);
+ if (r < 0)
+ return r;
+
+ r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
+ DHCP_OPTION_ROUTER, 4, &server->address);
+ if (r < 0)
+ return r;
+
r = dhcp_server_send_packet(server, req, packet, DHCP_OFFER, offset);
if (r < 0)
return r;
@@ -433,6 +447,16 @@ static int server_send_ack(sd_dhcp_server *server, DHCPRequest *req, be32_t addr
if (r < 0)
return r;
+ r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
+ DHCP_OPTION_SUBNET_MASK, 4, &server->netmask);
+ if (r < 0)
+ return r;
+
+ r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
+ DHCP_OPTION_ROUTER, 4, &server->address);
+ if (r < 0)
+ return r;
+
r = dhcp_server_send_packet(server, req, packet, DHCP_ACK, offset);
if (r < 0)
return r;
diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c
index a9bdb9ab8b..a94b62f8e6 100644
--- a/src/libsystemd-network/test-dhcp-server.c
+++ b/src/libsystemd-network/test-dhcp-server.c
@@ -56,9 +56,10 @@ static int test_basic(sd_event *event) {
assert_se(!sd_dhcp_server_unref(server));
assert_se(sd_dhcp_server_start(server) == -EUNATCH);
- assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
- assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
- assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
+ assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
+ assert_se(sd_dhcp_server_set_address(server, &address_any, 38) == -ERANGE);
+ assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
+ assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
assert_se(sd_dhcp_server_set_lease_pool(server, &address_any, 1) == -EINVAL);
assert_se(sd_dhcp_server_set_lease_pool(server, &address_lo, 0) == -EINVAL);
@@ -120,7 +121,7 @@ static void test_message_handler(void) {
};
assert_se(sd_dhcp_server_new(&server, 1) >= 0);
- assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
+ assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
assert_se(sd_dhcp_server_start(server) >= 0);