sd-dhcp-server.c revision 02557f973aed0fed7154fefe53d67e2935f918dc
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering This file is part of systemd.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright (C) 2013 Intel Corporation. All rights reserved.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Copyright (C) 2014 Tom Gundersen
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (at your option) any later version.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering systemd is distributed in the hope that it will be useful, but
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering Lesser General Public License for more details.
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering You should have received a copy of the GNU Lesser General Public License
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringint sd_dhcp_server_set_lease_pool(sd_dhcp_server *server, struct in_addr *address,
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering assert_return(server->pool_start == htobe32(INADDR_ANY), -EBUSY);
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering assert_return(!server->pool_size, -EBUSY);
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering assert_return(!server->bound_leases, -EBUSY);
3ed08c446cfaaae2b234fdfeb0c34ab6b4748c3eLennart Poettering server->bound_leases = new0(DHCPLease*, size);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringint sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert_return(server->address == htobe32(INADDR_ANY), -EBUSY);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringsd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) {
bb99a35a873c35e80b0b47fe045081022660374dLennart Poettering assert_se(REFCNT_INC(server->n_ref) >= 2);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringunsigned long client_id_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering siphash24((uint8_t*) &u, id->data, id->length, hash_key);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering return (unsigned long) u;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringint client_id_compare_func(const void *_a, const void *_b) {
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen return memcmp(a->data, b->data, a->length);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmekstatic void dhcp_lease_free(DHCPLease *lease) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DHCPLease*, dhcp_lease_free);
d0bbc21caa6e68693a47db60c93e99422bf2a858Lennart Poettering#define _cleanup_dhcp_lease_free_ _cleanup_(dhcp_lease_freep)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringsd_dhcp_server *sd_dhcp_server_unref(sd_dhcp_server *server) {
44b601bc79e46722bc0f0862ee0ce34a2284ef11Lennart Poettering if (server && REFCNT_DEC(server->n_ref) <= 0) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering HASHMAP_FOREACH(lease, server->leases_by_client_id, i) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering hashmap_remove(server->leases_by_client_id, lease);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering hashmap_free(server->leases_by_client_id);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringint sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering server->leases_by_client_id = hashmap_new(client_id_hash_func, client_id_compare_func);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringint sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int priority) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringint sd_dhcp_server_detach_event(sd_dhcp_server *server) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering server->event = sd_event_unref(server->event);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringsd_event *sd_dhcp_server_get_event(sd_dhcp_server *server) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringint sd_dhcp_server_stop(sd_dhcp_server *server) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering sd_event_source_unref(server->receive_message);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering server->fd_raw = safe_close(server->fd_raw);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poetteringstatic int dhcp_server_send_unicast_raw(sd_dhcp_server *server, DHCPPacket *packet,
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .ll.sll_protocol = htons(ETH_P_IP),
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek .ll.sll_ifindex = server->index,
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering memcpy(&link.ll.sll_addr, &packet->dhcp.chaddr, ETH_ALEN);
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen dhcp_packet_append_ip_headers(packet, server->address, DHCP_PORT_SERVER,
1ae464e09376853c52075ec4d8a6bfc4b4036d0cThomas Hindoe Paaboel Andersen packet->dhcp.yiaddr, DHCP_PORT_CLIENT, len);
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering r = dhcp_network_send_raw_socket(server->fd_raw, &link, packet, len);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poetteringstatic int dhcp_server_send_udp(sd_dhcp_server *server, be32_t destination,
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering .in.sin_port = htobe16(DHCP_PORT_CLIENT),
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering uint8_t cmsgbuf[CMSG_LEN(sizeof(struct in_pktinfo))] = {};
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* we attach source interface and address info to the message
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering rather than binding the socket. This will be mostly useful
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering when we gain support for arbitrary number of server addresses
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering pktinfo = (struct in_pktinfo*) CMSG_DATA(cmsg);
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering pktinfo->ipi_spec_dst.s_addr = server->address;
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poetteringstatic bool requested_broadcast(DHCPRequest *req) {
ee55db41442ad8055f5a84a339b1e0e22bc037c4Lennart Poettering return req->message->flags & htobe16(0x8000);
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poetteringint dhcp_server_send_packet(sd_dhcp_server *server,
6c045c0b4c49c88a1d3b9360c05efa5084796d2dZbigniew Jędrzejewski-Szmek assert(optoffset <= req->max_optlen);
6c045c0b4c49c88a1d3b9360c05efa5084796d2dZbigniew Jędrzejewski-Szmek r = dhcp_option_append(&packet->dhcp, req->max_optlen, &optoffset, 0,
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering r = dhcp_option_append(&packet->dhcp, req->max_optlen, &optoffset, 0,
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering /* RFC 2131 Section 4.1
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering If the ’giaddr’ field in a DHCP message from a client is non-zero,
73843b52585d42cc1a970a1c664818ece6942e9eLennart Poettering the server sends any return messages to the ’DHCP server’ port on the
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering BOOTP relay agent whose address appears in ’giaddr’. If the ’giaddr’
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering field is zero and the ’ciaddr’ field is nonzero, then the server
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering unicasts DHCPOFFER and DHCPACK messages to the address in ’ciaddr’.
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering If ’giaddr’ is zero and ’ciaddr’ is zero, and the broadcast bit is
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering set, then the server broadcasts DHCPOFFER and DHCPACK messages to
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering 0xffffffff. If the broadcast bit is not set and ’giaddr’ is zero and
73843b52585d42cc1a970a1c664818ece6942e9eLennart Poettering ’ciaddr’ is zero, then the server unicasts DHCPOFFER and DHCPACK
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering messages to the client’s hardware address and ’yiaddr’ address. In
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering all cases, when ’giaddr’ is zero, the server broadcasts any DHCPNAK
87b0284327e34a4b96c22085fa2cdb3219294991Zbigniew Jędrzejewski-Szmek messages to 0xffffffff.
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering If ’giaddr’ is set in the DHCPREQUEST message, the client is on a
73843b52585d42cc1a970a1c664818ece6942e9eLennart Poettering different subnet. The server MUST set the broadcast bit in the
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
73843b52585d42cc1a970a1c664818ece6942e9eLennart Poettering client, because the client may not have a correct network address
c79e98eadd3056a36a662699fa650db5b1bca0c3Lennart Poettering or subnet mask, and the client may not be answering ARP requests.
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering } else if (req->message->ciaddr && type != DHCP_NAK)
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (destination || requested_broadcast(req) || type == DHCP_NAK)
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering return dhcp_server_send_udp(server, destination, &packet->dhcp,
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering /* we cannot send UDP packet to specific MAC address when the address is
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering not yet configured, so must fall back to raw packets */
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering return dhcp_server_send_unicast_raw(server, packet,
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poetteringstatic int server_message_init(sd_dhcp_server *server, DHCPPacket **ret,
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering uint8_t type, size_t *_optoffset, DHCPRequest *req) {
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering _cleanup_free_ DHCPPacket *packet = NULL;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering assert(IN_SET(type, DHCP_OFFER, DHCP_ACK, DHCP_NAK));
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering packet = malloc0(sizeof(DHCPPacket) + req->max_optlen);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = dhcp_message_init(&packet->dhcp, BOOTREPLY, be32toh(req->message->xid),
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering packet->dhcp.flags = req->message->flags;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering packet->dhcp.giaddr = req->message->giaddr;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering memcpy(&packet->dhcp.chaddr, &req->message->chaddr, ETH_ALEN);
5ffa8c818120e35c89becd938d160235c069dd12Zbigniew Jędrzejewski-Szmekstatic int server_send_offer(sd_dhcp_server *server, DHCPRequest *req, be32_t address) {
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering _cleanup_free_ DHCPPacket *packet = NULL;
5c0aa72a4999bdcf03fe93ed5c8213c2b4c681f0Lennart Poettering r = server_message_init(server, &packet, DHCP_OFFER, &offset, req);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4, &lease_time);
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering r = dhcp_server_send_packet(server, req, packet, DHCP_OFFER, offset);
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmekstatic int server_send_ack(sd_dhcp_server *server, DHCPRequest *req, be32_t address) {
6e5abe1564070a760196b97031eca9cf5e95e8a2Zbigniew Jędrzejewski-Szmek _cleanup_free_ DHCPPacket *packet = NULL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = server_message_init(server, &packet, DHCP_ACK, &offset, req);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4, &lease_time);
61c024b328d5493a334242a4d01ba923582093faZbigniew Jędrzejewski-Szmek r = dhcp_server_send_packet(server, req, packet, DHCP_ACK, offset);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int server_send_nak(sd_dhcp_server *server, DHCPRequest *req) {
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering _cleanup_free_ DHCPPacket *packet = NULL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = server_message_init(server, &packet, DHCP_NAK, &offset, req);
4cd9a9d9ecf3a8835e21930f3215a5f5b74144beLennart Poettering r = dhcp_server_send_packet(server, req, packet, DHCP_NAK, offset);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int parse_request(uint8_t code, uint8_t len, const uint8_t *option,
61c024b328d5493a334242a4d01ba923582093faZbigniew Jędrzejewski-Szmek case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering req->lifetime = be32toh(*(be32_t*)option);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering req->max_optlen = be16toh(*(be16_t*)option) -
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic void dhcp_request_free(DHCPRequest *req) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DHCPRequest*, dhcp_request_free);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering#define _cleanup_dhcp_request_free_ _cleanup_(dhcp_request_freep)
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringstatic int ensure_sane_request(DHCPRequest *req, DHCPMessage *message) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering /* set client id based on mac address if client did not send an explicit one */
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering memcpy(&req->client_id.data[1], &message->chaddr, ETH_ALEN);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (req->max_optlen < DHCP_MIN_OPTIONS_SIZE)
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poetteringstatic int get_pool_offset(sd_dhcp_server *server, be32_t requested_ip) {
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering if (be32toh(requested_ip) < be32toh(server->pool_start) ||
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering be32toh(requested_ip) >= be32toh(server->pool_start) +
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering return be32toh(requested_ip) - be32toh(server->pool_start);
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poetteringint dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
b070e7f3c9ed680c821bd89d42506695f2438506Lennart Poettering _cleanup_dhcp_request_free_ DHCPRequest *req = NULL;
18c7ed186be28800a2eeb37ad31c9c44480d3d9cLennart Poettering type = dhcp_option_parse(message, length, parse_request, req);
switch(type) {
case DHCP_DISCOVER:
if (existing_lease)
strerror(-r));
return DHCP_OFFER;
case DHCP_DECLINE:
case DHCP_REQUEST:
bool init_reboot = false;
int pool_offset;
init_reboot = true;
if (pool_offset >= 0 &&
if (!existing_lease) {
return -ENOMEM;
strerror(-r));
if (!existing_lease)
return DHCP_ACK;
} else if (init_reboot) {
strerror(-r));
return DHCP_NAK;
case DHCP_RELEASE: {
int pool_offset;
if (!existing_lease)
if (pool_offset < 0)
if (buflen < 0)
return -EIO;
if (!message)
return -ENOMEM;
r = -errno;