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-2019 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 1.9.6
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 "core/ip.h"
37 #include "ipv6/ipv6.h"
38 #include "ipv6/ipv6_misc.h"
39 #include "ipv6/icmpv6.h"
40 #include "ipv6/ndp.h"
41 #include "ipv6/ndp_cache.h"
42 #include "ipv6/ndp_misc.h"
43 #include "ipv6/ndp_router_adv.h"
44 #include "mibs/ip_mib_module.h"
45 #include "debug.h"
46 
47 //Check TCP/IP stack configuration
48 #if (IPV6_SUPPORT == ENABLED && NDP_ROUTER_ADV_SUPPORT == ENABLED)
49 
50 //Tick counter to handle periodic operations
52 
53 
54 /**
55  * @brief Initialize settings with default values
56  * @param[out] settings Structure that contains the RA service configuration variables
57  **/
58 
60 {
61  //Underlying network interface
62  settings->interface = netGetDefaultInterface();
63 
64  //The maximum time allowed between sending unsolicited multicast
65  //Router Advertisements from the interface
67 
68  //The minimum time allowed between sending unsolicited multicast
69  //Router Advertisements from the interface
71 
72  //The default value to be placed in the Cur Hop Limit field in the
73  //Router Advertisement messages sent by the router
74  settings->curHopLimit = 0;
75 
76  //The value to be placed in the Managed Address Configuration
77  //flag in the Router Advertisement
78  settings->managedFlag = FALSE;
79 
80  //The value to be placed in the Other Configuration flag
81  //in the Router Advertisement
82  settings->otherConfigFlag = FALSE;
83 
84  //The value to be placed in the Mobile IPv6 Home Agent
85  //flag in the Router Advertisement
86  settings->homeAgentFlag = FALSE;
87 
88  //The value to be placed in the Router Selection Preferences
89  //field in the Router Advertisement
91 
92  //The value to be placed in the Neighbor Discovery Proxy
93  //flag in the Router Advertisement
94  settings->proxyFlag = FALSE;
95 
96  //The value to be placed in the Router Lifetime field of
97  //Router Advertisements sent from the interface
98  settings->defaultLifetime = 3 * (NDP_MAX_RTR_ADVERT_INTERVAL / 1000);
99 
100  //The value to be placed in the Reachable Time field in the
101  //Router Advertisement messages sent by the router
102  settings->reachableTime = 0;
103 
104  //The value to be placed in the Retrans Timer field in the
105  //Router Advertisement messages sent by the router
106  settings->retransTimer = 0;
107 
108  //The value to be placed in the MTU option sent by the router
109  settings->linkMtu = 0;
110 
111  //A list of prefixes to be placed in Prefix Information options (PIO)
112  //in Router Advertisement messages sent from the interface
113  settings->prefixList = NULL;
114  settings->prefixListLength = 0;
115 
116  //A list of routes to be placed in Route Information options (RIO)
117  //in Router Advertisement messages sent from the interface
118  settings->routeList = NULL;
119  settings->routeListLength = 0;
120 
121  //A list of header compression contexts to be placed in the 6LoWPAN Context
122  //options (6CO) in Router Advertisement messages sent from the interface
123  settings->contextList = NULL;
124  settings->contextListLength = 0;
125 }
126 
127 
128 /**
129  * @brief RA service initialization
130  * @param[in] context Pointer to the RA service context
131  * @param[in] settings RA service configuration variables
132  * @return Error code
133  **/
134 
136  const NdpRouterAdvSettings *settings)
137 {
138  NetInterface *interface;
139 
140  //Debug message
141  TRACE_INFO("Initializing Router Advertisement service...\r\n");
142 
143  //Ensure the parameters are valid
144  if(!context || !settings)
146 
147  //Valid network interface?
148  if(!settings->interface)
150 
151  //Get exclusive access
153 
154  //Point to the underlying network interface
155  interface = settings->interface;
156 
157  //Clear the RA service context
158  memset(context, 0, sizeof(NdpRouterAdvContext));
159  //Save user settings
160  context->settings = *settings;
161 
162  //The RA service is currently disabled on the interface
163  context->running = FALSE;
164  //Attach the RA service context to the network interface
165  interface->ndpRouterAdvContext = context;
166 
167  //Release exclusive access
169 
170  //Successful initialization
171  return NO_ERROR;
172 }
173 
174 
175 /**
176  * @brief Start RA service
177  * @param[in] context Pointer to the RA service context
178  * @return Error code
179  **/
180 
182 {
183  error_t error;
184  NetInterface *interface;
185 
186  //Check parameter
187  if(context == NULL)
189 
190  //Debug message
191  TRACE_INFO("Starting Router Advertisement service...\r\n");
192 
193  //Get exclusive access
195 
196  //Check whether the service is running
197  if(!context->running)
198  {
199  //Point to the underlying network interface
200  interface = context->settings.interface;
201 
202  //Join the All-Routers multicast address
204 
205  //Successful membership registration?
206  if(!error)
207  {
208  //Reset variables
209  context->timestamp = osGetSystemTime();
210  context->timeout = 0;
211  context->routerAdvCount = 0;
212 
213  //Enable the router to forward packets to or from the interface
214  interface->ipv6Context.isRouter = TRUE;
215 
216  //Default Hop Limit value
217  if(context->settings.curHopLimit != 0)
218  interface->ipv6Context.curHopLimit = context->settings.curHopLimit;
219 
220  //The time a node assumes a neighbor is reachable
221  if(context->settings.reachableTime != 0)
222  interface->ndpContext.reachableTime = context->settings.reachableTime;
223 
224  //The time between retransmissions of NS messages
225  if(context->settings.retransTimer != 0)
226  interface->ndpContext.retransTimer = context->settings.retransTimer;
227 
228  //Start transmitting Router Advertisements
229  context->running = TRUE;
230  }
231  }
232  else
233  {
234  //The service is already running...
235  error = NO_ERROR;
236  }
237 
238  //Release exclusive access
240 
241  //Return status code
242  return error;
243 }
244 
245 
246 /**
247  * @brief Stop RA service
248  * @param[in] context Pointer to the RA service context
249  * @return Error code
250  **/
251 
253 {
254  error_t error;
255  NetInterface *interface;
256 
257  //Check parameter
258  if(context == NULL)
260 
261  //Debug message
262  TRACE_INFO("Stopping Router Advertisement service...\r\n");
263 
264  //Get exclusive access
266 
267  //Check whether the service is running
268  if(context->running)
269  {
270  //Point to the underlying network interface
271  interface = context->settings.interface;
272 
273  //The router should transmit one or more final multicast Router
274  //Advertisements with a Router Lifetime field of zero
275  ndpSendRouterAdv(context, 0);
276 
277  //Leave the All-Routers multicast address
279 
280  //Restore default parameters
281  interface->ipv6Context.curHopLimit = IPV6_DEFAULT_HOP_LIMIT;
282  interface->ndpContext.reachableTime = NDP_REACHABLE_TIME;
283  interface->ndpContext.retransTimer = NDP_RETRANS_TIMER;
284 
285  //Stop transmitting Router Advertisements
286  context->running = FALSE;
287  }
288  else
289  {
290  //The service is not running...
291  error = NO_ERROR;
292  }
293 
294  //Release exclusive access
296 
297  //Return status code
298  return error;
299 }
300 
301 
302 /**
303  * @brief RA service timer handler
304  * @param[in] context Pointer to the RA service context
305  **/
306 
308 {
309  systime_t time;
310  NetInterface *interface;
311  NdpRouterAdvSettings *settings;
312 
313  //Make sure the RA service has been properly instantiated
314  if(context == NULL)
315  return;
316 
317  //Point to the underlying network interface
318  interface = context->settings.interface;
319  //Point to the router configuration variables
320  settings = &context->settings;
321 
322  //Get current time
323  time = osGetSystemTime();
324 
325  //Make sure that the link is up and the service is running
326  if(interface->linkState && context->running)
327  {
328  //Make sure that a valid link-local address has been assigned to the
329  //interface
331  {
332  //Check current time
333  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
334  {
335  //Send an unsolicited Router Advertisement
336  ndpSendRouterAdv(context, context->settings.defaultLifetime);
337 
338  //Save the time at which the message was sent
339  context->timestamp = time;
340 
341  //Whenever a multicast advertisement is sent from an interface, the
342  //timer is reset to a uniformly distributed random value between
343  //MinRtrAdvInterval and MaxRtrAdvInterval
344  context->timeout = netGetRandRange(settings->minRtrAdvInterval,
345  settings->maxRtrAdvInterval);
346 
347  //First Router Advertisements to be sent from this interface?
349  {
350  //For the first few advertisements sent from an interface when it
351  //becomes an advertising interface, the randomly chosen interval
352  //should not be greater than MAX_INITIAL_RTR_ADVERT_INTERVAL
354  }
355 
356  //Increment counter
357  context->routerAdvCount++;
358  }
359  }
360  }
361 }
362 
363 
364 /**
365  * @brief Callback function for link change event
366  * @param[in] context Pointer to the RA service context
367  **/
368 
370 {
371  NetInterface *interface;
372 
373  //Make sure the RA service has been properly instantiated
374  if(context == NULL)
375  return;
376 
377  //Point to the underlying network interface
378  interface = context->settings.interface;
379 
380  //Reset variables
381  context->timestamp = osGetSystemTime();
382  context->timeout = 0;
383  context->routerAdvCount = 0;
384 
385  //Default Hop Limit value
386  if(context->settings.curHopLimit != 0)
387  interface->ipv6Context.curHopLimit = context->settings.curHopLimit;
388 
389  //The time a node assumes a neighbor is reachable
390  if(context->settings.reachableTime != 0)
391  interface->ndpContext.reachableTime = context->settings.reachableTime;
392 
393  //The time between retransmissions of NS messages
394  if(context->settings.retransTimer != 0)
395  interface->ndpContext.retransTimer = context->settings.retransTimer;
396 }
397 
398 
399 /**
400  * @brief Router Solicitation message processing
401  * @param[in] interface Underlying network interface
402  * @param[in] pseudoHeader IPv6 pseudo header
403  * @param[in] buffer Multi-part buffer containing the Router Advertisement message
404  * @param[in] offset Offset to the first byte of the message
405  * @param[in] hopLimit Hop Limit field from IPv6 header
406  **/
407 
408 void ndpProcessRouterSol(NetInterface *interface, Ipv6PseudoHeader *pseudoHeader,
409  const NetBuffer *buffer, size_t offset, uint8_t hopLimit)
410 {
411  error_t error;
412  uint_t n;
413  size_t length;
414  systime_t time;
415  systime_t delay;
416  NdpRouterAdvContext *context;
418  NdpLinkLayerAddrOption *option;
419  NdpNeighborCacheEntry *entry;
420 
421  //Point to the RA service context
422  context = interface->ndpRouterAdvContext;
423 
424  //A host must silently discard any received Router Solicitation
425  if(context == NULL)
426  return;
427 
428  //Get current time
429  time = osGetSystemTime();
430 
431  //Retrieve the length of the message
432  length = netBufferGetLength(buffer) - offset;
433 
434  //Check the length of the Router Solicitation message
435  if(length < sizeof(NdpRouterSolMessage))
436  return;
437 
438  //Point to the beginning of the message
439  message = netBufferAt(buffer, offset);
440  //Sanity check
441  if(message == NULL)
442  return;
443 
444  //Debug message
445  TRACE_INFO("Router Solicitation message received (%" PRIuSIZE " bytes)...\r\n",
446  length);
447 
448  //Dump message contents for debugging purpose
450 
451  //The IPv6 Hop Limit field must have a value of 255 to ensure that the
452  //packet has not been forwarded by a router
453  if(hopLimit != NDP_HOP_LIMIT)
454  return;
455 
456  //ICMPv6 Code must be 0
457  if(message->code)
458  return;
459 
460  //Calculate the length of the Options field
461  length -= sizeof(NdpRouterSolMessage);
462 
463  //Parse Options field
464  error = ndpCheckOptions(message->options, length);
465  //All included options must have a length that is greater than zero
466  if(error)
467  return;
468 
469  //Search for the Source Link-Layer Address option
470  option = ndpGetOption(message->options,
472 
473  //Source Link-Layer Address option found?
474  if(option != NULL && option->length == 1)
475  {
476  //Debug message
477  TRACE_DEBUG(" Source Link-Layer Address = %s\r\n",
478  macAddrToString(&option->linkLayerAddr, NULL));
479 
480  //The Source Link-Layer Address option must not be included when the
481  //source IP address is the unspecified address
482  if(ipv6CompAddr(&pseudoHeader->srcAddr, &IPV6_UNSPECIFIED_ADDR))
483  return;
484 
485  //Search the Neighbor Cache for the source address of the solicitation
486  entry = ndpFindNeighborCacheEntry(interface, &pseudoHeader->srcAddr);
487 
488  //No matching entry has been found?
489  if(!entry)
490  {
491  //Create an entry
492  entry = ndpCreateNeighborCacheEntry(interface);
493 
494  //Neighbor Cache entry successfully created?
495  if(entry)
496  {
497  //Record the IPv6 and the corresponding MAC address
498  entry->ipAddr = pseudoHeader->srcAddr;
499  entry->macAddr = option->linkLayerAddr;
500  //The IsRouter flag must be set to FALSE
501  entry->isRouter = FALSE;
502  //Save current time
503  entry->timestamp = time;
504  //Enter the STALE state
505  entry->state = NDP_STATE_STALE;
506  }
507  }
508  else
509  {
510  //If a Neighbor Cache entry for the solicitation's sender exists the
511  //entry's IsRouter flag must be set to FALSE
512  entry->isRouter = FALSE;
513 
514  //INCOMPLETE state?
515  if(entry->state == NDP_STATE_INCOMPLETE)
516  {
517  //Record link-layer address
518  entry->macAddr = option->linkLayerAddr;
519  //Send all the packets that are pending for transmission
520  n = ndpSendQueuedPackets(interface, entry);
521  //Save current time
522  entry->timestamp = time;
523 
524  //Check whether any packets have been sent
525  if(n > 0)
526  {
527  //Start delay timer
529  //Switch to the DELAY state
530  entry->state = NDP_STATE_DELAY;
531  }
532  else
533  {
534  //Enter the STALE state
535  entry->state = NDP_STATE_STALE;
536  }
537  }
538  //REACHABLE, STALE, DELAY or PROBE state?
539  else
540  {
541  //Different link-layer address than cached?
542  if(!macCompAddr(&entry->macAddr, &option->linkLayerAddr))
543  {
544  //Update link-layer address
545  entry->macAddr = option->linkLayerAddr;
546  //Save current time
547  entry->timestamp = time;
548  //Enter the STALE state
549  entry->state = NDP_STATE_STALE;
550  }
551  }
552  }
553  }
554 
555  //Upon receipt of a Router Solicitation, compute a random delay within the
556  //range 0 through MAX_RA_DELAY_TIME
558 
559  //If the computed value corresponds to a time later than the time the next
560  //multicast Router Advertisement is scheduled to be sent, ignore the random
561  //delay and send the advertisement at the already-scheduled time
562  if(timeCompare(time + delay, context->timestamp + context->timeout) > 0)
563  return;
564 
565  //Check whether the router sent a multicast Router Advertisement (solicited
566  //or unsolicited) within the last MIN_DELAY_BETWEEN_RAS seconds
568  {
569  //Schedule the advertisement to be sent at a time corresponding to
570  //MIN_DELAY_BETWEEN_RAS plus the random value after the previous
571  //advertisement was sent. This ensures that the multicast Router
572  //Advertisements are rate limited
573  context->timeout = NDP_MIN_DELAY_BETWEEN_RAS + delay;
574  }
575  else
576  {
577  //Schedule the sending of a Router Advertisement at the time given by the
578  //random value
579  context->timeout = time + delay - context->timestamp;
580  }
581 }
582 
583 
584 /**
585  * @brief Send a Router Advertisement message
586  * @param[in] context Pointer to the RA service context
587  * @param[in] routerLifetime Router Lifetime field
588  * @return Error code
589  **/
590 
592 {
593  error_t error;
594  uint_t i;
595  uint32_t n;
596  size_t offset;
597  size_t length;
598  NetBuffer *buffer;
599  NetInterface *interface;
601  NdpRouterAdvSettings *settings;
602  Ipv6PseudoHeader pseudoHeader;
603 #if (ETH_SUPPORT == ENABLED)
604  NetInterface *logicalInterface;
605 #endif
606 
607  //Point to the underlying network interface
608  interface = context->settings.interface;
609  //Point to the router configuration variables
610  settings = &context->settings;
611 
612  //The destination address is typically the all-nodes multicast address
613  pseudoHeader.destAddr = IPV6_LINK_LOCAL_ALL_NODES_ADDR;
614 
615  //Routers must use their link-local address as the source for Router
616  //Advertisement messages so that hosts can uniquely identify routers
617  error = ipv6SelectSourceAddr(&interface, &pseudoHeader.destAddr,
618  &pseudoHeader.srcAddr);
619 
620  //No link-local address assigned to the interface?
621  if(error)
622  return error;
623 
624  //Compute the maximum size of the Router Advertisement message
625  length = sizeof(NdpRouterAdvMessage) +
626  sizeof(NdpLinkLayerAddrOption) + sizeof(NdpMtuOption) +
627  settings->prefixListLength * sizeof(NdpPrefixInfoOption) +
628  settings->routeListLength * sizeof(NdpRouteInfoOption) +
629  settings->contextListLength * sizeof(NdpContextOption);
630 
631  //Sanity check
632  if((length + sizeof(Ipv6Header)) > IPV6_DEFAULT_MTU)
633  return ERROR_FAILURE;
634 
635  //Allocate a memory buffer to hold the Router Advertisement message
636  buffer = ipAllocBuffer(length, &offset);
637  //Failed to allocate memory?
638  if(buffer == NULL)
639  return ERROR_OUT_OF_MEMORY;
640 
641  //Point to the beginning of the message
642  message = netBufferAt(buffer, offset);
643 
644  //Format Router Advertisement message
646  message->code = 0;
647  message->checksum = 0;
648  message->curHopLimit = settings->curHopLimit;
649  message->m = settings->managedFlag;
650  message->o = settings->otherConfigFlag;
651  message->h = settings->homeAgentFlag;
652  message->prf = settings->preference;
653  message->p = settings->proxyFlag;
654  message->reserved = 0;
655  message->routerLifetime = htons(routerLifetime);
656  message->reachableTime = htonl(settings->reachableTime);
657  message->retransTimer = htonl(settings->retransTimer);
658 
659  //If the Router Lifetime is zero, the preference value must be set to
660  //zero by the sender
661  if(routerLifetime == 0)
663 
664  //Length of the message, excluding any option
665  length = sizeof(NdpRouterAdvMessage);
666 
667 #if (ETH_SUPPORT == ENABLED)
668  //Point to the logical interface
669  logicalInterface = nicGetLogicalInterface(interface);
670 
671  //Check whether a MAC address has been assigned to the interface
672  if(!macCompAddr(&logicalInterface->macAddr, &MAC_UNSPECIFIED_ADDR))
673  {
674  //Add Source Link-Layer Address option
676  &logicalInterface->macAddr, sizeof(MacAddr));
677  }
678 #endif
679 
680  //A value of zero indicates that no MTU option is sent
681  if(settings->linkMtu > 0)
682  {
683  NdpMtuOption mtuOption;
684 
685  //The MTU option specifies the recommended MTU for the link
686  mtuOption.reserved = 0;
687  mtuOption.mtu = htonl(settings->linkMtu);
688 
689  //Add MTU option
691  (uint8_t *) &mtuOption + sizeof(NdpOption),
692  sizeof(NdpMtuOption) - sizeof(NdpOption));
693  }
694 
695  //Loop through the list of IPv6 prefixes
696  for(i = 0; i < settings->prefixListLength; i++)
697  {
698  NdpPrefixInfoOption prefixInfoOption;
699 
700  //The Prefix Information option provide hosts with on-link prefixes and
701  //prefixes for Address Autoconfiguration
702  prefixInfoOption.prefixLength = settings->prefixList[i].length;
703  prefixInfoOption.l = settings->prefixList[i].onLinkFlag;
704  prefixInfoOption.a = settings->prefixList[i].autonomousFlag;
705  prefixInfoOption.r = 0;
706  prefixInfoOption.reserved1 = 0;
707  prefixInfoOption.validLifetime = htonl(settings->prefixList[i].validLifetime);
708  prefixInfoOption.preferredLifetime = htonl(settings->prefixList[i].preferredLifetime);
709  prefixInfoOption.reserved2 = 0;
710  prefixInfoOption.prefix = settings->prefixList[i].prefix;
711 
712  //Add Prefix Information option (PIO)
714  (uint8_t *) &prefixInfoOption + sizeof(NdpOption),
715  sizeof(NdpPrefixInfoOption) - sizeof(NdpOption));
716  }
717 
718  //Loop through the list of routes
719  for(i = 0; i < settings->routeListLength; i++)
720  {
721  NdpRouteInfoOption routeInfoOption;
722 
723  //The Route Information option specifies prefixes that are reachable via
724  //the router
725  routeInfoOption.prefixLength = settings->routeList[i].length;
726  routeInfoOption.reserved1 = 0;
727  routeInfoOption.prf = settings->routeList[i].preference;
728  routeInfoOption.reserved2 = 0;
729  routeInfoOption.routeLifetime = htonl(settings->routeList[i].routeLifetime);
730  routeInfoOption.prefix = settings->routeList[i].prefix;
731 
732  //Add Route Information option (RIO)
734  (uint8_t *) &routeInfoOption + sizeof(NdpOption),
735  sizeof(NdpRouteInfoOption) - sizeof(NdpOption));
736  }
737 
738  //Loop through the list of 6LoWPAN compression contexts
739  for(i = 0; i < settings->contextListLength; i++)
740  {
741  NdpContextOption contextOption;
742 
743  //The 6LoWPAN Context option (6CO) carries prefix information for LoWPAN
744  //header compression
745  contextOption.contextLength = settings->contextList[i].length;
746  contextOption.reserved1 = 0;
747  contextOption.c = settings->contextList[i].compression;
748  contextOption.cid = settings->contextList[i].cid;
749  contextOption.reserved2 = 0;
750  contextOption.validLifetime = htons(settings->contextList[i].validLifetime);
751  contextOption.contextPrefix = settings->contextList[i].prefix;
752 
753  //Calculate the length of the option in bytes
754  n = sizeof(NdpContextOption) - sizeof(Ipv6Addr) + (contextOption.contextLength / 8);
755 
756  //Add 6LoWPAN Context option (6CO)
758  (uint8_t *) &contextOption + sizeof(NdpOption), n - sizeof(NdpOption));
759  }
760 
761  //Adjust the length of the multi-part buffer
762  netBufferSetLength(buffer, offset + length);
763 
764  //Format IPv6 pseudo header
765  pseudoHeader.length = htonl(length);
766  pseudoHeader.reserved = 0;
767  pseudoHeader.nextHeader = IPV6_ICMPV6_HEADER;
768 
769  //Calculate ICMPv6 header checksum
770  message->checksum = ipCalcUpperLayerChecksumEx(&pseudoHeader,
771  sizeof(Ipv6PseudoHeader), buffer, offset, length);
772 
773  //Total number of ICMP messages which this entity attempted to send
774  IP_MIB_INC_COUNTER32(icmpv6Stats.icmpStatsOutMsgs, 1);
775  //Increment per-message type ICMP counter
776  IP_MIB_INC_COUNTER32(icmpv6MsgStatsTable.icmpMsgStatsOutPkts[ICMPV6_TYPE_ROUTER_ADV], 1);
777 
778  //Debug message
779  TRACE_INFO("Sending Router Advertisement message (%" PRIuSIZE " bytes)...\r\n",
780  length);
781 
782  //Dump message contents for debugging purpose
784 
785  //Send Router Advertisement message
786  error = ipv6SendDatagram(interface, &pseudoHeader, buffer, offset, NDP_HOP_LIMIT);
787 
788  //Free previously allocated memory
789  netBufferFree(buffer);
790  //Return status code
791  return error;
792 }
793 
794 #endif
@ ICMPV6_TYPE_ROUTER_ADV
Definition: icmpv6.h:63
NdpNeighborCacheEntry * ndpFindNeighborCacheEntry(NetInterface *interface, const Ipv6Addr *ipAddr)
Search the Neighbor cache for a given IPv6 address.
Definition: ndp_cache.c:106
#define htons(value)
Definition: cpu_endian.h:392
IPv6 (Internet Protocol Version 6)
__start_packed struct @200 NdpRouteInfoOption
Route Information option (RIO)
uint8_t length
Definition: dtls_misc.h:149
#define NDP_MIN_DELAY_BETWEEN_RAS
Definition: ndp.h:102
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:637
@ NDP_ROUTER_SEL_PREFERENCE_MEDIUM
Definition: ndp.h:236
__start_packed struct @195 NdpOption
Neighbor Discovery option general format.
#define NDP_HOP_LIMIT
Definition: ndp.h:199
@ NDP_STATE_STALE
Definition: ndp.h:252
Helper functions for NDP (Neighbor Discovery Protocol)
NdpRouterAdvSettings settings
RA service settings.
void ndpRouterAdvGetDefaultSettings(NdpRouterAdvSettings *settings)
Initialize settings with default values.
@ NDP_OPT_MTU
Definition: ndp.h:220
#define IP_MIB_INC_COUNTER32(name, value)
Definition: ip_mib_module.h:46
void ndpDumpRouterSolMessage(const NdpRouterSolMessage *message)
Dump Router Solicitation message for debugging purpose.
Definition: ndp.c:1894
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
systime_t timestamp
Timestamp to manage retransmissions.
#define TRUE
Definition: os_port.h:50
void ndpAddOption(void *message, size_t *messageLen, uint8_t type, const void *value, size_t length)
Append an option to a NDP message.
Definition: ndp_misc.c:597
@ NDP_OPT_SOURCE_LINK_LAYER_ADDR
Definition: ndp.h:216
#define Ipv6Header
Definition: ipv6.h:36
bool_t running
This flag tells whether the RA service is running.
Neighbor cache entry.
Definition: ndp.h:544
uint_t routerAdvCount
Router Advertisement message counter.
error_t ipv6SendDatagram(NetInterface *interface, Ipv6PseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, uint_t flags)
Send an IPv6 datagram.
Definition: ipv6.c:1576
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:121
@ IPV6_ICMPV6_HEADER
Definition: ipv6.h:191
void ndpProcessRouterSol(NetInterface *interface, Ipv6PseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, uint8_t hopLimit)
Router Solicitation message processing.
__start_packed struct @190 NdpRouterSolMessage
Router Solicitation message.
#define timeCompare(t1, t2)
Definition: os_port.h:42
NdpRouterAdvRouteInfo * routeList
#define IPV6_DEFAULT_HOP_LIMIT
Definition: ipv6.h:59
#define NDP_DELAY_FIRST_PROBE_TIME
Definition: ndp.h:193
Router advertisement service.
systime_t timestamp
Timestamp to manage entry lifetime.
Definition: ndp.h:550
void ndpRouterAdvTick(NdpRouterAdvContext *context)
RA service timer handler.
#define FALSE
Definition: os_port.h:46
__start_packed struct @197 NdpPrefixInfoOption
Prefix Information option (PIO)
ICMPv6 (Internet Control Message Protocol Version 6)
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define htonl(value)
Definition: cpu_endian.h:393
systime_t ndpRouterAdvTickCounter
MacAddr macAddr
Link layer address associated with the IPv6 address.
Definition: ndp.h:548
error_t
Error codes.
Definition: error.h:42
uint16_t routerLifetime
Definition: ndp.h:304
#define Ipv6PseudoHeader
Definition: ipv6.h:42
error_t ipv6SelectSourceAddr(NetInterface **interface, const Ipv6Addr *destAddr, Ipv6Addr *srcAddr)
IPv6 source address selection.
Definition: ipv6_misc.c:882
void * netBufferAt(const NetBuffer *buffer, size_t offset)
Returns a pointer to the data at the specified position.
Definition: net_mem.c:413
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:777
NdpNeighborCacheEntry * ndpCreateNeighborCacheEntry(NetInterface *interface)
Create a new entry in the Neighbor cache.
Definition: ndp_cache.c:53
#define NDP_RETRANS_TIMER
Definition: ndp.h:186
const Ipv6Addr IPV6_LINK_LOCAL_ALL_NODES_ADDR
Definition: ipv6.c:75
__start_packed struct @203 NdpContextOption
6LoWPAN Context option (6CO)
#define NetInterface
Definition: net.h:36
error_t ndpRouterAdvInit(NdpRouterAdvContext *context, const NdpRouterAdvSettings *settings)
RA service initialization.
#define NDP_MAX_INITIAL_RTR_ADVERTISEMENTS
Definition: ndp.h:88
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:282
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:1782
uint_t ndpSendQueuedPackets(NetInterface *interface, NdpNeighborCacheEntry *entry)
Send packets that are waiting for address resolution.
Definition: ndp_cache.c:293
@ NDP_STATE_DELAY
Definition: ndp.h:253
Helper functions for IPv6.
error_t ndpSendRouterAdv(NdpRouterAdvContext *context, uint16_t routerLifetime)
Send a Router Advertisement message.
OsMutex netMutex
Definition: net.c:75
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:67
error_t ipv6LeaveMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Leave an IPv6 multicast group.
Definition: ipv6.c:2038
void ndpRouterAdvLinkChangeEvent(NdpRouterAdvContext *context)
Callback function for link change event.
__start_packed struct @179 Ipv6Addr
IPv6 network address.
#define NDP_MAX_RA_DELAY_TIME
Definition: ndp.h:109
error_t ipv6JoinMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Join an IPv6 multicast group.
Definition: ipv6.c:1928
#define TRACE_INFO(...)
Definition: debug.h:94
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
@ NDP_STATE_INCOMPLETE
Definition: ndp.h:250
#define MIN(a, b)
Definition: os_port.h:62
Neighbor and destination cache management.
NdpRouterAdvContextInfo * contextList
NDP (Neighbor Discovery Protocol)
int32_t netGetRandRange(int32_t min, int32_t max)
Get a random value in the specified range.
Definition: net.c:1841
#define NDP_MAX_INITIAL_RTR_ADVERT_INTERVAL
Definition: ndp.h:81
error_t ndpRouterAdvStart(NdpRouterAdvContext *context)
Start RA service.
#define TRACE_DEBUG(...)
Definition: debug.h:106
RA service context.
RA service settings.
__start_packed struct @191 NdpRouterAdvMessage
Router Advertisement message.
uint32_t time
uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader, size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP upper-layer checksum over a multi-part buffer.
Definition: ip.c:612
Ipv6Addr ipAddr
Unicast IPv6 address.
Definition: ndp.h:547
#define IPV6_DEFAULT_MTU
Definition: ipv6.h:109
IPv4 and IPv6 common routines.
uint8_t n
const Ipv6Addr IPV6_LINK_LOCAL_ALL_ROUTERS_ADDR
Definition: ipv6.c:79
IP MIB module.
@ NDP_OPT_ROUTE_INFORMATION
Definition: ndp.h:221
NdpState state
Reachability state.
Definition: ndp.h:546
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
void * ndpGetOption(uint8_t *options, size_t length, uint8_t type)
Find a specified option in a NDP message.
Definition: ndp_misc.c:645
@ NDP_OPT_PREFIX_INFORMATION
Definition: ndp.h:218
error_t netBufferSetLength(NetBuffer *buffer, size_t length)
Adjust the length of a multi-part buffer.
Definition: net_mem.c:320
NetInterface * interface
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:107
uint8_t message[]
Definition: chap.h:152
#define NDP_REACHABLE_TIME
Definition: ndp.h:179
__start_packed struct @196 NdpLinkLayerAddrOption
Source/Target Link-Layer Address option.
#define NDP_MAX_RTR_ADVERT_INTERVAL
Definition: ndp.h:74
error_t ndpRouterAdvStop(NdpRouterAdvContext *context)
Stop RA service.
error_t ndpCheckOptions(const uint8_t *options, size_t length)
Check NDP message options.
Definition: ndp_misc.c:687
systime_t timeout
Timeout value.
void ndpDumpRouterAdvMessage(const NdpRouterAdvMessage *message)
Dump Router Advertisement message for debugging purpose.
Definition: ndp.c:1908
@ IPV6_ADDR_STATE_PREFERRED
An address assigned to an interface whose use is unrestricted.
Definition: ipv6.h:173
#define PRIuSIZE
Definition: compiler_port.h:78
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:52
#define ipv6GetLinkLocalAddrState(interface)
Definition: ipv6.h:141
__start_packed struct @199 NdpMtuOption
MTU option.
uint32_t systime_t
Definition: compiler_port.h:46
@ NDP_OPT_6LOWPAN_CONTEXT
Definition: ndp.h:224
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:56
@ NO_ERROR
Success.
Definition: error.h:44
systime_t timeout
Timeout value.
Definition: ndp.h:551
Debugging facilities.
NdpRouterAdvPrefixInfo * prefixList
bool_t isRouter
A flag indicating whether the neighbor is a router or a host.
Definition: ndp.h:549
__start_packed struct @108 MacAddr
MAC address.
systime_t osGetSystemTime(void)
Retrieve system time.