ndp_router_adv.c
Go to the documentation of this file.
1 /**
2  * @file ndp_router_adv.c
3  * @brief Router advertisement service
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 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.2.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NDP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ipv6/ndp_router_adv.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Initialize settings with default values
46  * @param[out] settings Structure that contains the RA service configuration variables
47  **/
48 
50 {
51  //Underlying network interface
52  settings->interface = netGetDefaultInterface();
53 
54  //The maximum time allowed between sending unsolicited multicast
55  //Router Advertisements from the interface
57 
58  //The minimum time allowed between sending unsolicited multicast
59  //Router Advertisements from the interface
61 
62  //The default value to be placed in the Cur Hop Limit field in the
63  //Router Advertisement messages sent by the router
64  settings->curHopLimit = 0;
65 
66  //The value to be placed in the Managed Address Configuration
67  //flag in the Router Advertisement
68  settings->managedFlag = FALSE;
69 
70  //The value to be placed in the Other Configuration flag
71  //in the Router Advertisement
72  settings->otherConfigFlag = FALSE;
73 
74  //The value to be placed in the Mobile IPv6 Home Agent
75  //flag in the Router Advertisement
76  settings->homeAgentFlag = FALSE;
77 
78  //The value to be placed in the Router Selection Preferences
79  //field in the Router Advertisement
81 
82  //The value to be placed in the Neighbor Discovery Proxy
83  //flag in the Router Advertisement
84  settings->proxyFlag = FALSE;
85 
86  //The value to be placed in the Router Lifetime field of
87  //Router Advertisements sent from the interface
88  settings->defaultLifetime = 3 * (NDP_MAX_RTR_ADVERT_INTERVAL / 1000);
89 
90  //The value to be placed in the Reachable Time field in the
91  //Router Advertisement messages sent by the router
92  settings->reachableTime = 0;
93 
94  //The value to be placed in the Retrans Timer field in the
95  //Router Advertisement messages sent by the router
96  settings->retransTimer = 0;
97 
98  //The value to be placed in the MTU option sent by the router
99  settings->linkMtu = 0;
100 
101  //A list of prefixes to be placed in Prefix Information options (PIO)
102  //in Router Advertisement messages sent from the interface
103  settings->prefixList = NULL;
104  settings->prefixListLength = 0;
105 
106  //A list of routes to be placed in Route Information options (RIO)
107  //in Router Advertisement messages sent from the interface
108  settings->routeList = NULL;
109  settings->routeListLength = 0;
110 
111  //A list of header compression contexts to be placed in the 6LoWPAN Context
112  //options (6CO) in Router Advertisement messages sent from the interface
113  settings->contextList = NULL;
114  settings->contextListLength = 0;
115 }
116 
117 
118 /**
119  * @brief RA service initialization
120  * @param[in] context Pointer to the RA service context
121  * @param[in] settings RA service configuration variables
122  * @return Error code
123  **/
124 
126  const NdpRouterAdvSettings *settings)
127 {
128  NetInterface *interface;
129 
130  //Debug message
131  TRACE_INFO("Initializing Router Advertisement service...\r\n");
132 
133  //Ensure the parameters are valid
134  if(!context || !settings)
136 
137  //Valid network interface?
138  if(!settings->interface)
140 
141  //Get exclusive access
143 
144  //Point to the underlying network interface
145  interface = settings->interface;
146 
147  //Clear the RA service context
148  osMemset(context, 0, sizeof(NdpRouterAdvContext));
149  //Save user settings
150  context->settings = *settings;
151 
152  //The RA service is currently disabled on the interface
153  context->running = FALSE;
154  //Attach the RA service context to the network interface
155  interface->ndpRouterAdvContext = context;
156 
157  //Release exclusive access
159 
160  //Successful initialization
161  return NO_ERROR;
162 }
163 
164 
165 /**
166  * @brief Start RA service
167  * @param[in] context Pointer to the RA service context
168  * @return Error code
169  **/
170 
172 {
173  error_t error;
174  NetInterface *interface;
175 
176  //Make sure the RA service context is valid
177  if(context == NULL)
179 
180  //Debug message
181  TRACE_INFO("Starting Router Advertisement service...\r\n");
182 
183  //Get exclusive access
185 
186  //Check whether the service is running
187  if(!context->running)
188  {
189  //Point to the underlying network interface
190  interface = context->settings.interface;
191 
192  //Join the All-Routers multicast address
194 
195  //Successful membership registration?
196  if(!error)
197  {
198  //Reset variables
199  context->timestamp = osGetSystemTime();
200  context->timeout = 0;
201  context->routerAdvCount = 0;
202 
203  //Enable the router to forward packets to or from the interface
204  interface->ipv6Context.isRouter = TRUE;
205 
206  //Default Hop Limit value
207  if(context->settings.curHopLimit != 0)
208  {
209  interface->ipv6Context.curHopLimit = context->settings.curHopLimit;
210  }
211 
212  //The time a node assumes a neighbor is reachable
213  if(context->settings.reachableTime != 0)
214  {
215  interface->ndpContext.reachableTime = context->settings.reachableTime;
216  }
217 
218  //The time between retransmissions of NS messages
219  if(context->settings.retransTimer != 0)
220  {
221  interface->ndpContext.retransTimer = context->settings.retransTimer;
222  }
223 
224  //Start transmitting Router Advertisements
225  context->running = TRUE;
226  }
227  }
228  else
229  {
230  //The service is already running...
231  error = NO_ERROR;
232  }
233 
234  //Release exclusive access
236 
237  //Return status code
238  return error;
239 }
240 
241 
242 /**
243  * @brief Stop RA service
244  * @param[in] context Pointer to the RA service context
245  * @return Error code
246  **/
247 
249 {
250  error_t error;
251  NetInterface *interface;
252 
253  //Make sure the RA service context is valid
254  if(context == NULL)
256 
257  //Debug message
258  TRACE_INFO("Stopping Router Advertisement service...\r\n");
259 
260  //Get exclusive access
262 
263  //Check whether the service is running
264  if(context->running)
265  {
266  //Point to the underlying network interface
267  interface = context->settings.interface;
268 
269  //The router should transmit one or more final multicast Router
270  //Advertisements with a Router Lifetime field of zero
271  ndpSendRouterAdv(context, 0);
272 
273  //Leave the All-Routers multicast address
275 
276  //Restore default parameters
277  interface->ipv6Context.curHopLimit = IPV6_DEFAULT_HOP_LIMIT;
278  interface->ndpContext.reachableTime = NDP_REACHABLE_TIME;
279  interface->ndpContext.retransTimer = NDP_RETRANS_TIMER;
280 
281  //Stop transmitting Router Advertisements
282  context->running = FALSE;
283  }
284  else
285  {
286  //The service is not running...
287  error = NO_ERROR;
288  }
289 
290  //Release exclusive access
292 
293  //Return status code
294  return error;
295 }
296 
297 #endif
error_t ndpSendRouterAdv(NdpRouterAdvContext *context, uint16_t routerLifetime)
Send a Router Advertisement message.
@ NDP_ROUTER_SEL_PREFERENCE_MEDIUM
Definition: ndp.h:236
NdpRouterAdvSettings settings
RA service settings.
void ndpRouterAdvGetDefaultSettings(NdpRouterAdvSettings *settings)
Initialize settings with default values.
#define netMutex
Definition: net_legacy.h:195
systime_t timestamp
Timestamp to manage retransmissions.
#define TRUE
Definition: os_port.h:52
bool_t running
This flag tells whether the RA service is running.
uint_t routerAdvCount
Router Advertisement message counter.
NdpRouterAdvRouteInfo * routeList
#define IPV6_DEFAULT_HOP_LIMIT
Definition: ipv6.h:59
Router advertisement service.
#define FALSE
Definition: os_port.h:48
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
#define NDP_RETRANS_TIMER
Definition: ndp.h:186
#define NetInterface
Definition: net.h:36
error_t ndpRouterAdvInit(NdpRouterAdvContext *context, const NdpRouterAdvSettings *settings)
RA service initialization.
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:410
error_t ipv6LeaveMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Leave an IPv6 multicast group.
Definition: ipv6.c:2112
error_t ipv6JoinMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Join an IPv6 multicast group.
Definition: ipv6.c:2002
#define TRACE_INFO(...)
Definition: debug.h:95
NdpRouterAdvContextInfo * contextList
error_t ndpRouterAdvStart(NdpRouterAdvContext *context)
Start RA service.
RA service context.
RA service settings.
const Ipv6Addr IPV6_LINK_LOCAL_ALL_ROUTERS_ADDR
Definition: ipv6.c:78
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
NetInterface * interface
#define NDP_REACHABLE_TIME
Definition: ndp.h:179
#define NDP_MAX_RTR_ADVERT_INTERVAL
Definition: ndp.h:74
error_t ndpRouterAdvStop(NdpRouterAdvContext *context)
Stop RA service.
systime_t timeout
Timeout value.
Helper functions for router advertisement service.
#define osMemset(p, value, length)
Definition: os_port.h:134
TCP/IP stack core.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
NdpRouterAdvPrefixInfo * prefixList
systime_t osGetSystemTime(void)
Retrieve system time.