slaac.c
Go to the documentation of this file.
1 /**
2  * @file slaac.c
3  * @brief IPv6 Stateless Address Autoconfiguration
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  * Stateless Address Autoconfiguration is a facility to allow devices to
30  * configure themselves independently. Refer to the following RFCs for
31  * complete details:
32  * - RFC 4862: IPv6 Stateless Address Autoconfiguration
33  * - RFC 6106: IPv6 Router Advertisement Options for DNS Configuration
34  *
35  * @author Oryx Embedded SARL (www.oryx-embedded.com)
36  * @version 2.6.0
37  **/
38 
39 //Switch to the appropriate trace level
40 #define TRACE_LEVEL SLAAC_TRACE_LEVEL
41 
42 //Dependencies
43 #include "core/net.h"
44 #include "core/ethernet.h"
45 #include "ipv6/ipv6.h"
46 #include "ipv6/ipv6_misc.h"
47 #include "ipv6/slaac.h"
48 #include "ipv6/slaac_misc.h"
49 #include "debug.h"
50 
51 //Check TCP/IP stack configuration
52 #if (IPV6_SUPPORT == ENABLED && SLAAC_SUPPORT == ENABLED)
53 
54 
55 /**
56  * @brief Initialize settings with default values
57  * @param[out] settings Structure that contains SLAAC settings
58  **/
59 
61 {
62  //Network interface to configure
63  settings->interface = NULL;
64 
65  //Use the DNS servers specified by the RDNSS option
66  settings->manualDnsConfig = FALSE;
67  //Link state change event
68  settings->linkChangeEvent = NULL;
69  //Router Advertisement parsing callback
70  settings->parseRouterAdvCallback = NULL;
71 }
72 
73 
74 /**
75  * @brief SLAAC initialization
76  * @param[in] context Pointer to the SLAAC context
77  * @param[in] settings SLAAC specific settings
78  * @return Error code
79  **/
80 
81 error_t slaacInit(SlaacContext *context, const SlaacSettings *settings)
82 {
83  NetInterface *interface;
84 
85  //Debug message
86  TRACE_INFO("Initializing SLAAC...\r\n");
87 
88  //Ensure the parameters are valid
89  if(context == NULL || settings == NULL)
91 
92  //The SLAAC service must be bound to a valid interface
93  if(settings->interface == NULL)
95 
96  //Point to the underlying network interface
97  interface = settings->interface;
98 
99  //Clear the SLAAC context
100  osMemset(context, 0, sizeof(SlaacContext));
101 
102  //Attach TCP/IP stack context
103  context->netContext = settings->interface->netContext;
104 
105  //Save user settings
106  context->interface = settings->interface;
107  context->manualDnsConfig = settings->manualDnsConfig;
108  context->linkChangeEvent = settings->linkChangeEvent;
109  context->parseRouterAdvCallback = settings->parseRouterAdvCallback;
110 
111  //SLAAC operation is currently suspended
112  context->running = FALSE;
113 
114  //Get exclusive access
115  netLock(context->netContext);
116  //Attach the SLAAC context to the network interface
117  interface->slaacContext = context;
118  //Release exclusive access
119  netUnlock(context->netContext);
120 
121  //Successful initialization
122  return NO_ERROR;
123 }
124 
125 
126 /**
127  * @brief Start SLAAC process
128  * @param[in] context Pointer to the SLAAC context
129  * @return Error code
130  **/
131 
133 {
134  NetInterface *interface;
135 
136  //Make sure the SLAAC context is valid
137  if(context == NULL)
139 
140  //Debug message
141  TRACE_INFO("Starting SLAAC...\r\n");
142 
143  //Get exclusive access
144  netLock(context->netContext);
145 
146  //Point to the underlying network interface
147  interface = context->interface;
148 
149  //Clear the list of IPv6 addresses
150  ipv6FlushAddrList(interface);
151 
152  //Automatic DNS server configuration?
153  if(!context->manualDnsConfig)
154  {
155  //Clear the list of DNS servers
156  ipv6FlushDnsServerList(interface);
157  }
158 
159  //Check if the link is up?
160  if(interface->linkState)
161  {
162  //A link-local address is formed by combining the well-known
163  //link-local prefix fe80::/10 with the interface identifier
165  }
166 
167  //Start SLAAC operation
168  context->running = TRUE;
169 
170  //Release exclusive access
171  netUnlock(context->netContext);
172 
173  //Successful processing
174  return NO_ERROR;
175 }
176 
177 
178 /**
179  * @brief Stop SLAAC process
180  * @param[in] context Pointer to the SLAAC context
181  * @return Error code
182  **/
183 
185 {
186  //Make sure the SLAAC context is valid
187  if(context == NULL)
189 
190  //Debug message
191  TRACE_INFO("Stopping SLAAC...\r\n");
192 
193  //Get exclusive access
194  netLock(context->netContext);
195  //Suspend SLAAC operation
196  context->running = FALSE;
197  //Release exclusive access
198  netUnlock(context->netContext);
199 
200  //Successful processing
201  return NO_ERROR;
202 }
203 
204 
205 /**
206  * @brief Release SLAAC context
207  * @param[in] context Pointer to the SLAAC context
208  **/
209 
210 void slaacDeinit(SlaacContext *context)
211 {
212  NetInterface *interface;
213 
214  //Make sure the SLAAC context is valid
215  if(context != NULL)
216  {
217  //Get exclusive access
218  netLock(context->netContext);
219 
220  //Point to the underlying network interface
221  interface = context->interface;
222  //Detach the SLAAC context from the network interface
223  interface->slaacContext = NULL;
224 
225  //Release exclusive access
226  netUnlock(context->netContext);
227 
228  //Clear SLAAC context
229  osMemset(context, 0, sizeof(SlaacContext));
230  }
231 }
232 
233 #endif
IPv6 (Internet Protocol Version 6)
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
#define TRUE
Definition: os_port.h:50
SlaacLinkChangeCallback linkChangeEvent
Link state change event.
Definition: slaac.h:82
void ipv6FlushAddrList(NetInterface *interface)
Flush the list of IPv6 addresses.
Definition: ipv6_misc.c:681
IPv6 Stateless Address Autoconfiguration.
SlaacParseRouterAdvCallback parseRouterAdvCallback
Router Advertisement parsing callback.
Definition: slaac.h:83
Ethernet.
#define FALSE
Definition: os_port.h:46
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
void ipv6FlushDnsServerList(NetInterface *interface)
Flush the list of DNS servers.
Definition: ipv6_misc.c:776
#define SlaacContext
Definition: slaac.h:50
error_t
Error codes.
Definition: error.h:43
#define NetInterface
Definition: net.h:40
Helper functions for IPv6.
void slaacGetDefaultSettings(SlaacSettings *settings)
Initialize settings with default values.
Definition: slaac.c:60
#define TRACE_INFO(...)
Definition: debug.h:105
bool_t manualDnsConfig
Force manual DNS configuration.
Definition: slaac.h:81
error_t slaacInit(SlaacContext *context, const SlaacSettings *settings)
SLAAC initialization.
Definition: slaac.c:81
error_t slaacGenerateLinkLocalAddr(SlaacContext *context)
Generate a link-local address.
Definition: slaac_misc.c:429
void slaacDeinit(SlaacContext *context)
Release SLAAC context.
Definition: slaac.c:210
error_t slaacStart(SlaacContext *context)
Start SLAAC process.
Definition: slaac.c:132
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
Helper functions for SLAAC.
NetInterface * interface
Network interface to configure.
Definition: slaac.h:80
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
error_t slaacStop(SlaacContext *context)
Stop SLAAC process.
Definition: slaac.c:184
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
SLAAC settings.
Definition: slaac.h:79