dhcpv6_client.h
Go to the documentation of this file.
1 /**
2  * @file dhcpv6_client.h
3  * @brief DHCPv6 client (Dynamic Host Configuration Protocol for IPv6)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _DHCPV6_CLIENT_H
32 #define _DHCPV6_CLIENT_H
33 
34 //Dependencies
35 #include "dhcpv6/dhcpv6_common.h"
36 #include "core/socket.h"
37 
38 //DHCPv6 client support
39 #ifndef DHCPV6_CLIENT_SUPPORT
40  #define DHCPV6_CLIENT_SUPPORT DISABLED
41 #elif (DHCPV6_CLIENT_SUPPORT != ENABLED && DHCPV6_CLIENT_SUPPORT != DISABLED)
42  #error DHCPV6_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //DHCPv6 client tick interval
46 #ifndef DHCPV6_CLIENT_TICK_INTERVAL
47  #define DHCPV6_CLIENT_TICK_INTERVAL 200
48 #elif (DHCPV6_CLIENT_TICK_INTERVAL < 10)
49  #error DHCPV6_CLIENT_TICK_INTERVAL parameter is not valid
50 #endif
51 
52 //Maximum number of IPv6 addresses in the client's IA
53 #ifndef DHCPV6_CLIENT_ADDR_LIST_SIZE
54  #define DHCPV6_CLIENT_ADDR_LIST_SIZE 2
55 #elif (DHCPV6_CLIENT_ADDR_LIST_SIZE < 1)
56  #error DHCPV6_CLIENT_ADDR_LIST_SIZE parameter is not valid
57 #endif
58 
59 //Max delay of first Solicit
60 #ifndef DHCPV6_CLIENT_SOL_MAX_DELAY
61  #define DHCPV6_CLIENT_SOL_MAX_DELAY 1000
62 #elif (DHCPV6_CLIENT_SOL_MAX_DELAY < 100)
63  #error DHCPV6_CLIENT_SOL_MAX_DELAY parameter is not valid
64 #endif
65 
66 //Initial Solicit timeout
67 #ifndef DHCPV6_CLIENT_SOL_TIMEOUT
68  #define DHCPV6_CLIENT_SOL_TIMEOUT 1000
69 #elif (DHCPV6_CLIENT_SOL_TIMEOUT < 100)
70  #error DHCPV6_CLIENT_SOL_TIMEOUT parameter is not valid
71 #endif
72 
73 //Max Solicit timeout value
74 #ifndef DHCPV6_CLIENT_SOL_MAX_RT
75  #define DHCPV6_CLIENT_SOL_MAX_RT 120000
76 #elif (DHCPV6_CLIENT_SOL_MAX_RT < 100)
77  #error DHCPV6_CLIENT_SOL_MAX_RT parameter is not valid
78 #endif
79 
80 //Initial Request timeout
81 #ifndef DHCPV6_CLIENT_REQ_TIMEOUT
82  #define DHCPV6_CLIENT_REQ_TIMEOUT 1000
83 #elif (DHCPV6_CLIENT_REQ_TIMEOUT < 100)
84  #error DHCPV6_CLIENT_REQ_TIMEOUT parameter is not valid
85 #endif
86 
87 //Max Request timeout value
88 #ifndef DHCPV6_CLIENT_REQ_MAX_RT
89  #define DHCPV6_CLIENT_REQ_MAX_RT 30000
90 #elif (DHCPV6_CLIENT_REQ_MAX_RT < 100)
91  #error DHCPV6_CLIENT_REQ_MAX_RT parameter is not valid
92 #endif
93 
94 //Max Request retry attempts
95 #ifndef DHCPV6_CLIENT_REQ_MAX_RC
96  #define DHCPV6_CLIENT_REQ_MAX_RC 10
97 #elif (DHCPV6_CLIENT_REQ_MAX_RC < 1)
98  #error DHCPV6_CLIENT_REQ_MAX_RC parameter is not valid
99 #endif
100 
101 //Max delay of first Confirm
102 #ifndef DHCPV6_CLIENT_CNF_MAX_DELAY
103  #define DHCPV6_CLIENT_CNF_MAX_DELAY 1000
104 #elif (DHCPV6_CLIENT_CNF_MAX_DELAY < 100)
105  #error DHCPV6_CLIENT_CNF_MAX_DELAY parameter is not valid
106 #endif
107 
108 //Initial Confirm timeout
109 #ifndef DHCPV6_CLIENT_CNF_TIMEOUT
110  #define DHCPV6_CLIENT_CNF_TIMEOUT 1000
111 #elif (DHCPV6_CLIENT_CNF_TIMEOUT < 100)
112  #error DHCPV6_CLIENT_CNF_TIMEOUT parameter is not valid
113 #endif
114 
115 //Max Confirm timeout
116 #ifndef DHCPV6_CLIENT_CNF_MAX_RT
117  #define DHCPV6_CLIENT_CNF_MAX_RT 4000
118 #elif (DHCPV6_CLIENT_CNF_MAX_RT < 100)
119  #error DHCPV6_CLIENT_CNF_MAX_RT parameter is not valid
120 #endif
121 
122 //Max Confirm duration
123 #ifndef DHCPV6_CLIENT_CNF_MAX_RD
124  #define DHCPV6_CLIENT_CNF_MAX_RD 10000
125 #elif (DHCPV6_CLIENT_CNF_MAX_RD < 100)
126  #error DHCPV6_CLIENT_CNF_MAX_RD parameter is not valid
127 #endif
128 
129 //Initial Renew timeout
130 #ifndef DHCPV6_CLIENT_REN_TIMEOUT
131  #define DHCPV6_CLIENT_REN_TIMEOUT 10000
132 #elif (DHCPV6_CLIENT_REN_TIMEOUT < 100)
133  #error DHCPV6_CLIENT_REN_TIMEOUT parameter is not valid
134 #endif
135 
136 //Max Renew timeout value
137 #ifndef DHCPV6_CLIENT_REN_MAX_RT
138  #define DHCPV6_CLIENT_REN_MAX_RT 600000
139 #elif (DHCPV6_CLIENT_REN_MAX_RT < 100)
140  #error DHCPV6_CLIENT_REN_MAX_RT parameter is not valid
141 #endif
142 
143 //Initial Rebind timeout
144 #ifndef DHCPV6_CLIENT_REB_TIMEOUT
145  #define DHCPV6_CLIENT_REB_TIMEOUT 10000
146 #elif (DHCPV6_CLIENT_REB_TIMEOUT < 100)
147  #error DHCPV6_CLIENT_REB_TIMEOUT parameter is not valid
148 #endif
149 
150 //Max Rebind timeout value
151 #ifndef DHCPV6_CLIENT_REB_MAX_RT
152  #define DHCPV6_CLIENT_REB_MAX_RT 600000
153 #elif (DHCPV6_CLIENT_REB_MAX_RT < 100)
154  #error DHCPV6_CLIENT_REB_MAX_RT parameter is not valid
155 #endif
156 
157 //Max delay of first Information-request
158 #ifndef DHCPV6_CLIENT_INF_MAX_DELAY
159  #define DHCPV6_CLIENT_INF_MAX_DELAY 1000
160 #elif (DHCPV6_CLIENT_INF_MAX_DELAY < 100)
161  #error DHCPV6_CLIENT_INF_MAX_DELAY parameter is not valid
162 #endif
163 
164 //Initial Information-request timeout
165 #ifndef DHCPV6_CLIENT_INF_TIMEOUT
166  #define DHCPV6_CLIENT_INF_TIMEOUT 1000
167 #elif (DHCPV6_CLIENT_INF_TIMEOUT < 100)
168  #error DHCPV6_CLIENT_INF_TIMEOUT parameter is not valid
169 #endif
170 
171 //Max Information-request timeout value
172 #ifndef DHCPV6_CLIENT_INF_MAX_RT
173  #define DHCPV6_CLIENT_INF_MAX_RT 120000
174 #elif (DHCPV6_CLIENT_INF_MAX_RT < 1000)
175  #error DHCPV6_CLIENT_INF_MAX_RT parameter is not valid
176 #endif
177 
178 //Initial Release timeout
179 #ifndef DHCPV6_CLIENT_REL_TIMEOUT
180  #define DHCPV6_CLIENT_REL_TIMEOUT 1000
181 #elif (DHCPV6_CLIENT_REL_TIMEOUT < 100)
182  #error DHCPV6_CLIENT_REL_TIMEOUT parameter is not valid
183 #endif
184 
185 //Max Release attempts
186 #ifndef DHCPV6_CLIENT_REL_MAX_RC
187  #define DHCPV6_CLIENT_REL_MAX_RC 5
188 #elif (DHCPV6_CLIENT_REL_MAX_RC < 1)
189  #error DHCPV6_CLIENT_REL_MAX_RC parameter is not valid
190 #endif
191 
192 //Initial Decline timeout
193 #ifndef DHCPV6_CLIENT_DEC_TIMEOUT
194  #define DHCPV6_CLIENT_DEC_TIMEOUT 1000
195 #elif (DHCPV6_CLIENT_DEC_TIMEOUT < 100)
196  #error DHCPV6_CLIENT_DEC_TIMEOUT parameter is not valid
197 #endif
198 
199 //Max Decline attempts
200 #ifndef DHCPV6_CLIENT_DEC_MAX_RC
201  #define DHCPV6_CLIENT_DEC_MAX_RC 5
202 #elif (DHCPV6_CLIENT_DEC_MAX_RC < 1)
203  #error DHCPV6_CLIENT_DEC_MAX_RC parameter is not valid
204 #endif
205 
206 //Initial Reconfigure timeout
207 #ifndef DHCPV6_CLIENT_REC_TIMEOUT
208  #define DHCPV6_CLIENT_REC_TIMEOUT 2000
209 #elif (DHCPV6_CLIENT_REC_TIMEOUT < 100)
210  #error DHCPV6_CLIENT_REC_TIMEOUT parameter is not valid
211 #endif
212 
213 //Max Reconfigure attempts
214 #ifndef DHCPV6_CLIENT_REC_MAX_RC
215  #define DHCPV6_CLIENT_REC_MAX_RC 8
216 #elif (DHCPV6_CLIENT_REC_MAX_RC < 1)
217  #error DHCPV6_CLIENT_REC_MAX_RC parameter is not valid
218 #endif
219 
220 //Application specific context
221 #ifndef DHCPV6_CLIENT_PRIVATE_CONTEXT
222  #define DHCPV6_CLIENT_PRIVATE_CONTEXT
223 #endif
224 
225 //Forward declaration of Dhcpv6ClientContext structure
226 struct _Dhcpv6ClientContext;
227 #define Dhcpv6ClientContext struct _Dhcpv6ClientContext
228 
229 //C++ guard
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233 
234 
235 /**
236  * @brief DHCPv6 client FSM states
237  **/
238 
239 typedef enum
240 {
253 
254 
255 /**
256  * @brief DHCPv6 configuration timeout callback
257  **/
258 
260  NetInterface *interface);
261 
262 
263 /**
264  * @brief Link state change callback
265  **/
266 
268  NetInterface *interface, bool_t linkState);
269 
270 
271 /**
272  * @brief FSM state change callback
273  **/
274 
276  NetInterface *interface, Dhcpv6State state);
277 
278 
279 /**
280  * @brief Add DHCPv6 options callback
281  **/
282 
284  Dhcpv6Message *message, size_t *length);
285 
286 
287 /**
288  * @brief Parse DHCPv6 options callback
289  **/
290 
292  const Dhcpv6Message *message, size_t length);
293 
294 
295 /**
296  * @brief DHCPv6 client settings
297  **/
298 
299 typedef struct
300 {
301  NetInterface *interface; ///<Network interface to configure
302  bool_t rapidCommit; ///<Quick configuration using rapid commit
303  bool_t manualDnsConfig; ///<Force manual DNS configuration
304  systime_t timeout; ///<DHCPv6 configuration timeout
305  Dhcpv6TimeoutCallback timeoutEvent; ///<DHCPv6 configuration timeout event
306  Dhcpv6LinkChangeCallback linkChangeEvent; ///<Link state change event
307  Dhcpv6StateChangeCallback stateChangeEvent; ///<FSM state change event
308  Dhcpv6AddOptionsCallback addOptionsCallback; ///<Add DHCPv6 options callback
309  Dhcpv6ParseOptionsCallback parseOptionsCallback; ///<Parse DHCPv6 options callback
311 
312 
313 /**
314  * @brief IA address entry
315  **/
316 
317 typedef struct
318 {
319  Ipv6Addr addr; ///<IPv6 address
320  uint32_t validLifetime; ///<Valid lifetime
321  uint32_t preferredLifetime; ///<Preferred lifetime
323 
324 
325 /**
326  * @brief Client's IA (Identity Association)
327  **/
328 
329 typedef struct
330 {
331  uint32_t t1; ///<T1 parameter
332  uint32_t t2; ///<T2 parameter
333  Dhcpv6ClientAddrEntry addrList[DHCPV6_CLIENT_ADDR_LIST_SIZE]; ///<Set of IPv6 addresses
335 
336 
337 /**
338  * @brief DHCPv6 client context
339  **/
340 
342 {
343  Dhcpv6ClientSettings settings; ///<DHCPv6 client settings
344  bool_t running; ///<This flag tells whether the DHCP client is running or not
345  Dhcpv6State state; ///<Current state of the FSM
346  bool_t timeoutEventDone; ///<Timeout callback function has been called
347  systime_t timestamp; ///<Timestamp to manage retransmissions
348  systime_t timeout; ///<Timeout value
349  uint_t retransmitCount; ///<Retransmission counter
350  uint8_t clientId[DHCPV6_MAX_DUID_SIZE]; ///<Client DUID
351  size_t clientIdLen; ///<Length of the client DUID
352  uint8_t serverId[DHCPV6_MAX_DUID_SIZE]; ///<Server DUID
353  size_t serverIdLen; ///<Length of the server DUID
354  int_t serverPreference; ///<Preference value for the server
355  uint32_t transactionId; ///<Value to match requests with replies
356  systime_t configStartTime; ///<Address acquisition or renewal process start time
357  systime_t exchangeStartTime; ///<Time at which the client sent the first message
358  systime_t leaseStartTime; ///<Lease start time
359  Dhcpv6ClientIa ia; ///<Identity association
360  DHCPV6_CLIENT_PRIVATE_CONTEXT ///<Application specific context
361 };
362 
363 
364 //DHCPv6 client related functions
366 
368  const Dhcpv6ClientSettings *settings);
369 
372 
375 
376 //C++ guard
377 #ifdef __cplusplus
378 }
379 #endif
380 
381 #endif
uint8_t message[]
Definition: chap.h:154
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
error_t dhcpv6ClientRelease(Dhcpv6ClientContext *context)
Release DHCPv6 lease.
error_t dhcpv6ClientStop(Dhcpv6ClientContext *context)
Stop DHCPv6 client.
void(* Dhcpv6StateChangeCallback)(Dhcpv6ClientContext *context, NetInterface *interface, Dhcpv6State state)
FSM state change callback.
void dhcpv6ClientGetDefaultSettings(Dhcpv6ClientSettings *settings)
Initialize settings with default values.
Definition: dhcpv6_client.c:59
#define DHCPV6_CLIENT_PRIVATE_CONTEXT
#define DHCPV6_CLIENT_ADDR_LIST_SIZE
Definition: dhcpv6_client.h:54
Dhcpv6State
DHCPv6 client FSM states.
@ DHCPV6_STATE_INIT
@ DHCPV6_STATE_DECLINE
@ DHCPV6_STATE_RENEW
@ DHCPV6_STATE_DAD
@ DHCPV6_STATE_BOUND
@ DHCPV6_STATE_REQUEST
@ DHCPV6_STATE_RELEASE
@ DHCPV6_STATE_INIT_CONFIRM
@ DHCPV6_STATE_SOLICIT
@ DHCPV6_STATE_CONFIRM
@ DHCPV6_STATE_REBIND
void(* Dhcpv6AddOptionsCallback)(Dhcpv6ClientContext *context, Dhcpv6Message *message, size_t *length)
Add DHCPv6 options callback.
Dhcpv6State dhcpv6ClientGetState(Dhcpv6ClientContext *context)
Retrieve current state.
#define Dhcpv6ClientContext
error_t dhcpv6ClientStart(Dhcpv6ClientContext *context)
Start DHCPv6 client.
void(* Dhcpv6ParseOptionsCallback)(Dhcpv6ClientContext *context, const Dhcpv6Message *message, size_t length)
Parse DHCPv6 options callback.
void(* Dhcpv6LinkChangeCallback)(Dhcpv6ClientContext *context, NetInterface *interface, bool_t linkState)
Link state change callback.
error_t dhcpv6ClientInit(Dhcpv6ClientContext *context, const Dhcpv6ClientSettings *settings)
DHCPv6 client initialization.
Definition: dhcpv6_client.c:92
void(* Dhcpv6TimeoutCallback)(Dhcpv6ClientContext *context, NetInterface *interface)
DHCPv6 configuration timeout callback.
Definitions common to DHCPv6 client, server and relay agent.
Dhcpv6Message
#define DHCPV6_MAX_DUID_SIZE
Definition: dhcpv6_common.h:46
error_t
Error codes.
Definition: error.h:43
Ipv6Addr
Definition: ipv6.h:251
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
Socket API.
DHCPv6 client context.
bool_t timeoutEventDone
Timeout callback function has been called.
uint8_t clientId[DHCPV6_MAX_DUID_SIZE]
Client DUID.
systime_t timestamp
Timestamp to manage retransmissions.
uint8_t serverId[DHCPV6_MAX_DUID_SIZE]
Server DUID.
uint32_t transactionId
Value to match requests with replies.
size_t clientIdLen
Length of the client DUID.
systime_t leaseStartTime
Lease start time.
bool_t running
This flag tells whether the DHCP client is running or not.
uint_t retransmitCount
Retransmission counter.
int_t serverPreference
Preference value for the server.
Dhcpv6State state
Current state of the FSM.
systime_t timeout
Timeout value.
systime_t exchangeStartTime
Time at which the client sent the first message.
Dhcpv6ClientIa ia
Identity association.
systime_t configStartTime
Address acquisition or renewal process start time.
size_t serverIdLen
Length of the server DUID.
Dhcpv6ClientSettings settings
DHCPv6 client settings.
IA address entry.
uint32_t preferredLifetime
Preferred lifetime.
uint32_t validLifetime
Valid lifetime.
Ipv6Addr addr
IPv6 address.
Client's IA (Identity Association)
uint32_t t1
T1 parameter.
uint32_t t2
T2 parameter.
DHCPv6 client settings.
bool_t rapidCommit
Quick configuration using rapid commit.
Dhcpv6ParseOptionsCallback parseOptionsCallback
Parse DHCPv6 options callback.
bool_t manualDnsConfig
Force manual DNS configuration.
Dhcpv6TimeoutCallback timeoutEvent
DHCPv6 configuration timeout event.
Dhcpv6StateChangeCallback stateChangeEvent
FSM state change event.
Dhcpv6AddOptionsCallback addOptionsCallback
Add DHCPv6 options callback.
systime_t timeout
DHCPv6 configuration timeout.
Dhcpv6LinkChangeCallback linkChangeEvent
Link state change event.
NetInterface * interface
Network interface to configure.
uint8_t length
Definition: tcp.h:368