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-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  * @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.4.4
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  //Use default interface
63  settings->interface = netGetDefaultInterface();
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  //Save user settings
102  context->settings = *settings;
103 
104  //SLAAC operation is currently suspended
105  context->running = FALSE;
106 
107  //Attach the SLAAC context to the network interface
108  interface->slaacContext = context;
109 
110  //Successful initialization
111  return NO_ERROR;
112 }
113 
114 
115 /**
116  * @brief Start SLAAC process
117  * @param[in] context Pointer to the SLAAC context
118  * @return Error code
119  **/
120 
122 {
123  NetInterface *interface;
124 
125  //Make sure the SLAAC context is valid
126  if(context == NULL)
128 
129  //Debug message
130  TRACE_INFO("Starting SLAAC...\r\n");
131 
132  //Get exclusive access
134 
135  //Point to the underlying network interface
136  interface = context->settings.interface;
137 
138  //Clear the list of IPv6 addresses
139  ipv6FlushAddrList(interface);
140 
141  //Automatic DNS server configuration?
142  if(!context->settings.manualDnsConfig)
143  {
144  //Clear the list of DNS servers
145  ipv6FlushDnsServerList(interface);
146  }
147 
148  //Check if the link is up?
149  if(interface->linkState)
150  {
151  //A link-local address is formed by combining the well-known
152  //link-local prefix fe80::/10 with the interface identifier
154  }
155 
156  //Start SLAAC operation
157  context->running = TRUE;
158 
159  //Release exclusive access
161 
162  //Successful processing
163  return NO_ERROR;
164 }
165 
166 
167 /**
168  * @brief Stop SLAAC process
169  * @param[in] context Pointer to the SLAAC context
170  * @return Error code
171  **/
172 
174 {
175  //Make sure the SLAAC context is valid
176  if(context == NULL)
178 
179  //Debug message
180  TRACE_INFO("Stopping SLAAC...\r\n");
181 
182  //Get exclusive access
184 
185  //Suspend SLAAC operation
186  context->running = FALSE;
187 
188  //Release exclusive access
190 
191  //Successful processing
192  return NO_ERROR;
193 }
194 
195 #endif
IPv6 (Internet Protocol Version 6)
#define netMutex
Definition: net_legacy.h:195
#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:661
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:756
#define SlaacContext
Definition: slaac.h:50
error_t
Error codes.
Definition: error.h:43
#define NetInterface
Definition: net.h:36
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:467
Helper functions for IPv6.
void slaacGetDefaultSettings(SlaacSettings *settings)
Initialize settings with default values.
Definition: slaac.c:60
#define TRACE_INFO(...)
Definition: debug.h:95
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
error_t slaacStart(SlaacContext *context)
Start SLAAC process.
Definition: slaac.c:121
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Helper functions for SLAAC.
NetInterface * interface
Network interface to configure.
Definition: slaac.h:80
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
error_t slaacStop(SlaacContext *context)
Stop SLAAC process.
Definition: slaac.c:173
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
SLAAC settings.
Definition: slaac.h:79