dhcpv6_relay.c
Go to the documentation of this file.
1 /**
2  * @file dhcpv6_relay.c
3  * @brief DHCPv6 relay agent (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-2026 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  * @section Description
28  *
29  * DHCPv6 Relay-Agents are deployed to forward DHCPv6 messages between clients
30  * and servers when they are not on the same IPv6 link and are often implemented
31  * alongside a routing function in a common node. Refer to RFC 8415
32  *
33  * @author Oryx Embedded SARL (www.oryx-embedded.com)
34  * @version 2.6.0
35  **/
36 
37 //Switch to the appropriate trace level
38 #define TRACE_LEVEL DHCPV6_TRACE_LEVEL
39 
40 //Dependencies
41 #include "core/net.h"
42 #include "dhcpv6/dhcpv6_relay.h"
44 #include "debug.h"
45 
46 //Check TCP/IP stack configuration
47 #if (IPV6_SUPPORT == ENABLED && DHCPV6_RELAY_SUPPORT == ENABLED)
48 
49 
50 /**
51  * @brief Initialize settings with default values
52  * @param[out] settings Structure that contains DHCPv6 relay agent settings
53  **/
54 
56 {
57  uint_t i;
58 
59  //Default task parameters
60  settings->task = OS_TASK_DEFAULT_PARAMS;
63 
64  //Network-facing interface
65  settings->serverInterface = NULL;
66 
67  //Number of client-facing interfaces
68  settings->numClientInterfaces = 0;
69 
70  //Client-facing interfaces
71  for(i = 0; i < DHCPV6_RELAY_MAX_CLIENT_INTERFACES; i++)
72  {
73  settings->clientInterfaces[i] = NULL;
74  }
75 
76  //If the relay agent has not been explicitly configured, it must use the
77  //All_DHCP_Servers multicast address as the default (refer to RFC 8415,
78  //section 19)
80 }
81 
82 
83 /**
84  * @brief DHCPv6 relay agent initialization
85  * @param[in] context Pointer to the DHCPv6 client context
86  * @param[in] settings DHCPv6 relay agent specific settings
87  * @return Error code
88  **/
89 
91  const Dhcpv6RelaySettings *settings)
92 {
93  error_t error;
94  uint_t i;
95 
96  //Debug message
97  TRACE_INFO("Initializing DHCPv6 relay agent...\r\n");
98 
99  //Ensure the parameters are valid
100  if(context == NULL || settings == NULL)
102 
103  //Invalid network-facing interface?
104  if(settings->serverInterface == NULL)
106 
107  //Invalid number of client-facing interfaces
108  if(settings->numClientInterfaces < 1 ||
110  {
112  }
113 
114  //Loop through the client-facing interfaces
115  for(i = 0; i < settings->numClientInterfaces; i++)
116  {
117  //Invalid client-facing interface?
118  if(settings->clientInterfaces[i] == NULL)
120  }
121 
122  //Check the address to be used when forwarding messages to the server
124  return ERROR_INVALID_ADDRESS;
125 
126  //Initialize status code
127  error = NO_ERROR;
128 
129  //Clear the DHCPv6 relay agent context
130  osMemset(context, 0, sizeof(Dhcpv6RelayContext));
131 
132  //Initialize task parameters
133  context->taskParams = settings->task;
134  context->taskId = OS_INVALID_TASK_ID;
135 
136  //Attach TCP/IP stack context
137  context->netContext = settings->serverInterface->netContext;
138 
139  //Save user settings
140  context->serverInterface = settings->serverInterface;
141  context->numClientInterfaces = settings->numClientInterfaces;
142  context->serverIpAddr = settings->serverIpAddr;
143 
144  //Save client-facing interfaces
145  for(i = 0; i < context->numClientInterfaces; i++)
146  {
147  context->clientInterfaces[i] = settings->clientInterfaces[i];
148  }
149 
150  //Create an event object to poll the state of sockets
151  if(!osCreateEvent(&context->event))
152  {
153  //Failed to create event
154  error = ERROR_OUT_OF_RESOURCES;
155  }
156 
157  //Any error to report?
158  if(error)
159  {
160  //Clean up side effects
161  dhcpv6RelayDeinit(context);
162  }
163 
164  //Return status code
165  return error;
166 }
167 
168 
169 /**
170  * @brief Start DHCPv6 relay agent
171  * @param[in] context Pointer to the DHCPv6 relay agent context
172  * @return Error code
173  **/
174 
176 {
177  error_t error;
178  uint_t i;
179 
180  //Make sure the DHCPv6 relay agent context is valid
181  if(context == NULL)
183 
184  //Debug message
185  TRACE_INFO("Starting DHCPv6 relay agent...\r\n");
186 
187  //Make sure the DHCPv6 relay agent is not already running
188  if(context->running)
189  return ERROR_ALREADY_RUNNING;
190 
191  //Start of exception handling block
192  do
193  {
194  //Open the socket that carries traffic towards the DHCPv6 server
195  error = dhcpv6RelayOpenServerSocket(context);
196  //Any error to report?
197  if(error)
198  break;
199 
200  //Open the sockets that carry traffic towards the DHCPv6 clients
201  for(i = 0; i < context->numClientInterfaces && !error; i++)
202  {
203  error = dhcpv6RelayOpenClientSocket(context, i);
204  }
205 
206  //Propagate exception if necessary
207  if(error)
208  break;
209 
210  //Start the DHCPv6 relay agent
211  context->stop = FALSE;
212  context->running = TRUE;
213 
214  //Create a task
215  context->taskId = osCreateTask("DHCPv6 Relay",
216  (OsTaskCode) dhcpv6RelayTask, context, &context->taskParams);
217 
218  //Failed to create task?
219  if(context->taskId == OS_INVALID_TASK_ID)
220  {
221  //Report an error
222  error = ERROR_OUT_OF_RESOURCES;
223  break;
224  }
225 
226  //End of exception handling block
227  } while(0);
228 
229  //Any error to report?
230  if(error)
231  {
232  //Clean up side effects
233  context->running = FALSE;
234 
235  //Close the socket associated with the network-facing interface
236  socketClose(context->serverSocket);
237  context->serverSocket = NULL;
238 
239  //Close the socket associated with client-facing interfaces
240  for(i = 0; i < context->numClientInterfaces; i++)
241  {
242  socketClose(context->clientSockets[i]);
243  context->clientSockets[i] = NULL;
244  }
245  }
246 
247  //Return status code
248  return error;
249 }
250 
251 
252 /**
253  * @brief Stop DHCPv6 relay agent
254  * @param[in] context Pointer to the DHCPv6 relay agent context
255  * @return Error code
256  **/
257 
259 {
260  uint_t i;
261 
262  //Make sure the DHCPv6 relay agent context is valid
263  if(context == NULL)
265 
266  //Debug message
267  TRACE_INFO("Stopping DHCPv6 relay agent...\r\n");
268 
269  //Check whether the DHCPv6 relay agent is running
270  if(context->running)
271  {
272 #if (NET_RTOS_SUPPORT == ENABLED)
273  //Stop the DHCPv6 relay agent
274  context->stop = TRUE;
275  //Send a signal to the task to abort any blocking operation
276  osSetEvent(&context->event);
277 
278  //Wait for the task to terminate
279  while(context->running)
280  {
281  osDelayTask(1);
282  }
283 #endif
284 
285  //Close the socket that carries traffic towards the DHCPv6 server
286  socketClose(context->serverSocket);
287  context->serverSocket = NULL;
288 
289  //Close the sockets that carry traffic towards the DHCPv6 clients
290  for(i = 0; i < context->numClientInterfaces; i++)
291  {
292  socketClose(context->clientSockets[i]);
293  context->clientSockets[i] = NULL;
294  }
295  }
296 
297  //Successful processing
298  return NO_ERROR;
299 }
300 
301 
302 /**
303  * @brief DHCPv6 relay agent task
304  * @param[in] context Pointer to the DHCPv6 relay agent context
305  **/
306 
308 {
309  error_t error;
310  uint_t i;
311 
312  //Task prologue
313  osEnterTask();
314 
315  //Specify the events the application is interested in for each client-facing
316  //sockets
317  for(i = 0; i < context->numClientInterfaces; i++)
318  {
319  context->eventDesc[i].socket = context->clientSockets[i];
321  }
322 
323  //Specify the events the application is interested in for the network-facing
324  //socket
325  context->eventDesc[i].socket = context->serverSocket;
327 
328  //Main loop
329  while(1)
330  {
331  //Wait for incoming packets on network-facing or client-facing interfaces
332  error = socketPoll(context->eventDesc, context->numClientInterfaces + 1,
333  &context->event, INFINITE_DELAY);
334 
335  //Check status code
336  if(error == NO_ERROR || error == ERROR_TIMEOUT ||
337  error == ERROR_WAIT_CANCELED)
338  {
339  //Stop request?
340  if(context->stop)
341  {
342  //Stop DHCPv6 relay agent operation
343  context->running = FALSE;
344  //Task epilogue
345  osExitTask();
346  //Kill ourselves
348  }
349 
350  //Loop through client-facing interfaces
351  for(i = 0; i < context->numClientInterfaces; i++)
352  {
353  //Check the state of each client-facing socket
354  if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY)
355  {
356  //Relay messages from the clients
357  dhcpv6ForwardClientMessage(context, i);
358  }
359  }
360 
361  //Check the state of the network-facing socket
362  if(context->eventDesc[i].eventFlags & SOCKET_EVENT_RX_READY)
363  {
364  //Forward Relay-Reply messages from the network
366  }
367  }
368  }
369 }
370 
371 
372 /**
373  * @brief Release DHCP relay agent context
374  * @param[in] context Pointer to the DHCP relay agent context
375  **/
376 
378 {
379  //Make sure the DHCP relay agent context is valid
380  if(context != NULL)
381  {
382  //Free previously allocated resources
383  osDeleteEvent(&context->event);
384 
385  //Clear DHCP relay agent context
386  osMemset(context, 0, sizeof(Dhcpv6RelayContext));
387  }
388 }
389 
390 #endif
bool_t running
Operational state of the DHCPv6 relay agent.
Definition: dhcpv6_relay.h:102
#define DHCPV6_RELAY_MAX_CLIENT_INTERFACES
Definition: dhcpv6_relay.h:59
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
error_t dhcpv6ForwardClientMessage(Dhcpv6RelayContext *context, uint_t index)
Forward client message.
OsTaskParameters task
Task parameters.
Definition: dhcpv6_relay.h:80
#define osExitTask()
void dhcpv6RelayGetDefaultSettings(Dhcpv6RelaySettings *settings)
Initialize settings with default values.
Definition: dhcpv6_relay.c:55
error_t dhcpv6ForwardRelayReplyMessage(Dhcpv6RelayContext *context)
Forward Relay-Reply message.
DHCPv6 relay agent (Dynamic Host Configuration Protocol for IPv6)
NetInterface * clientInterfaces[DHCPV6_RELAY_MAX_CLIENT_INTERFACES]
Client-facing interfaces.
Definition: dhcpv6_relay.h:83
@ ERROR_INVALID_INTERFACE
Invalid interface.
Definition: error.h:53
#define TRUE
Definition: os_port.h:50
#define OS_INVALID_TASK_ID
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
error_t dhcpv6RelayOpenServerSocket(Dhcpv6RelayContext *context)
Open server-facing socket.
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
error_t dhcpv6RelayStop(Dhcpv6RelayContext *context)
Stop DHCPv6 relay agent.
Definition: dhcpv6_relay.c:258
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:134
#define OS_SELF_TASK_ID
#define DHCPV6_RELAY_PRIORITY
Definition: dhcpv6_relay.h:54
error_t dhcpv6RelayStart(Dhcpv6RelayContext *context)
Start DHCPv6 relay agent.
Definition: dhcpv6_relay.c:175
NetInterface * serverInterface
Network-facing interface.
Definition: dhcpv6_relay.h:81
Helper functions for DHCPv6 relay agent.
void osDeleteTask(OsTaskId taskId)
Delete a task.
#define FALSE
Definition: os_port.h:46
NetInterface * serverInterface
Network-facing interface.
Definition: dhcpv6_relay.h:95
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
OsEvent event
Event object used to poll the sockets.
Definition: dhcpv6_relay.h:104
uint_t numClientInterfaces
Number of client-facing interfaces.
Definition: dhcpv6_relay.h:96
error_t
Error codes.
Definition: error.h:43
void(* OsTaskCode)(void *arg)
Task routine.
error_t dhcpv6RelayInit(Dhcpv6RelayContext *context, const Dhcpv6RelaySettings *settings)
DHCPv6 relay agent initialization.
Definition: dhcpv6_relay.c:90
NetInterface * clientInterfaces[DHCPV6_RELAY_MAX_CLIENT_INTERFACES]
Client-facing interfaces.
Definition: dhcpv6_relay.h:97
@ ERROR_INVALID_ADDRESS
Definition: error.h:103
void osDeleteEvent(OsEvent *event)
Delete an event object.
NetContext * netContext
TCP/IP stack context.
Definition: dhcpv6_relay.h:94
Socket * clientSockets[DHCPV6_RELAY_MAX_CLIENT_INTERFACES]
Sockets that handle client-facing interfaces.
Definition: dhcpv6_relay.h:100
const Ipv6Addr DHCPV6_ALL_SERVERS_ADDR
Definition: dhcpv6_common.c:58
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
uint_t numClientInterfaces
Number of client-facing interfaces.
Definition: dhcpv6_relay.h:82
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
#define TRACE_INFO(...)
Definition: debug.h:105
error_t dhcpv6RelayOpenClientSocket(Dhcpv6RelayContext *context, uint_t index)
Open client-facing socket.
#define osEnterTask()
uint_t eventFlags
Returned events.
Definition: socket.h:436
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:2182
void dhcpv6RelayTask(Dhcpv6RelayContext *context)
DHCPv6 relay agent task.
Definition: dhcpv6_relay.c:307
DHCPv6 relay agent settings.
Definition: dhcpv6_relay.h:79
bool_t stop
Stop request.
Definition: dhcpv6_relay.h:103
@ ERROR_TIMEOUT
Definition: error.h:95
SocketEventDesc eventDesc[DHCPV6_RELAY_MAX_CLIENT_INTERFACES]
The events the application is interested in.
Definition: dhcpv6_relay.h:101
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
DHCPv6 relay agent context.
Definition: dhcpv6_relay.h:93
@ ERROR_WAIT_CANCELED
Definition: error.h:73
bool_t osCreateEvent(OsEvent *event)
Create an event object.
OsTaskParameters taskParams
Task parameters.
Definition: dhcpv6_relay.h:105
void osDelayTask(systime_t delay)
Delay routine.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Ipv6Addr serverIpAddr
Address to be used when relaying messages to the server.
Definition: dhcpv6_relay.h:84
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:434
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
Ipv6Addr serverIpAddr
Address to be used when relaying messages to the server.
Definition: dhcpv6_relay.h:98
#define DHCPV6_RELAY_STACK_SIZE
Definition: dhcpv6_relay.h:47
uint_t eventMask
Requested events.
Definition: socket.h:435
void dhcpv6RelayDeinit(Dhcpv6RelayContext *context)
Release DHCP relay agent context.
Definition: dhcpv6_relay.c:377
@ ERROR_ALREADY_RUNNING
Definition: error.h:294
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
Socket * serverSocket
Socket that handles the network-facing interface.
Definition: dhcpv6_relay.h:99
#define INFINITE_DELAY
Definition: os_port.h:75
OsTaskId taskId
Task identifier.
Definition: dhcpv6_relay.h:106