dhcpv6_common.h
Go to the documentation of this file.
1 /**
2  * @file dhcpv6_common.h
3  * @brief Definitions common to DHCPv6 client, server and relay agent
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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 1.9.6
29  **/
30 
31 #ifndef _DHCPV6_COMMON_H
32 #define _DHCPV6_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/ethernet.h"
37 #include "ipv6/ipv6.h"
38 
39 //UDP ports used by DHCPv6 clients and servers
40 #define DHCPV6_CLIENT_PORT 546
41 #define DHCPV6_SERVER_PORT 547
42 
43 //Maximum DHCPv6 message size
44 #define DHCPV6_MAX_MSG_SIZE 1232
45 //Maximum DUID size (128 octets not including the type code)
46 #define DHCPV6_MAX_DUID_SIZE 130
47 
48 //Maximum hop count in a relay-forward message
49 #define DHCPV6_HOP_COUNT_LIMIT 32
50 //Highest server preference value
51 #define DHCPV6_MAX_SERVER_PREFERENCE 255
52 //Infinite lifetime representation
53 #define DHCPV6_INFINITE_TIME 0xFFFFFFFF
54 
55 //C++ guard
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 
61 /**
62  * @brief DUID types
63  **/
64 
65 typedef enum
66 {
71 
72 
73 /**
74  * @brief Hardware types
75  **/
76 
77 typedef enum
78 {
82 
83 
84 /**
85  * @brief DHCPv6 message types
86  **/
87 
88 typedef enum
89 {
104 
105 
106 /**
107  * @brief DHCPv6 option codes
108  **/
109 
110 typedef enum
111 {
138 
139 
140 /**
141  * @brief Status code
142  **/
143 
144 typedef enum
145 {
153 
154 
155 //CodeWarrior or Win32 compiler?
156 #if defined(__CWCC__) || defined(_WIN32)
157  #pragma pack(push, 1)
158 #endif
159 
160 
161 /**
162  * @brief DUID-LLT structure
163  **/
164 
165 typedef __start_packed struct
166 {
167  uint16_t type; //0-1
168  uint16_t hardwareType; //2-3
169  uint32_t time; //4-7
172 
173 
174 /**
175  * @brief DUID-EN structure
176  **/
177 
178 typedef __start_packed struct
179 {
180  uint16_t type; //0-1
181  uint32_t enterpriseNumber; //2-5
182  uint8_t identifier[]; //6
184 
185 
186 /**
187  * @brief DUID-LL structure
188  **/
189 
190 typedef __start_packed struct
191 {
192  uint16_t type; //0-1
193  uint16_t hardwareType; //2-3
194 #if (ETH_SUPPORT == ENABLED)
195  MacAddr linkLayerAddr; //4-9
196 #else
197  Eui64 linkLayerAddr; //4-11
198 #endif
200 
201 
202 /**
203  * @brief DHCPv6 message
204  **/
205 
206 typedef __start_packed struct
207 {
208  uint8_t msgType; //0
209  uint8_t transactionId[3]; //1-3
210  uint8_t options[]; //4
212 
213 
214 /**
215  * @brief DHCPv6 relay agent message
216  **/
217 
218 typedef __start_packed struct
219 {
220  uint8_t msgType; //0
221  uint8_t hopCount; //1
224  uint8_t options[]; //34
226 
227 
228 /**
229  * @brief DHCPv6 option
230  **/
231 
232 typedef __start_packed struct
233 {
234  uint16_t code; //0-1
235  uint16_t length; //2-3
236  uint8_t value[]; //4
238 
239 
240 /**
241  * @brief Identity Association for Non-temporary Addresses option
242  **/
243 
244 typedef __start_packed struct
245 {
246  uint32_t iaId; //0-3
247  uint32_t t1; //4-7
248  uint32_t t2; //8-11
249  uint8_t options[]; //12
251 
252 
253 /**
254  * @brief Identity Association for Temporary Addresses option
255  **/
256 
257 typedef __start_packed struct
258 {
259  uint32_t iaId; //0-3
260  uint8_t options[]; //4
262 
263 
264 /**
265  * @brief IA Address option
266  **/
267 
268 typedef __start_packed struct
269 {
271  uint32_t preferredLifetime; //16-19
272  uint32_t validLifetime; //20-23
273  uint8_t options[]; //24
275 
276 
277 /**
278  * @brief Option Request option
279  **/
280 
281 typedef __start_packed struct
282 {
283  uint16_t requestedOption[1]; //0-1
285 
286 
287 /**
288  * @brief Preference option
289  **/
290 
291 typedef __start_packed struct
292 {
293  uint8_t value; //0
295 
296 
297 /**
298  * @brief Elapsed Time option
299  **/
300 
301 typedef __start_packed struct
302 {
303  uint16_t value; //0-1
305 
306 
307 /**
308  * @brief Authentication option
309  **/
310 
311 typedef __start_packed struct
312 {
313  uint8_t protocol; //0
314  uint8_t algorithm; //1
315  uint8_t rdm; //2
316  uint8_t replayDetection[8]; //3-10
317  uint8_t authInfo[]; //11
319 
320 
321 /**
322  * @brief Server Unicast option
323  **/
324 
325 typedef __start_packed struct
326 {
329 
330 
331 /**
332  * @brief Status Code option
333  **/
334 
335 typedef __start_packed struct
336 {
337  uint16_t statusCode; //0-1
340 
341 
342 /**
343  * @brief Reconfigure Message option
344  **/
345 
346 typedef __start_packed struct
347 {
348  uint8_t msgType; //0
350 
351 
352 /**
353  * @brief DNS Recursive Name Server option
354  **/
355 
356 typedef __start_packed struct
357 {
358  Ipv6Addr address[1]; //0-15
360 
361 
362 /**
363  * @brief Domain Search List option
364  **/
365 
366 typedef __start_packed struct
367 {
368  uint8_t searchList[1]; //0
370 
371 
372 /**
373  * @brief Identity Association for Prefix Delegation Option
374  **/
375 
376 typedef __start_packed struct
377 {
378  uint32_t iaId; //0-3
379  uint32_t t1; //4-7
380  uint32_t t2; //8-11
381  uint8_t options[]; //12
383 
384 
385 /**
386  * @brief IA_PD Prefix option
387  **/
388 
389 typedef __start_packed struct
390 {
391  uint32_t preferredLifetime; //0-3
392  uint32_t validLifetime; //4-7
393  uint8_t prefixLen; //8
394  Ipv6Addr prefix; //9-24
395  uint8_t options[]; //25
397 
398 
399 /**
400  * @brief Fully Qualified Domain Name option
401  **/
402 
403 typedef __start_packed struct
404 {
405 #ifdef _CPU_BIG_ENDIAN
406  uint8_t mbz : 5; //0
407  uint8_t n : 1;
408  uint8_t o : 1;
409  uint8_t s : 1;
410 #else
411  uint8_t s : 1; //0
412  uint8_t o : 1;
413  uint8_t n : 1;
414  uint8_t mbz : 5;
415 #endif
416  uint8_t domainName[]; //1
418 
419 
420 //CodeWarrior or Win32 compiler?
421 #if defined(__CWCC__) || defined(_WIN32)
422  #pragma pack(pop)
423 #endif
424 
425 
426 //DHCPv6 related constants
428 extern const Ipv6Addr DHCPV6_ALL_SERVERS_ADDR;
429 
430 //DHCPv6 related functions
431 Dhcpv6StatusCode dhcpv6GetStatusCode(const uint8_t *options, size_t length);
432 
433 Dhcpv6Option *dhcpv6AddOption(void *message, size_t *messageLen,
434  uint16_t optionCode, const void *optionValue, size_t optionLen);
435 
436 Dhcpv6Option *dhcpv6AddSubOption(Dhcpv6Option *baseOption, size_t *messageLen,
437  uint16_t optionCode, const void *optionValue, size_t optionLen);
438 
439 Dhcpv6Option *dhcpv6GetOption(const uint8_t *options,
440  size_t optionsLength, uint16_t optionCode);
441 
442 int32_t dhcpv6Rand(int32_t value);
443 int32_t dhcpv6RandRange(int32_t min, int32_t max);
444 
445 //C++ guard
446 #ifdef __cplusplus
447 }
448 #endif
449 
450 #endif
IPv6 (Internet Protocol Version 6)
uint8_t value[]
Ipv6Addr linkAddress
__start_packed struct @130 Dhcpv6Option
DHCPv6 option.
@ DHCPV6_MSG_TYPE_DECLINE
Definition: dhcpv6_common.h:98
__start_packed struct @125 Dhcpv6DuidLlt
DUID-LLT structure.
@ DHCPV6_DUID_LLT
Definition: dhcpv6_common.h:67
@ DHCPV6_STATUS_NOT_ON_LINK
uint8_t options[]
__start_packed struct @109 Eui64
EUI-64 identifier.
@ DHCPV6_MSG_TYPE_SOLICIT
Definition: dhcpv6_common.h:90
uint8_t algorithm
uint8_t o
@ DHCPV6_OPTION_VENDOR_OPTS
uint32_t preferredLifetime
Ipv6Addr peerAddress
Dhcpv6Option * dhcpv6AddSubOption(Dhcpv6Option *baseOption, size_t *messageLen, uint16_t optionCode, const void *optionValue, size_t optionLen)
Add a suboption under an existing base option.
@ DHCPV6_MSG_TYPE_REBIND
Definition: dhcpv6_common.h:95
@ DHCPV6_MSG_TYPE_RELAY_FORW
@ DHCPV6_DUID_EN
Definition: dhcpv6_common.h:68
@ DHCPV6_STATUS_UNSPEC_FAILURE
uint8_t domainName[]
uint16_t type
@ DHCPV6_OPTION_CAPTIVE_PORTAL
@ DHCPV6_OPTION_STATUS_CODE
__start_packed struct @140 Dhcpv6ReconfMessageOption
Reconfigure Message option.
__start_packed struct @127 Dhcpv6DuidLl
DUID-LL structure.
@ DHCPV6_OPTION_IAPREFIX
int32_t dhcpv6RandRange(int32_t min, int32_t max)
Get a random value in the specified range.
Ipv6Addr prefix
@ DHCPV6_OPTION_DNS_SERVERS
__start_packed struct @129 Dhcpv6RelayMessage
DHCPv6 relay agent message.
@ DHCPV6_HARDWARE_TYPE_EUI64
Definition: dhcpv6_common.h:80
uint32_t iaId
@ DHCPV6_OPTION_ORO
Ethernet.
int32_t dhcpv6Rand(int32_t value)
Multiplication by a randomization factor.
uint8_t msgType
__start_packed struct @144 Dhcpv6IaPrefixOption
IA_PD Prefix option.
__start_packed struct @136 Dhcpv6ElapsedTimeOption
Elapsed Time option.
MacAddr linkLayerAddr
@ DHCPV6_STATUS_NO_BINDING
uint16_t requestedOption[1]
@ DHCPV6_STATUS_SUCCESS
Dhcpv6StatusCode dhcpv6GetStatusCode(const uint8_t *options, size_t length)
Retrieve status code.
Definition: dhcpv6_common.c:70
__start_packed struct @138 Dhcpv6ServerUnicastOption
Server Unicast option.
__start_packed struct @139 Dhcpv6StatusCodeOption
Status Code option.
uint32_t enterpriseNumber
uint8_t protocol
@ DHCPV6_OPTION_IAADDR
uint16_t statusCode
uint8_t identifier[]
__start_packed struct @143 Dhcpv6IaPdOption
Identity Association for Prefix Delegation Option.
@ DHCPV6_MSG_TYPE_RELEASE
Definition: dhcpv6_common.h:97
__start_packed struct @142 Dhcpv6DomainListOption
Domain Search List option.
uint8_t searchList[1]
__start_packed struct _Ipv4Header __end_packed
@ DHCPV6_MSG_TYPE_REPLY
Definition: dhcpv6_common.h:96
__start_packed struct @128 Dhcpv6Message
DHCPv6 message.
@ DHCPV6_OPTION_ELAPSED_TIME
@ DHCPV6_OPTION_AUTH
@ DHCPV6_OPTION_DOMAIN_LIST
__start_packed struct @179 Ipv6Addr
IPv6 network address.
@ DHCPV6_MSG_TYPE_RENEW
Definition: dhcpv6_common.h:94
@ DHCPV6_OPTION_IA_NA
Dhcpv6StatusCode
Status code.
uint32_t t2
const Ipv6Addr DHCPV6_ALL_SERVERS_ADDR
Definition: dhcpv6_common.c:55
uint16_t code
__start_packed struct @126 Dhcpv6DuidEn
DUID-EN structure.
@ DHCPV6_MSG_TYPE_ADVERTISE
Definition: dhcpv6_common.h:91
uint8_t replayDetection[8]
uint8_t hopCount
uint8_t transactionId[3]
@ DHCPV6_OPTION_RECONF_MSG
uint8_t authInfo[]
const Ipv6Addr DHCPV6_ALL_RELAY_AGENTS_AND_SERVERS_ADDR
Definition: dhcpv6_common.c:51
__start_packed struct @134 Dhcpv6OroOption
Option Request option.
@ DHCPV6_MSG_TYPE_INFO_REQUEST
@ DHCPV6_MSG_TYPE_RELAY_REPL
Dhcpv6DuidType
DUID types.
Definition: dhcpv6_common.h:65
__start_packed struct @131 Dhcpv6IaNaOption
Identity Association for Non-temporary Addresses option.
__start_packed struct @145 Dhcpv6FqdnOption
Fully Qualified Domain Name option.
@ DHCPV6_OPTION_IA_PD
__start_packed struct @132 Dhcpv6IaTaOption
Identity Association for Temporary Addresses option.
uint8_t prefixLen
char char_t
Definition: compiler_port.h:43
uint32_t time
uint32_t t1
uint16_t hardwareType
@ DHCPV6_OPTION_UNICAST
Dhcpv6MessageType
DHCPv6 message types.
Definition: dhcpv6_common.h:88
uint8_t n
@ DHCPV6_STATUS_USE_MULTICAST
@ DHCPV6_OPTION_SERVERID
__start_packed struct @133 Dhcpv6IaAddrOption
IA Address option.
@ DHCPV6_OPTION_IA_TA
@ DHCPV6_STATUS_NO_ADDRS_AVAILABLE
Ipv6Addr serverAddr
@ DHCPV6_OPTION_RELAY_MSG
Dhcpv6HardwareType
Hardware types.
Definition: dhcpv6_common.h:77
uint8_t s
uint8_t message[]
Definition: chap.h:152
char_t statusMessage[]
@ DHCPV6_MSG_TYPE_CONFIRM
Definition: dhcpv6_common.h:93
Dhcpv6Option * dhcpv6GetOption(const uint8_t *options, size_t optionsLength, uint16_t optionCode)
Find the specified option in a DHCPv6 message.
Ipv6Addr address
@ DHCPV6_HARDWARE_TYPE_ETH
Definition: dhcpv6_common.h:79
uint32_t validLifetime
uint8_t mbz
Dhcpv6OptionCode
DHCPv6 option codes.
uint8_t rdm
@ DHCPV6_OPTION_RECONF_ACCEPT
@ DHCPV6_OPTION_FQDN
@ DHCPV6_MSG_TYPE_RECONFIGURE
Definition: dhcpv6_common.h:99
@ DHCPV6_OPTION_USER_CLASS
__start_packed struct @141 Dhcpv6DnsServersOption
DNS Recursive Name Server option.
uint16_t length
Dhcpv6Option * dhcpv6AddOption(void *message, size_t *messageLen, uint16_t optionCode, const void *optionValue, size_t optionLen)
Add an option to a DHCPv6 message.
TCP/IP stack core.
@ DHCPV6_OPTION_CLIENTID
@ DHCPV6_OPTION_INTERFACE_ID
__start_packed struct @135 Dhcpv6PreferenceOption
Preference option.
@ DHCPV6_DUID_LL
Definition: dhcpv6_common.h:69
__start_packed struct @137 Dhcpv6AuthOption
Authentication option.
@ DHCPV6_OPTION_RAPID_COMMIT
@ DHCPV6_OPTION_PREFERENCE
@ DHCPV6_OPTION_VENDOR_CLASS
@ DHCPV6_MSG_TYPE_REQUEST
Definition: dhcpv6_common.h:92
__start_packed struct @108 MacAddr
MAC address.