auto_ip_misc.c
Go to the documentation of this file.
1 /**
2  * @file auto_ip_misc.c
3  * @brief Helper functions for Auto-IP
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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL AUTO_IP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/ethernet.h"
37 #include "ipv4/arp.h"
38 #include "ipv4/auto_ip.h"
39 #include "ipv4/auto_ip_misc.h"
40 #include "mdns/mdns_responder.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (IPV4_SUPPORT == ENABLED && AUTO_IP_SUPPORT == ENABLED)
45 
46 //Tick counter to handle periodic operations
48 
49 
50 /**
51  * @brief Auto-IP timer handler
52  *
53  * This routine must be periodically called by the TCP/IP stack to
54  * manage Auto-IP operation
55  *
56  * @param[in] context Pointer to the Auto-IP context
57  **/
58 
59 void autoIpTick(AutoIpContext *context)
60 {
61  uint_t i;
63  systime_t delay;
64  NetInterface *interface;
65 
66  //Make sure Auto-IP has been properly instantiated
67  if(context == NULL)
68  return;
69 
70  //Point to the underlying network interface
71  interface = context->settings.interface;
72  //Index of the IP address in the list of addresses assigned to the interface
73  i = context->settings.ipAddrIndex;
74 
75  //Get current time
77 
78  //Check current state
79  if(context->state == AUTO_IP_STATE_INIT)
80  {
81  //Wait for the link to be up before starting Auto-IP
82  if(context->running && interface->linkState)
83  {
84  //Configure subnet mask
85  interface->ipv4Context.addrList[i].subnetMask = AUTO_IP_MASK;
86 
87  //The address must be in the range from 169.254.1.0 to 169.254.254.255
88  if(ntohl(context->linkLocalAddr) < NTOHL(AUTO_IP_ADDR_MIN) ||
89  ntohl(context->linkLocalAddr) > NTOHL(AUTO_IP_ADDR_MAX))
90  {
91  //Generate a random link-local address
92  autoIpGenerateAddr(&context->linkLocalAddr);
93  }
94 
95  //Use the link-local address as a tentative address
96  interface->ipv4Context.addrList[i].addr = context->linkLocalAddr;
97  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_TENTATIVE;
98 
99  //Clear conflict flag
100  interface->ipv4Context.addrList[i].conflict = FALSE;
101 
102  //Initial random delay
104 
105  //Check whether the number of conflicts exceeds the maximum acceptable
106  //value
107  if(context->conflictCount >= AUTO_IP_MAX_CONFLICTS)
108  {
109  //The host must limit the rate at which it probes for new addresses
111  }
112 
113  //Verify the uniqueness of the link-local address
114  autoIpChangeState(context, AUTO_IP_STATE_PROBING, delay);
115  }
116  }
117  else if(context->state == AUTO_IP_STATE_PROBING)
118  {
119  //Any conflict detected?
120  if(interface->ipv4Context.addrList[i].conflict)
121  {
122  //The address is already in use by some other host and must not be
123  //assigned to the interface
124  autoIpResetConfig(context);
125 
126  //The host should maintain a counter of the number of address conflicts
127  //it has experienced
128  context->conflictCount++;
129 
130  //The host must pick a new random address and repeat the process
131  autoIpGenerateAddr(&context->linkLocalAddr);
132  //Update state machine
134  }
135  else
136  {
137  //Check current time
138  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
139  {
140  //Address Conflict Detection is on-going?
141  if(context->retransmitCount < AUTO_IP_PROBE_NUM)
142  {
143  //Conflict detection is done using ARP probes
144  arpSendProbe(interface, context->linkLocalAddr);
145 
146  //Save the time at which the packet was sent
147  context->timestamp = time;
148  //Increment retransmission counter
149  context->retransmitCount++;
150 
151  //Last probe packet sent?
152  if(context->retransmitCount == AUTO_IP_PROBE_NUM)
153  {
154  //Delay before announcing
155  context->timeout = AUTO_IP_ANNOUNCE_WAIT;
156  }
157  else
158  {
159  //Maximum delay till repeated probe
160  context->timeout = netGenerateRandRange(AUTO_IP_PROBE_MIN,
162  }
163  }
164  else
165  {
166  //The use of the IPv4 address is now unrestricted
167  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_VALID;
168 
169 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
170  //Restart mDNS probing process
171  mdnsResponderStartProbing(interface->mdnsResponderContext);
172 #endif
173  //The host must then announce its claimed address
175  }
176  }
177  }
178  }
179  else if(context->state == AUTO_IP_STATE_ANNOUNCING)
180  {
181  //Check current time
182  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
183  {
184  //An ARP announcement is identical to an ARP probe, except that now
185  //the sender and target IP addresses are both set to the host's newly
186  //selected IPv4 address
187  arpSendRequest(interface, context->linkLocalAddr, &MAC_BROADCAST_ADDR);
188 
189  //Save the time at which the packet was sent
190  context->timestamp = time;
191  //Time interval between announcement packets
192  context->timeout = AUTO_IP_ANNOUNCE_INTERVAL;
193  //Increment retransmission counter
194  context->retransmitCount++;
195 
196  //Announcing is complete?
197  if(context->retransmitCount >= AUTO_IP_ANNOUNCE_NUM)
198  {
199  //Successful address autoconfiguration
201  //Reset conflict counter
202  context->conflictCount = 0;
203 
204  //Dump current IPv4 configuration for debugging purpose
205  autoIpDumpConfig(context);
206  }
207  }
208  }
209  else if(context->state == AUTO_IP_STATE_CONFIGURED)
210  {
211  //Address Conflict Detection is an on-going process that is in effect for
212  //as long as a host is using an IPv4 link-local address
213  if(interface->ipv4Context.addrList[i].conflict)
214  {
215  //The host may elect to attempt to defend its address by recording
216  //the time that the conflicting ARP packet was received, and then
217  //broadcasting one single ARP announcement, giving its own IP and
218  //hardware addresses as the sender addresses of the ARP
219 #if (AUTO_IP_BCT_SUPPORT == ENABLED)
220  arpSendProbe(interface, context->linkLocalAddr);
221 #else
222  arpSendRequest(interface, context->linkLocalAddr, &MAC_BROADCAST_ADDR);
223 #endif
224  //Clear conflict flag
225  interface->ipv4Context.addrList[i].conflict = FALSE;
226 
227  //The host can then continue to use the address normally without
228  //any further special action
230  }
231  }
232  else if(context->state == AUTO_IP_STATE_DEFENDING)
233  {
234  //if this is not the first conflicting ARP packet the host has seen, and
235  //the time recorded for the previous conflicting ARP packet is recent,
236  //within DEFEND_INTERVAL seconds, then the host must immediately cease
237  //using this address
238  if(interface->ipv4Context.addrList[i].conflict)
239  {
240  //The link-local address cannot be used anymore
241  autoIpResetConfig(context);
242 
243 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
244  //Restart mDNS probing process
245  mdnsResponderStartProbing(interface->mdnsResponderContext);
246 #endif
247  //The host must pick a new random address and probes/announces again
248  autoIpGenerateAddr(&context->linkLocalAddr);
249  //Update state machine
251  }
252  else
253  {
254  //Check whether the DEFEND_INTERVAL has elapsed
255  if(timeCompare(time, context->timestamp + AUTO_IP_DEFEND_INTERVAL) >= 0)
256  {
257  //The host can continue to use its link-local address
259  }
260  }
261  }
262 }
263 
264 
265 /**
266  * @brief Callback function for link change event
267  * @param[in] context Pointer to the Auto-IP context
268  **/
269 
271 {
272  NetInterface *interface;
273 
274  //Make sure Auto-IP has been properly instantiated
275  if(context == NULL)
276  return;
277 
278  //Point to the underlying network interface
279  interface = context->settings.interface;
280 
281  //Check whether Auto-IP is enabled
282  if(context->running)
283  {
284  //The host address is not longer valid
285  autoIpResetConfig(context);
286 
287 #if (MDNS_RESPONDER_SUPPORT == ENABLED)
288  //Restart mDNS probing process
289  mdnsResponderStartProbing(interface->mdnsResponderContext);
290 #endif
291  }
292 
293  //Reinitialize state machine
294  context->state = AUTO_IP_STATE_INIT;
295  //Reset conflict counter
296  context->conflictCount = 0;
297 
298  //Any registered callback?
299  if(context->settings.linkChangeEvent != NULL)
300  {
301  //Release exclusive access
303  //Invoke user callback function
304  context->settings.linkChangeEvent(context, interface, interface->linkState);
305  //Get exclusive access
307  }
308 }
309 
310 
311 /**
312  * @brief Update Auto-IP FSM state
313  * @param[in] context Pointer to the Auto-IP context
314  * @param[in] newState New Auto-IP state to switch to
315  * @param[in] delay Initial delay
316  **/
317 
319  systime_t delay)
320 {
321  NetInterface *interface;
322 
323  //Point to the underlying network interface
324  interface = context->settings.interface;
325 
326  //Set time stamp
327  context->timestamp = osGetSystemTime();
328  //Set initial delay
329  context->timeout = delay;
330  //Reset retransmission counter
331  context->retransmitCount = 0;
332  //Switch to the new state
333  context->state = newState;
334 
335  //Any registered callback?
336  if(context->settings.stateChangeEvent != NULL)
337  {
338  //Release exclusive access
340  //Invoke user callback function
341  context->settings.stateChangeEvent(context, interface, newState);
342  //Get exclusive access
344  }
345 }
346 
347 
348 /**
349  * @brief Generate a random link-local address
350  * @param[out] ipAddr Random link-local address
351  **/
352 
354 {
355  uint32_t n;
356 
357  //Generate a random address in the range from 169.254.1.0 to 169.254.254.255
360 
361  //Convert the resulting address to network byte order
362  *ipAddr = htonl(n);
363 }
364 
365 
366 /**
367  * @brief Reset Auto-IP configuration
368  * @param[in] context Pointer to the Auto-IP context
369  **/
370 
372 {
373  uint_t i;
374  NetInterface *interface;
375 
376  //Point to the underlying network interface
377  interface = context->settings.interface;
378  //Index of the IP address in the list of addresses assigned to the interface
379  i = context->settings.ipAddrIndex;
380 
381  //The host address is not longer valid
382  interface->ipv4Context.addrList[i].addr = IPV4_UNSPECIFIED_ADDR;
383  interface->ipv4Context.addrList[i].state = IPV4_ADDR_STATE_INVALID;
384 
385  //Clear subnet mask
386  interface->ipv4Context.addrList[i].subnetMask = IPV4_UNSPECIFIED_ADDR;
387 
388  //The host must not send packets to any router for forwarding (refer to
389  //RFC 3927, section 2.6.2)
390  interface->ipv4Context.addrList[i].defaultGateway = IPV4_UNSPECIFIED_ADDR;
391 }
392 
393 
394 /**
395  * @brief Dump Auto-IP configuration for debugging purpose
396  * @param[in] context Pointer to the Auto-IP context
397  **/
398 
400 {
401 #if (AUTO_IP_TRACE_LEVEL >= TRACE_LEVEL_INFO)
402  uint_t i;
403  NetInterface *interface;
404  Ipv4Context *ipv4Context;
405 
406  //Point to the underlying network interface
407  interface = context->settings.interface;
408  //Point to the IPv4 context
409  ipv4Context = &interface->ipv4Context;
410 
411  //Index of the IP address in the list of addresses assigned to the interface
412  i = context->settings.ipAddrIndex;
413 
414  //Debug message
415  TRACE_INFO("\r\n");
416  TRACE_INFO("Auto-IP configuration:\r\n");
417 
418  //Link-local address
419  TRACE_INFO(" Link-local Address = %s\r\n",
420  ipv4AddrToString(ipv4Context->addrList[i].addr, NULL));
421 
422  //Subnet mask
423  TRACE_INFO(" Subnet Mask = %s\r\n",
424  ipv4AddrToString(ipv4Context->addrList[i].subnetMask, NULL));
425 
426  //Debug message
427  TRACE_INFO("\r\n");
428 #endif
429 }
430 
431 #endif
@ AUTO_IP_STATE_PROBING
Definition: auto_ip.h:154
#define netMutex
Definition: net_legacy.h:195
@ AUTO_IP_STATE_DEFENDING
Definition: auto_ip.h:157
void autoIpDumpConfig(AutoIpContext *context)
Dump Auto-IP configuration for debugging purpose.
Definition: auto_ip_misc.c:399
#define AUTO_IP_MAX_CONFLICTS
Definition: auto_ip.h:109
#define AutoIpContext
Definition: auto_ip.h:139
#define AUTO_IP_DEFEND_INTERVAL
Definition: auto_ip.h:123
#define AUTO_IP_PROBE_NUM
Definition: auto_ip.h:67
void autoIpLinkChangeEvent(AutoIpContext *context)
Callback function for link change event.
Definition: auto_ip_misc.c:270
#define timeCompare(t1, t2)
Definition: os_port.h:40
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:297
#define AUTO_IP_PROBE_MIN
Definition: auto_ip.h:74
IPv4 context.
Definition: ipv4.h:426
Ethernet.
uint32_t netGenerateRand(void)
Generate a random 32-bit value.
Definition: net_misc.c:922
#define AUTO_IP_ADDR_MIN
Definition: auto_ip.h:134
#define FALSE
Definition: os_port.h:46
@ AUTO_IP_STATE_CONFIGURED
Definition: auto_ip.h:156
#define htonl(value)
Definition: cpu_endian.h:414
uint32_t netGenerateRandRange(uint32_t min, uint32_t max)
Generate a random value in the specified range.
Definition: net_misc.c:948
@ IPV4_ADDR_STATE_TENTATIVE
An address whose uniqueness on a link is being verified.
Definition: ipv4.h:203
#define AUTO_IP_ANNOUNCE_NUM
Definition: auto_ip.h:95
#define AUTO_IP_ANNOUNCE_INTERVAL
Definition: auto_ip.h:102
void autoIpChangeState(AutoIpContext *context, AutoIpState newState, systime_t delay)
Update Auto-IP FSM state.
Definition: auto_ip_misc.c:318
#define AUTO_IP_MASK
Definition: auto_ip.h:131
#define NetInterface
Definition: net.h:36
void autoIpResetConfig(AutoIpContext *context)
Reset Auto-IP configuration.
Definition: auto_ip_misc.c:371
#define TRACE_INFO(...)
Definition: debug.h:95
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:877
uint32_t systime_t
System time.
#define AUTO_IP_ADDR_MAX
Definition: auto_ip.h:135
uint32_t time
void autoIpGenerateAddr(Ipv4Addr *ipAddr)
Generate a random link-local address.
Definition: auto_ip_misc.c:353
uint8_t n
#define AUTO_IP_ANNOUNCE_WAIT
Definition: auto_ip.h:88
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
@ IPV4_ADDR_STATE_INVALID
An address that is not assigned to any interface.
Definition: ipv4.h:202
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
systime_t autoIpTickCounter
Definition: auto_ip_misc.c:47
@ AUTO_IP_STATE_INIT
Definition: auto_ip.h:153
void autoIpTick(AutoIpContext *context)
Auto-IP timer handler.
Definition: auto_ip_misc.c:59
Helper functions for Auto-IP.
#define AUTO_IP_RATE_LIMIT_INTERVAL
Definition: auto_ip.h:116
Ipv4Addr ipAddr
Definition: ipcp.h:105
#define AUTO_IP_PROBE_WAIT
Definition: auto_ip.h:60
@ IPV4_ADDR_STATE_VALID
An address assigned to an interface whose use is unrestricted.
Definition: ipv4.h:204
#define NTOHL(value)
Definition: cpu_endian.h:419
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
char_t * ipv4AddrToString(Ipv4Addr ipAddr, char_t *str)
Convert a binary IPv4 address to dot-decimal notation.
Definition: ipv4.c:1457
AutoIpState
Auto-IP FSM states.
Definition: auto_ip.h:152
ARP (Address Resolution Protocol)
#define ntohl(value)
Definition: cpu_endian.h:422
error_t mdnsResponderStartProbing(MdnsResponderContext *context)
Restart probing process.
@ AUTO_IP_STATE_ANNOUNCING
Definition: auto_ip.h:155
#define AUTO_IP_PROBE_MAX
Definition: auto_ip.h:81
Debugging facilities.
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:817
const MacAddr MAC_BROADCAST_ADDR
Definition: ethernet.c:55
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:117
mDNS responder (Multicast DNS)
Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
systime_t osGetSystemTime(void)
Retrieve system time.