ipv6cp.c
Go to the documentation of this file.
1 /**
2  * @file ipv6cp.c
3  * @brief IPV6CP (PPP IPv6 Control Protocol)
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 PPP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ipv6/ipv6.h"
37 #include "ipv6/ipv6_misc.h"
38 #include "ppp/ppp_fsm.h"
39 #include "ppp/ppp_misc.h"
40 #include "ppp/ppp_debug.h"
41 #include "ppp/ipv6cp.h"
42 #include "debug.h"
43 
44 //Check TCP/IP stack configuration
45 #if (PPP_SUPPORT == ENABLED && IPV6_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief IPV6CP FSM callbacks
50  **/
51 
53 {
67  NULL
68 };
69 
70 
71 /**
72  * @brief IPV6CP Open event
73  * @param[in] context PPP context
74  * @return Error code
75  **/
76 
78 {
79  //Debug message
80  TRACE_INFO("\r\nIPV6CP Open event\r\n");
81 
82  //The link is administratively available for traffic
83  pppOpenEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
84  //The lower layer is ready to carry packets
85  pppUpEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
86 
87  //Successful processing
88  return NO_ERROR;
89 }
90 
91 
92 /**
93  * @brief IPV6CP Close event
94  * @param[in] context PPP context
95  * @return Error code
96  **/
97 
99 {
100  //Debug message
101  TRACE_INFO("\r\nIPV6CP Close event\r\n");
102 
103  //The lower layer is no longer ready to carry packets
104  pppDownEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
105  //The link is no longer available for traffic
106  pppCloseEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
107 
108  //Successful processing
109  return NO_ERROR;
110 }
111 
112 
113 /**
114  * @brief IPV6CP timer handler
115  *
116  * This routine must be periodically called by the TCP/IP stack to
117  * manage retransmissions
118  *
119  * @param[in] context PPP context
120  **/
121 
122 void ipv6cpTick(PppContext *context)
123 {
124  //Check whether the restart timer is running
125  if(context->ipv6cpFsm.state >= PPP_STATE_4_CLOSING &&
126  context->ipv6cpFsm.state <= PPP_STATE_8_ACK_SENT)
127  {
128  //Get current time
130 
131  //Check restart timer
132  if((time - context->ipv6cpFsm.timestamp) >= PPP_RESTART_TIMER)
133  {
134  //Debug message
135  TRACE_INFO("\r\nIPV6CP Timeout event\r\n");
136 
137  //The restart timer is used to retransmit Configure-Request
138  //and Terminate-Request packets
139  pppTimeoutEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
140  }
141  }
142 }
143 
144 
145 /**
146  * @brief Process an incoming IPV6CP packet
147  * @param[in] context PPP context
148  * @param[in] packet IPV6CP packet received from the peer
149  * @param[in] length Length of the packet, in bytes
150  **/
151 
152 void ipv6cpProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
153 {
154  //Ensure the length of the incoming IPV6CP packet is valid
155  if(length < sizeof(PppPacket))
156  return;
157 
158  //Check the length field
159  if(ntohs(packet->length) > length)
160  return;
161  if(ntohs(packet->length) < sizeof(PppPacket))
162  return;
163 
164  //Save the length of the IPV6CP packet
165  length = ntohs(packet->length);
166 
167  //Debug message
168  TRACE_INFO("IPV6CP packet received (%" PRIuSIZE " bytes)...\r\n", length);
169  //Dump IPV6CP packet contents for debugging purpose
171 
172  //Check IPV6CP code field
173  switch(packet->code)
174  {
175  //Configure-Request packet?
177  //Process Configure-Request packet
178  ipv6cpProcessConfigureReq(context, (PppConfigurePacket *) packet);
179  break;
180  //Configure-Ack packet?
182  //Process Configure-Ack packet
183  ipv6cpProcessConfigureAck(context, (PppConfigurePacket *) packet);
184  break;
185  //Configure-Nak packet?
187  //Process Configure-Nak packet
188  ipv6cpProcessConfigureNak(context, (PppConfigurePacket *) packet);
189  break;
190  //Configure-Reject packet?
192  //Process Configure-Reject packet
194  break;
195  //Terminate-Request packet?
197  //Process Terminate-Request packet
198  ipv6cpProcessTerminateReq(context, (PppTerminatePacket *) packet);
199  break;
200  //Terminate-Ack packet?
202  //Process Terminate-Ack packet
203  ipv6cpProcessTerminateAck(context, (PppTerminatePacket *) packet);
204  break;
205  //Code-Reject packet?
206  case PPP_CODE_CODE_REJ:
207  //Process Code-Reject packet
208  ipv6cpProcessCodeRej(context, (PppCodeRejPacket *) packet);
209  break;
210  //Unknown code field
211  default:
212  //The packet is un-interpretable
213  ipv6cpProcessUnknownCode(context, packet);
214  break;
215  }
216 }
217 
218 
219 /**
220  * @brief Process Configure-Request packet
221  * @param[in] context PPP context
222  * @param[in] configureReqPacket Packet received from the peer
223  * @return Error code
224  **/
225 
227  const PppConfigurePacket *configureReqPacket)
228 {
229  error_t error;
230  size_t length;
231  bool_t notRecognizable;
232  bool_t notAcceptable;
233  PppOption *option;
234 
235  //Debug message
236  TRACE_INFO("\r\nIPV6CP Receive-Configure-Request event\r\n");
237 
238  //Initialize variables
239  error = NO_ERROR;
240  notRecognizable = FALSE;
241  notAcceptable = FALSE;
242 
243  //Retrieve the length of the option list
244  length = ntohs(configureReqPacket->length) - sizeof(PppConfigurePacket);
245  //Point to the first option
246  option = (PppOption *) configureReqPacket->options;
247 
248  //Parse configuration options
249  while(length > 0)
250  {
251  //Parse current option
252  error = ipv6cpParseOption(context, option, length, NULL);
253 
254  //Any error to report?
255  if(error == ERROR_INVALID_TYPE)
256  {
257  //Option not recognizable
258  notRecognizable = TRUE;
259  //Catch error
260  error = NO_ERROR;
261  }
262  else if(error == ERROR_INVALID_VALUE)
263  {
264  //Option not acceptable for configuration
265  notAcceptable = TRUE;
266  //Catch error
267  error = NO_ERROR;
268  }
269  else if(error)
270  {
271  //Malformed Configure-Request packet
272  break;
273  }
274 
275  //Remaining bytes to process
276  length -= option->length;
277  //Jump to the next option
278  option = (PppOption *) ((uint8_t *) option + option->length);
279  }
280 
281  //Valid Configure-Request packet received from the peer?
282  if(!error)
283  {
284  //Check flags
285  if(notRecognizable)
286  {
287  //If some configuration options received in the Configure-Request are not
288  //recognizable or not acceptable for negotiation, then the implementation
289  //must transmit a Configure-Reject
290  pppRcvConfigureReqEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks,
291  configureReqPacket, PPP_CODE_CONFIGURE_REJ);
292  }
293  else if(notAcceptable)
294  {
295  //If all configuration options are recognizable, but some values are not
296  //acceptable, then the implementation must transmit a Configure-Nak
297  pppRcvConfigureReqEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks,
298  configureReqPacket, PPP_CODE_CONFIGURE_NAK);
299  }
300  else
301  {
302  //If every configuration option received in the Configure-Request is
303  //recognizable and all values are acceptable, then the implementation
304  //must transmit a Configure-Ack
305  pppRcvConfigureReqEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks,
306  configureReqPacket, PPP_CODE_CONFIGURE_ACK);
307  }
308  }
309 
310  //Return status code
311  return error;
312 }
313 
314 
315 /**
316  * @brief Process Configure-Ack packet
317  * @param[in] context PPP context
318  * @param[in] configureAckPacket Packet received from the peer
319  * @return Error code
320  **/
321 
323  const PppConfigurePacket *configureAckPacket)
324 {
325  //Debug message
326  TRACE_INFO("\r\nIPV6CP Receive-Configure-Ack event\r\n");
327 
328  //When a packet is received with an invalid Identifier field, the
329  //packet is silently discarded without affecting the automaton
330  if(configureAckPacket->identifier != context->ipv6cpFsm.identifier)
331  return ERROR_WRONG_IDENTIFIER;
332 
333  //A valid Configure-Ack packet has been received from the peer
334  pppRcvConfigureAckEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
335 
336  //Successful processing
337  return NO_ERROR;
338 }
339 
340 
341 /**
342  * @brief Process Configure-Nak packet
343  * @param[in] context PPP context
344  * @param[in] configureNakPacket Packet received from the peer
345  * @return Error code
346  **/
347 
349  const PppConfigurePacket *configureNakPacket)
350 {
351  size_t length;
352  PppOption *option;
353 
354  //Debug message
355  TRACE_INFO("IPV6CP Receive-Configure-Nak event\r\n");
356 
357  //When a packet is received with an invalid Identifier field, the
358  //packet is silently discarded without affecting the automaton
359  if(configureNakPacket->identifier != context->ipv6cpFsm.identifier)
360  return ERROR_WRONG_IDENTIFIER;
361 
362  //Retrieve the length of the option list
363  length = ntohs(configureNakPacket->length) - sizeof(PppConfigurePacket);
364  //Point to the first option
365  option = (PppOption *) configureNakPacket->options;
366 
367  //Parse configuration options
368  while(length > 0)
369  {
370  //Check option length
371  if(option->length < sizeof(PppOption))
372  return ERROR_INVALID_LENGTH;
373  if(option->length > length)
374  return ERROR_INVALID_LENGTH;
375 
376  //Interface-Identifier option?
377  if(option->type == IPV6CP_OPTION_INTERFACE_ID)
378  {
379  //Cast option
380  Ipv6cpInterfaceIdOption *interfaceIdOption = (Ipv6cpInterfaceIdOption *) option;
381 
382  //Check option length
383  if(interfaceIdOption->length != sizeof(Ipv6cpInterfaceIdOption))
384  return ERROR_INVALID_LENGTH;
385 
386  //Save interface identifier
387  context->localConfig.interfaceId = interfaceIdOption->interfaceId;
388  }
389 
390  //Remaining bytes to process
391  length -= option->length;
392  //Jump to the next option
393  option = (PppOption *) ((uint8_t *) option + option->length);
394  }
395 
396  //A valid Configure-Nak or Configure-Reject packet has been received from the peer
397  pppRcvConfigureNakEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
398 
399  //Successful processing
400  return NO_ERROR;
401 }
402 
403 
404 /**
405  * @brief Process Configure-Reject packet
406  * @param[in] context PPP context
407  * @param[in] configureRejPacket Packet received from the peer
408  * @return Error code
409  **/
410 
412  const PppConfigurePacket *configureRejPacket)
413 {
414  size_t length;
415  PppOption *option;
416 
417  //Debug message
418  TRACE_INFO("\r\nIPV6CP Receive-Configure-Reject event\r\n");
419 
420  //When a packet is received with an invalid Identifier field, the
421  //packet is silently discarded without affecting the automaton
422  if(configureRejPacket->identifier != context->ipv6cpFsm.identifier)
423  return ERROR_WRONG_IDENTIFIER;
424 
425  //Retrieve the length of the option list
426  length = ntohs(configureRejPacket->length) - sizeof(PppConfigurePacket);
427  //Point to the first option
428  option = (PppOption *) configureRejPacket->options;
429 
430  //Parse configuration options
431  while(length > 0)
432  {
433  //Check option length
434  if(option->length < sizeof(PppOption))
435  return ERROR_INVALID_LENGTH;
436  if(option->length > length)
437  return ERROR_INVALID_LENGTH;
438 
439  //Interface-Identifier option?
440  if(option->type == IPV6CP_OPTION_INTERFACE_ID)
441  {
442  //The option is not recognized by the peer
443  context->localConfig.interfaceIdRejected = TRUE;
444  }
445 
446  //Remaining bytes to process
447  length -= option->length;
448  //Jump to the next option
449  option = (PppOption *) ((uint8_t *) option + option->length);
450  }
451 
452  //A valid Configure-Nak or Configure-Reject packet has been received from the peer
453  pppRcvConfigureNakEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
454 
455  //Successful processing
456  return NO_ERROR;
457 }
458 
459 
460 /**
461  * @brief Process Terminate-Request packet
462  * @param[in] context PPP context
463  * @param[in] terminateReqPacket Packet received from the peer
464  * @return Error code
465  **/
466 
468  const PppTerminatePacket *terminateReqPacket)
469 {
470  //Debug message
471  TRACE_INFO("\r\nIPV6CP Receive-Terminate-Request event\r\n");
472 
473  //The Terminate-Request indicates the desire of the peer to close the connection
474  pppRcvTerminateReqEvent(context, &context->ipv6cpFsm,
475  &ipv6cpCallbacks, terminateReqPacket);
476 
477  //Successful processing
478  return NO_ERROR;
479 }
480 
481 
482 /**
483  * @brief Process Terminate-Ack packet
484  * @param[in] context PPP context
485  * @param[in] terminateAckPacket Packet received from the peer
486  * @return Error code
487  **/
488 
490  const PppTerminatePacket *terminateAckPacket)
491 {
492  //Debug message
493  TRACE_INFO("\r\nIPV6CP Receive-Terminate-Ack event\r\n");
494 
495  //The Terminate-Ack packet is usually a response to a Terminate-Request
496  //packet. This packet may also indicate that the peer is in Closed or
497  //Stopped states, and serves to re-synchronize the link configuration
498  pppRcvTerminateAckEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks);
499 
500  //Successful processing
501  return NO_ERROR;
502 }
503 
504 
505 /**
506  * @brief Process Code-Reject packet
507  * @param[in] context PPP context
508  * @param[in] codeRejPacket Packet received from the peer
509  * @return Error code
510  **/
511 
513  const PppCodeRejPacket *codeRejPacket)
514 {
515  size_t length;
516  PppPacket *packet;
517 
518  //Debug message
519  TRACE_INFO("\r\nIPV6CP Receive-Code-Reject event\r\n");
520 
521  //Point to the rejected packet
522  packet = (PppPacket *) codeRejPacket->rejectedPacket;
523  //Retrieve the length of the rejected packet
524  length = ntohs(codeRejPacket->length) - sizeof(PppCodeRejPacket);
525 
526  //Make sure the length of the rejected packet is valid
527  if(length < sizeof(PppPacket))
528  return ERROR_INVALID_LENGTH;
529 
530  //Check whether the rejected value is acceptable or catastrophic
531  if(packet->code < PPP_CODE_CONFIGURE_REQ ||
532  packet->code > PPP_CODE_CODE_REJ)
533  {
534  //The RXJ+ event arises when the rejected value is acceptable, such
535  //as a Code-Reject of an extended code, or a Protocol-Reject of a
536  //NCP. These are within the scope of normal operation
537  pppRcvCodeRejEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks, TRUE);
538  }
539  else
540  {
541  //The RXJ- event arises when the rejected value is catastrophic, such
542  //as a Code-Reject of Configure-Request! This event communicates an
543  //unrecoverable error that terminates the connection
544  pppRcvCodeRejEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks, FALSE);
545  }
546 
547  //Successful processing
548  return NO_ERROR;
549 }
550 
551 
552 /**
553  * @brief Process packet with unknown code
554  * @param[in] context PPP context
555  * @param[in] packet Un-interpretable packet received from the peer
556  * @return Error code
557  **/
558 
560  const PppPacket *packet)
561 {
562  //Debug message
563  TRACE_INFO("\r\nIPV6CP Receive-Unknown-Code event\r\n");
564 
565  //This event occurs when an un-interpretable packet is received from
566  //the peer. A Code-Reject packet is sent in response
567  pppRcvUnknownCodeEvent(context, &context->ipv6cpFsm, &ipv6cpCallbacks, packet);
568 
569  //Successful processing
570  return NO_ERROR;
571 }
572 
573 
574 /**
575  * @brief This-Layer-Up callback function
576  * @param[in] context PPP context
577  **/
578 
580 {
581  NetInterface *interface;
583 
584  //Debug message
585  TRACE_INFO("IPV6CP This-Layer-Up callback\r\n");
586 
587  //Debug message
588  TRACE_INFO(" Local Interface Id = %s\r\n",
589  eui64AddrToString(&context->localConfig.interfaceId, NULL));
590  TRACE_INFO(" Peer Interface Id = %s\r\n",
591  eui64AddrToString(&context->peerConfig.interfaceId, NULL));
592 
593  //Point to the underlying interface
594  interface = context->interface;
595 
596  //Generate an IPv6 address from the local interface identifier
597  ipv6GenerateLinkLocalAddr(&context->localConfig.interfaceId, &ipAddr);
598 
599  //Update IPv6 configuration
602 
603  //Generate an IPv6 address from the remote interface identifier
604  ipv6GenerateLinkLocalAddr(&context->peerConfig.interfaceId, &ipAddr);
605 
606  //Update IPv6 configuration
607  interface->ipv6Context.routerList[0].addr = ipAddr;
608  interface->ipv6Context.routerList[0].permanent = TRUE;
609 
610  //Link is up
611  interface->linkState = TRUE;
612 
613  //Disable interrupts
614  interface->nicDriver->disableIrq(interface);
615  //Process link state change event
616  nicNotifyLinkChange(interface);
617  //Re-enable interrupts
618  interface->nicDriver->enableIrq(interface);
619 }
620 
621 
622 /**
623  * @brief This-Layer-Down callback function
624  * @param[in] context PPP context
625  **/
626 
628 {
629  NetInterface *interface;
630 
631  //Debug message
632  TRACE_INFO("IPV6CP This-Layer-Down callback\r\n");
633 
634  //Point to the underlying interface
635  interface = context->interface;
636 
637  //Link is up
638  interface->linkState = FALSE;
639 
640  //Disable interrupts
641  interface->nicDriver->disableIrq(interface);
642  //Process link state change event
643  nicNotifyLinkChange(interface);
644  //Re-enable interrupts
645  interface->nicDriver->enableIrq(interface);
646 }
647 
648 
649 /**
650  * @brief This-Layer-Started callback function
651  * @param[in] context PPP context
652  **/
653 
655 {
656  //Debug message
657  TRACE_INFO("IPV6CP This-Layer-Started callback\r\n");
658 }
659 
660 
661 /**
662  * @brief This-Layer-Finished callback function
663  * @param[in] context PPP context
664  **/
665 
667 {
668  //Debug message
669  TRACE_INFO("IPV6CP This-Layer-Finished callback\r\n");
670 }
671 
672 
673 /**
674  * @brief Initialize-Restart-Count callback function
675  * @param[in] context PPP context
676  * @param[in] value Restart counter value
677  **/
678 
680 {
681  //Debug message
682  TRACE_INFO("IPV6CP Initialize-Restart-Count callback\r\n");
683 
684  //Initialize restart counter
685  context->ipv6cpFsm.restartCounter = value;
686 }
687 
688 
689 /**
690  * @brief Zero-Restart-Count callback function
691  * @param[in] context PPP context
692  **/
693 
695 {
696  //Debug message
697  TRACE_INFO("IPV6CP Zero-Restart-Count callback\r\n");
698 
699  //Zero restart counter
700  context->ipv6cpFsm.restartCounter = 0;
701 
702  //The receiver of a Terminate-Request should wait for the peer to
703  //disconnect, and must not disconnect until at least one Restart
704  //time has passed after sending a Terminate-Ack
705  context->ipv6cpFsm.timestamp = osGetSystemTime();
706 }
707 
708 
709 /**
710  * @brief Send-Configure-Request callback function
711  * @param[in] context PPP context
712  * @return Error code
713  **/
714 
716 {
717  error_t error;
718  size_t length;
719  size_t offset;
720  NetBuffer *buffer;
721  PppConfigurePacket *configureReqPacket;
722 
723  //Debug message
724  TRACE_INFO("IPV6CP Send-Configure-Request callback\r\n");
725 
726  //Allocate a buffer memory to hold the Configure-Request packet
727  buffer = pppAllocBuffer(PPP_MAX_CONF_REQ_SIZE, &offset);
728  //Failed to allocate memory?
729  if(buffer == NULL)
730  return ERROR_OUT_OF_MEMORY;
731 
732  //Point to the Configure-Request packet
733  configureReqPacket = netBufferAt(buffer, offset);
734 
735  //Format packet header
736  configureReqPacket->code = PPP_CODE_CONFIGURE_REQ;
737  configureReqPacket->identifier = ++context->ipv6cpFsm.identifier;
738  configureReqPacket->length = sizeof(PppConfigurePacket);
739 
740  //Make sure the Interface-Identifier option has not been previously rejected
741  if(!context->localConfig.interfaceIdRejected)
742  {
743  //Add option
744  pppAddOption(configureReqPacket, IPV6CP_OPTION_INTERFACE_ID,
745  &context->localConfig.interfaceId, sizeof(Eui64));
746  }
747 
748  //Save packet length
749  length = configureReqPacket->length;
750  //Convert length field to network byte order
751  configureReqPacket->length = htons(length);
752 
753  //Adjust the length of the multi-part buffer
754  netBufferSetLength(buffer, offset + length);
755 
756  //Debug message
757  TRACE_INFO("Sending Configure-Request packet (%" PRIuSIZE " bytes)...\r\n", length);
758  //Dump packet contents for debugging purpose
759  pppDumpPacket((PppPacket *) configureReqPacket, length, PPP_PROTOCOL_IPV6CP);
760 
761  //Send PPP frame
762  error = pppSendFrame(context->interface, buffer, offset, PPP_PROTOCOL_IPV6CP);
763 
764  //The restart counter is decremented each time a Configure-Request is sent
765  if(context->ipv6cpFsm.restartCounter > 0)
766  context->ipv6cpFsm.restartCounter--;
767 
768  //Save the time at which the packet was sent
769  context->ipv6cpFsm.timestamp = osGetSystemTime();
770 
771  //Free previously allocated memory block
772  netBufferFree(buffer);
773  //Return status code
774  return error;
775 }
776 
777 
778 /**
779  * @brief Send-Configure-Ack callback function
780  * @param[in] context PPP context
781  * @param[in] configureReqPacket Configure-Request packet received from the peer
782  * @return Error code
783  **/
784 
786  const PppConfigurePacket *configureReqPacket)
787 {
788  //Debug message
789  TRACE_INFO("IPV6CP Send-Configure-Ack callback\r\n");
790 
791  //Send Configure-Ack packet
792  return pppSendConfigureAckNak(context, configureReqPacket,
794 }
795 
796 
797 /**
798  * @brief Send-Configure-Nak callback function
799  * @param[in] context PPP context
800  * @param[in] configureReqPacket Configure-Request packet received from the peer
801  * @return Error code
802  **/
803 
805  const PppConfigurePacket *configureReqPacket)
806 {
807  //Debug message
808  TRACE_INFO("IPV6CP Send-Configure-Nak callback\r\n");
809 
810  //Send Configure-Nak packet
811  return pppSendConfigureAckNak(context, configureReqPacket,
813 }
814 
815 
816 /**
817  * @brief Send-Configure-Reject callback function
818  * @param[in] context PPP context
819  * @param[in] configureReqPacket Configure-Request packet received from the peer
820  * @return Error code
821  **/
822 
824  const PppConfigurePacket *configureReqPacket)
825 {
826  //Debug message
827  TRACE_INFO("IPV6CP Send-Configure-Reject callback\r\n");
828 
829  //Send Configure-Reject packet
830  return pppSendConfigureAckNak(context, configureReqPacket,
832 }
833 
834 
835 /**
836  * @brief Send-Terminate-Request callback function
837  * @param[in] context PPP context
838  * @return Error code
839  **/
840 
842 {
843  error_t error;
844 
845  //Debug message
846  TRACE_INFO("IPV6CP Send-Terminate-Request callback\r\n");
847 
848  //On transmission, the Identifier field must be changed
849  context->ipv6cpFsm.identifier++;
850 
851  //Send Terminate-Request packet
852  error = pppSendTerminateReq(context, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
853 
854  //The restart counter is decremented each time a Terminate-Request is sent
855  if(context->ipv6cpFsm.restartCounter > 0)
856  context->ipv6cpFsm.restartCounter--;
857 
858  //Save the time at which the packet was sent
859  context->ipv6cpFsm.timestamp = osGetSystemTime();
860 
861  //Return status code
862  return error;
863 }
864 
865 
866 /**
867  * @brief Send-Terminate-Ack callback function
868  * @param[in] context PPP context
869  * @param[in] terminateReqPacket Terminate-Request packet received from the peer
870  * @return Error code
871  **/
872 
874  const PppTerminatePacket *terminateReqPacket)
875 {
876  uint8_t identifier;
877 
878  //Debug message
879  TRACE_INFO("IPV6CP Send-Terminate-Ack callback\r\n");
880 
881  //Check whether this Terminate-Ack acknowledges the reception of a
882  //Terminate-Request packet
883  if(terminateReqPacket != NULL)
884  {
885  //The Identifier field of the Terminate-Request is copied into the
886  //Identifier field of the Terminate-Ack packet
887  identifier = terminateReqPacket->identifier;
888  }
889  else
890  {
891  //This Terminate-Ack packet serves to synchronize the automatons
892  identifier = ++context->ipv6cpFsm.identifier;
893  }
894 
895  //Send Terminate-Ack packet
897 }
898 
899 
900 /**
901  * @brief Send-Code-Reject callback function
902  * @param[in] context PPP context
903  * @param[in] packet Un-interpretable packet received from the peer
904  * @return Error code
905  **/
906 
908 {
909  //Debug message
910  TRACE_INFO("IPV6CP Send-Code-Reject callback\r\n");
911 
912  //The Identifier field must be changed for each Code-Reject sent
913  context->ipv6cpFsm.identifier++;
914 
915  //Send Code-Reject packet
916  return pppSendCodeRej(context, packet, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
917 }
918 
919 
920 /**
921  * @brief Parse IPV6CP configuration option
922  * @param[in] context PPP context
923  * @param[in] option Option to be checked
924  * @param[in] inPacketLen Remaining bytes to process in the incoming packet
925  * @param[out] outPacket Pointer to the Configure-Ack, Nak or Reject packet
926  * @return Error code
927  **/
928 
930  size_t inPacketLen, PppConfigurePacket *outPacket)
931 {
932  error_t error;
933 
934  //Malformed IPV6CP packet?
935  if(inPacketLen < sizeof(PppOption))
936  return ERROR_INVALID_LENGTH;
937 
938  //Check option length
939  if(option->length < sizeof(PppOption))
940  return ERROR_INVALID_LENGTH;
941  if(option->length > inPacketLen)
942  return ERROR_INVALID_LENGTH;
943 
944  //Check option type
945  switch(option->type)
946  {
948  //Check Interface-Identifier option
949  error = ipv6cpParseInterfaceIdOption(context, (Ipv6cpInterfaceIdOption *) option, outPacket);
950  break;
951  default:
952  //If some configuration options received in the Configure-Request are not
953  //recognizable or not acceptable for negotiation, then the implementation
954  //must transmit a Configure-Reject
955  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_REJ)
956  {
957  //The options field of the Configure-Reject packet is filled
958  //with the unrecognized options from the Configure-Request
959  pppAddOption(outPacket, option->type, option->data,
960  option->length - sizeof(PppOption));
961  }
962 
963  //The option is not acceptable for negotiation
964  error = ERROR_INVALID_TYPE;
965  break;
966  }
967 
968  //Return status code
969  return error;
970 }
971 
972 
973 /**
974  * @brief Parse Interface-Identifier option
975  * @param[in] context PPP context
976  * @param[in] option Option to be checked
977  * @param[out] outPacket Pointer to the Configure-Nak or Configure-Reject packet
978  * @return Error code
979  **/
980 
982  Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
983 {
984  error_t error;
985 
986  //Check length field
987  if(option->length == sizeof(Ipv6cpInterfaceIdOption))
988  {
989  //Check whether the option value is acceptable
990  if(!eui64CompAddr(&option->interfaceId, &EUI64_UNSPECIFIED_ADDR))
991  {
992  //If every configuration option received in the Configure-Request is
993  //recognizable and all values are acceptable, then the implementation
994  //must transmit a Configure-Ack
995  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_ACK)
996  {
997  //Save interface identifier
998  context->peerConfig.interfaceId = option->interfaceId;
999 
1000  //The options field of the Configure-Ack packet contains the
1001  //configuration options that the sender is acknowledging
1003  &option->interfaceId, option->length - sizeof(PppOption));
1004  }
1005 
1006  //The value is acceptable
1007  error = NO_ERROR;
1008  }
1009  else
1010  {
1011  //If all configuration options are recognizable, but some values are not
1012  //acceptable, then the implementation must transmit a Configure-Nak
1013  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_NAK)
1014  {
1015  //The option must be modified to a value acceptable to the
1016  //Configure-Nak sender
1018  &context->peerConfig.interfaceId, sizeof(Eui64));
1019  }
1020 
1021  //The value is not acceptable
1022  error = ERROR_INVALID_VALUE;
1023  }
1024  }
1025  else
1026  {
1027  //Invalid length field
1028  error = ERROR_INVALID_LENGTH;
1029  }
1030 
1031  //Return status code
1032  return error;
1033 }
1034 
1035 #endif
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:941
#define htons(value)
Definition: cpu_endian.h:392
void ipv6cpZeroRestartCount(PppContext *context)
Zero-Restart-Count callback function.
Definition: ipv6cp.c:694
IPv6 (Internet Protocol Version 6)
uint8_t length
Definition: dtls_misc.h:149
Data logging functions for debugging purpose (PPP)
@ PPP_CODE_CONFIGURE_REJ
Configure-Reject.
Definition: ppp.h:219
int bool_t
Definition: compiler_port.h:49
#define PppPacket
Definition: ppp.h:37
void pppRcvConfigureNakEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Receive-Configure-Nak event.
Definition: ppp_fsm.c:586
error_t ipv6cpSendConfigureReq(PppContext *context)
Send-Configure-Request callback function.
Definition: ipv6cp.c:715
@ PPP_CODE_CONFIGURE_ACK
Configure-Ack.
Definition: ppp.h:217
void pppRcvUnknownCodeEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks, const PppPacket *packet)
Process Receive-Unknown-Code event.
Definition: ppp_fsm.c:757
__start_packed struct @286 PppTerminatePacket
Terminate-Request and Terminate-Ack packet.
error_t ipv6cpParseOption(PppContext *context, PppOption *option, size_t inPacketLen, PppConfigurePacket *outPacket)
Parse IPV6CP configuration option.
Definition: ipv6cp.c:929
__start_packed struct @109 Eui64
EUI-64 identifier.
error_t ipv6cpSendTerminateReq(PppContext *context)
Send-Terminate-Request callback function.
Definition: ipv6cp.c:841
error_t ipv6cpParseInterfaceIdOption(PppContext *context, Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
Parse Interface-Identifier option.
Definition: ipv6cp.c:981
void pppUpEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Up event.
Definition: ppp_fsm.c:50
void pppRcvCodeRejEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks, bool_t acceptable)
Process Receive-Code-Reject or Receive-Protocol-Reject event.
Definition: ppp_fsm.c:792
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
PPP finite state machine.
#define TRUE
Definition: os_port.h:50
error_t ipv6cpClose(PppContext *context)
IPV6CP Close event.
Definition: ipv6cp.c:98
IPV6CP (PPP IPv6 Control Protocol)
void ipv6cpThisLayerFinished(PppContext *context)
This-Layer-Finished callback function.
Definition: ipv6cp.c:666
const PppCallbacks ipv6cpCallbacks
IPV6CP FSM callbacks.
Definition: ipv6cp.c:52
__start_packed struct @291 PppOption
LCP/NCP option.
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
NetBuffer * pppAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold a PPP frame.
Definition: ppp.c:1265
#define NDP_INFINITE_LIFETIME
Definition: ndp.h:202
void pppCloseEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Close event.
Definition: ppp_fsm.c:189
void ipv6cpThisLayerStarted(PppContext *context)
This-Layer-Started callback function.
Definition: ipv6cp.c:654
#define PppContext
Definition: ppp.h:38
void pppOpenEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Open event.
Definition: ppp_fsm.c:135
error_t pppSendCodeRej(PppContext *context, const PppPacket *packet, uint8_t identifier, PppProtocol protocol)
Send Code-Reject packet.
Definition: ppp_misc.c:265
PPP FSM actions.
Definition: ppp_fsm.h:152
error_t ipv6cpProcessConfigureAck(PppContext *context, const PppConfigurePacket *configureAckPacket)
Process Configure-Ack packet.
Definition: ipv6cp.c:322
error_t ipv6cpSendConfigureNak(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Nak callback function.
Definition: ipv6cp.c:804
void ipv6cpThisLayerDown(PppContext *context)
This-Layer-Down callback function.
Definition: ipv6cp.c:627
error_t ipv6cpSendCodeRej(PppContext *context, const PppPacket *packet)
Send-Code-Reject callback function.
Definition: ipv6cp.c:907
@ PPP_CODE_CODE_REJ
Code-Reject.
Definition: ppp.h:222
error_t pppAddOption(PppConfigurePacket *packet, uint8_t optionType, const void *optionValue, uint8_t optionLen)
Add an option to a Configure packet.
Definition: ppp_misc.c:448
@ PPP_CODE_CONFIGURE_REQ
Configure-Request.
Definition: ppp.h:216
__start_packed struct @273 Ipv6cpInterfaceIdOption
Interface-Identifier option.
error_t ipv6cpProcessTerminateAck(PppContext *context, const PppTerminatePacket *terminateAckPacket)
Process Terminate-Ack packet.
Definition: ipv6cp.c:489
#define FALSE
Definition: os_port.h:46
#define eui64CompAddr(eui64Addr1, eui64Addr2)
Definition: ethernet.h:116
error_t
Error codes.
Definition: error.h:42
error_t ipv6cpProcessCodeRej(PppContext *context, const PppCodeRejPacket *codeRejPacket)
Process Code-Reject packet.
Definition: ipv6cp.c:512
void ipv6cpThisLayerUp(PppContext *context)
This-Layer-Up callback function.
Definition: ipv6cp.c:579
void * netBufferAt(const NetBuffer *buffer, size_t offset)
Returns a pointer to the data at the specified position.
Definition: net_mem.c:413
void pppRcvConfigureAckEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Receive-Configure-Ack event.
Definition: ppp_fsm.c:524
uint8_t identifier[]
const Eui64 EUI64_UNSPECIFIED_ADDR
Definition: ethernet.c:839
error_t ipv6cpProcessConfigureNak(PppContext *context, const PppConfigurePacket *configureNakPacket)
Process Configure-Nak packet.
Definition: ipv6cp.c:348
#define NetInterface
Definition: net.h:36
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:282
@ ERROR_INVALID_LENGTH
Definition: error.h:109
Helper functions for IPv6.
#define PPP_RESTART_TIMER
Definition: ppp.h:96
@ PPP_CODE_CONFIGURE_NAK
Configure-Nak.
Definition: ppp.h:218
__start_packed struct @179 Ipv6Addr
IPv6 network address.
@ ERROR_INVALID_TYPE
Definition: error.h:113
#define TRACE_INFO(...)
Definition: debug.h:94
void pppRcvTerminateReqEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks, const PppTerminatePacket *terminateReqPacket)
Process Receive-Terminate-Req event.
Definition: ppp_fsm.c:648
void ipv6cpInitRestartCount(PppContext *context, uint_t value)
Initialize-Restart-Count callback function.
Definition: ipv6cp.c:679
@ PPP_PROTOCOL_IPV6CP
IPv6 Control Protocol.
Definition: ppp.h:202
error_t ipv6cpProcessConfigureReject(PppContext *context, const PppConfigurePacket *configureRejPacket)
Process Configure-Reject packet.
Definition: ipv6cp.c:411
error_t ipv6cpProcessConfigureReq(PppContext *context, const PppConfigurePacket *configureReqPacket)
Process Configure-Request packet.
Definition: ipv6cp.c:226
@ IPV6CP_OPTION_INTERFACE_ID
Interface-Identifier.
Definition: ipv6cp.h:50
void ipv6cpProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
Process an incoming IPV6CP packet.
Definition: ipv6cp.c:152
error_t pppSendTerminateAck(PppContext *context, uint8_t identifier, PppProtocol protocol)
Send Terminate-Ack packet.
Definition: ppp_misc.c:215
@ PPP_STATE_8_ACK_SENT
Definition: ppp.h:188
@ PPP_CODE_TERMINATE_ACK
Terminate-Ack.
Definition: ppp.h:221
#define ntohs(value)
Definition: cpu_endian.h:398
error_t ipv6SetAddr(NetInterface *interface, uint_t index, const Ipv6Addr *addr, Ipv6AddrState state, systime_t validLifetime, systime_t preferredLifetime, bool_t permanent)
Set IPv6 address and address state.
Definition: ipv6_misc.c:95
__start_packed struct @285 PppConfigurePacket
Configure-Request, Configure-Ack, Configure-Nak and Configure-Reject packets.
uint32_t time
error_t pppSendFrame(NetInterface *interface, NetBuffer *buffer, size_t offset, uint16_t protocol)
Send a PPP frame.
Definition: ppp.c:1006
void pppRcvConfigureReqEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks, const PppConfigurePacket *configureReqPacket, PppCode code)
Process Receive-Configure-Request event.
Definition: ppp_fsm.c:335
@ ERROR_INVALID_VALUE
Definition: error.h:114
void ipv6cpTick(PppContext *context)
IPV6CP timer handler.
Definition: ipv6cp.c:122
error_t pppDumpPacket(const PppPacket *packet, size_t length, PppProtocol protocol)
Dump LCP/NCP packet for debugging purpose.
Definition: ppp_debug.c:143
PPP miscellaneous functions.
error_t ipv6cpSendTerminateAck(PppContext *context, const PppTerminatePacket *terminateReqPacket)
Send-Terminate-Ack callback function.
Definition: ipv6cp.c:873
error_t pppSendConfigureAckNak(PppContext *context, const PppConfigurePacket *configureReqPacket, PppProtocol protocol, PppCode code)
Send Configure-Ack, Nak or Reject packet.
Definition: ppp_misc.c:56
void ipv6GenerateLinkLocalAddr(const Eui64 *interfaceId, Ipv6Addr *ipAddr)
Generate a IPv6 link-local address from an interface identifier.
Definition: ipv6_misc.c:1455
error_t ipv6cpProcessTerminateReq(PppContext *context, const PppTerminatePacket *terminateReqPacket)
Process Terminate-Request packet.
Definition: ipv6cp.c:467
error_t netBufferSetLength(NetBuffer *buffer, size_t length)
Adjust the length of a multi-part buffer.
Definition: net_mem.c:320
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:88
error_t ipv6cpProcessUnknownCode(PppContext *context, const PppPacket *packet)
Process packet with unknown code.
Definition: ipv6cp.c:559
error_t ipv6cpOpen(PppContext *context)
IPV6CP Open event.
Definition: ipv6cp.c:77
error_t pppSendTerminateReq(PppContext *context, uint8_t identifier, PppProtocol protocol)
Send Terminate-Request packet.
Definition: ppp_misc.c:166
@ PPP_STATE_4_CLOSING
Definition: ppp.h:184
@ IPV6_ADDR_STATE_PREFERRED
An address assigned to an interface whose use is unrestricted.
Definition: ipv6.h:173
void pppDownEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Down event.
Definition: ppp_fsm.c:84
uint8_t value[]
Definition: dtls_misc.h:150
#define PRIuSIZE
Definition: compiler_port.h:78
void pppRcvTerminateAckEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Receive-Terminate-Ack event.
Definition: ppp_fsm.c:697
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
#define PPP_MAX_CONF_REQ_SIZE
Definition: ppp.h:140
uint32_t systime_t
Definition: compiler_port.h:46
uint8_t ipAddr[4]
Definition: mib_common.h:187
error_t ipv6cpSendConfigureAck(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Ack callback function.
Definition: ipv6cp.c:785
error_t ipv6cpSendConfigureRej(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Reject callback function.
Definition: ipv6cp.c:823
@ NO_ERROR
Success.
Definition: error.h:44
@ PPP_CODE_TERMINATE_REQ
Terminate-Request.
Definition: ppp.h:220
Debugging facilities.
void pppTimeoutEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Timeout event.
Definition: ppp_fsm.c:257
__start_packed struct @287 PppCodeRejPacket
Code-Reject packet.
systime_t osGetSystemTime(void)
Retrieve system time.