Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dhcpv6_client: add DNS recursive name server option handling #16585

Merged
merged 1 commit into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PSEUDOMODULES += cpu_check_address
PSEUDOMODULES += dbgpin
PSEUDOMODULES += devfs_%
PSEUDOMODULES += dhcpv6_%
PSEUDOMODULES += dhcpv6_client_dns
PSEUDOMODULES += ecc_%
PSEUDOMODULES += event_%
PSEUDOMODULES += event_timeout_ztimer
Expand Down
3 changes: 3 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ ifneq (,$(filter dhcpv6_client,$(USEMODULE)))
USEMODULE += event
USEMODULE += random
USEMODULE += xtimer
ifneq (,$(filter sock_dns,$(USEMODULE)))
USEMODULE += dhcpv6_client_dns
endif
endif

ifneq (,$(filter fuzzing,$(USEMODULE)))
Expand Down
1 change: 1 addition & 0 deletions sys/include/net/dhcpv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extern "C" {
#define DHCPV6_OPT_PREF (7U) /**< preference option */
#define DHCPV6_OPT_ELAPSED_TIME (8U) /**< elapsed time option */
#define DHCPV6_OPT_STATUS (13U) /**< status code option */
#define DHCPV6_OPT_DNS_RNS (23U) /**< DNS recursive name server option */
#define DHCPV6_OPT_IA_PD (25U) /**< identity association for prefix
* delegation (IA_PD) option */
#define DHCPV6_OPT_IAPFX (26U) /**< IA prefix option */
Expand Down
24 changes: 24 additions & 0 deletions sys/net/application_layer/dhcpv6/_dhcpv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ typedef struct __attribute__((packed)) {
char msg[]; /**< UTF-8 encoded text string (not 0-terminated!) */
} dhcpv6_opt_status_t;

/**
* @brief DHCPv6 DNS recursive name server option
* @see [RFC 3646, section 3]
* (https://datatracker.ietf.org/doc/html/rfc3646#section-3)
* @note Only parsed with `dhcpv6_client_dns` module compiled in.
*/
typedef struct __attribute__((packed)) {
network_uint16_t type; /**< @ref DHCPV6_OPT_DNS_RNS */
network_uint16_t len; /**< length of dhcpv6_opt_status_t::dns_rns in byte */
ipv6_addr_t dns_rns[]; /**< addresses of DNS recursive name servers
* in order of preference */
} dhcpv6_opt_dns_rns_t;

/**
* @brief DHCPv6 identity association for prefix delegation option (IA_PD)
* format
Expand Down Expand Up @@ -224,6 +237,17 @@ typedef struct __attribute__((packed)) {
char mud_string[]; /**< MUD URL using the "https" scheme */
} dhcpv6_opt_mud_url_t;

/**
* @brief Configures a DNS recursive name server provided by the server.
*
* @note Only available with module `dhcpv6_client_dns`.
*
* @param[in] opt A legal DNS recursive name option.
* @param[in] netif Network interface the message carrying @p opt came in.
*/
void dhcpv6_client_dns_rns_conf(const dhcpv6_opt_dns_rns_t *opt,
uint16_t netif);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions sys/net/application_layer/dhcpv6/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,12 @@ static bool _parse_reply(uint8_t *rep, size_t len)
for (dhcpv6_opt_t *opt = (dhcpv6_opt_t *)(&rep[sizeof(dhcpv6_msg_t)]);
len > 0; len -= _opt_len(opt), opt = _opt_next(opt)) {
switch (byteorder_ntohs(opt->type)) {
#if IS_USED(MODULE_DHCPV6_CLIENT_DNS)
case DHCPV6_OPT_DNS_RNS:
dhcpv6_client_dns_rns_conf((dhcpv6_opt_dns_rns_t *)opt,
remote.netif);
break;
#endif /* IS_USED(MODULE_DHCPV6_CLIENT_DNS) */
case DHCPV6_OPT_IA_PD:
for (unsigned i = 0; i < CONFIG_DHCPV6_CLIENT_PFX_LEASE_MAX; i++) {
dhcpv6_opt_iapfx_t *iapfx = NULL;
Expand Down
46 changes: 46 additions & 0 deletions sys/net/application_layer/dhcpv6/client_dns.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2021 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @{
*
* @file
* @author Martine Lenders <m.lenders@fu-berlin.de>
*/

#include "net/dhcpv6/client.h"
#if IS_USED(MODULE_SOCK_DNS)
#include "net/sock/dns.h"
#endif

#include "_dhcpv6.h"

#define ENABLE_DEBUG 0
#include "debug.h"

static char addr_str[IPV6_ADDR_MAX_STR_LEN];

void dhcpv6_client_dns_rns_conf(const dhcpv6_opt_dns_rns_t *opt, uint16_t netif)
{
if (byteorder_ntohs(opt->len) < sizeof(ipv6_addr_t)) {
DEBUG("dhcpv6_client_dns: no DNS recursive name server provided.\n");
return;
}
#if IS_USED(MODULE_SOCK_DNS) && IS_ACTIVE(SOCK_HAS_IPV6)
DEBUG("Overriding sock_dns_server with %s\n",
ipv6_addr_to_str(addr_str, opt->dns_rns, sizeof(addr_str)));
sock_dns_server.port = SOCK_DNS_PORT;
sock_dns_server.family = AF_INET6;
sock_dns_server.netif = netif;
memcpy(sock_dns_server.addr.ipv6, opt->dns_rns,
sizeof(sock_dns_server.addr.ipv6));
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind this return is that maybe other DNS client implementations could be added after this.

#endif
}

/** @} */