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-2023 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.2.2
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  //Set the EUI-64 interface identifier
597  interface->eui64 = context->localConfig.interfaceId;
598  //Link is up
599  interface->linkState = TRUE;
600 
601  //Disable interrupts
602  interface->nicDriver->disableIrq(interface);
603  //Process link state change event
604  nicNotifyLinkChange(interface);
605  //Re-enable interrupts
606  interface->nicDriver->enableIrq(interface);
607 }
608 
609 
610 /**
611  * @brief This-Layer-Down callback function
612  * @param[in] context PPP context
613  **/
614 
616 {
617  NetInterface *interface;
618 
619  //Debug message
620  TRACE_INFO("IPV6CP This-Layer-Down callback\r\n");
621 
622  //Point to the underlying interface
623  interface = context->interface;
624 
625  //Link is up
626  interface->linkState = FALSE;
627 
628  //Disable interrupts
629  interface->nicDriver->disableIrq(interface);
630  //Process link state change event
631  nicNotifyLinkChange(interface);
632  //Re-enable interrupts
633  interface->nicDriver->enableIrq(interface);
634 }
635 
636 
637 /**
638  * @brief This-Layer-Started callback function
639  * @param[in] context PPP context
640  **/
641 
643 {
644  //Debug message
645  TRACE_INFO("IPV6CP This-Layer-Started callback\r\n");
646 }
647 
648 
649 /**
650  * @brief This-Layer-Finished callback function
651  * @param[in] context PPP context
652  **/
653 
655 {
656  //Debug message
657  TRACE_INFO("IPV6CP This-Layer-Finished callback\r\n");
658 }
659 
660 
661 /**
662  * @brief Initialize-Restart-Count callback function
663  * @param[in] context PPP context
664  * @param[in] value Restart counter value
665  **/
666 
668 {
669  //Debug message
670  TRACE_INFO("IPV6CP Initialize-Restart-Count callback\r\n");
671 
672  //Initialize restart counter
673  context->ipv6cpFsm.restartCounter = value;
674 }
675 
676 
677 /**
678  * @brief Zero-Restart-Count callback function
679  * @param[in] context PPP context
680  **/
681 
683 {
684  //Debug message
685  TRACE_INFO("IPV6CP Zero-Restart-Count callback\r\n");
686 
687  //Zero restart counter
688  context->ipv6cpFsm.restartCounter = 0;
689 
690  //The receiver of a Terminate-Request should wait for the peer to
691  //disconnect, and must not disconnect until at least one Restart
692  //time has passed after sending a Terminate-Ack
693  context->ipv6cpFsm.timestamp = osGetSystemTime();
694 }
695 
696 
697 /**
698  * @brief Send-Configure-Request callback function
699  * @param[in] context PPP context
700  * @return Error code
701  **/
702 
704 {
705  error_t error;
706  size_t length;
707  size_t offset;
708  NetBuffer *buffer;
709  PppConfigurePacket *configureReqPacket;
710 
711  //Debug message
712  TRACE_INFO("IPV6CP Send-Configure-Request callback\r\n");
713 
714  //Allocate a buffer memory to hold the Configure-Request packet
715  buffer = pppAllocBuffer(PPP_MAX_CONF_REQ_SIZE, &offset);
716  //Failed to allocate memory?
717  if(buffer == NULL)
718  return ERROR_OUT_OF_MEMORY;
719 
720  //Point to the Configure-Request packet
721  configureReqPacket = netBufferAt(buffer, offset);
722 
723  //Format packet header
724  configureReqPacket->code = PPP_CODE_CONFIGURE_REQ;
725  configureReqPacket->identifier = ++context->ipv6cpFsm.identifier;
726  configureReqPacket->length = sizeof(PppConfigurePacket);
727 
728  //Make sure the Interface-Identifier option has not been previously rejected
729  if(!context->localConfig.interfaceIdRejected)
730  {
731  //Add option
732  pppAddOption(configureReqPacket, IPV6CP_OPTION_INTERFACE_ID,
733  &context->localConfig.interfaceId, sizeof(Eui64));
734  }
735 
736  //Save packet length
737  length = configureReqPacket->length;
738  //Convert length field to network byte order
739  configureReqPacket->length = htons(length);
740 
741  //Adjust the length of the multi-part buffer
742  netBufferSetLength(buffer, offset + length);
743 
744  //Debug message
745  TRACE_INFO("Sending Configure-Request packet (%" PRIuSIZE " bytes)...\r\n", length);
746  //Dump packet contents for debugging purpose
747  pppDumpPacket((PppPacket *) configureReqPacket, length, PPP_PROTOCOL_IPV6CP);
748 
749  //Send PPP frame
750  error = pppSendFrame(context->interface, buffer, offset, PPP_PROTOCOL_IPV6CP);
751 
752  //The restart counter is decremented each time a Configure-Request is sent
753  if(context->ipv6cpFsm.restartCounter > 0)
754  context->ipv6cpFsm.restartCounter--;
755 
756  //Save the time at which the packet was sent
757  context->ipv6cpFsm.timestamp = osGetSystemTime();
758 
759  //Free previously allocated memory block
760  netBufferFree(buffer);
761  //Return status code
762  return error;
763 }
764 
765 
766 /**
767  * @brief Send-Configure-Ack callback function
768  * @param[in] context PPP context
769  * @param[in] configureReqPacket Configure-Request packet received from the peer
770  * @return Error code
771  **/
772 
774  const PppConfigurePacket *configureReqPacket)
775 {
776  //Debug message
777  TRACE_INFO("IPV6CP Send-Configure-Ack callback\r\n");
778 
779  //Send Configure-Ack packet
780  return pppSendConfigureAckNak(context, configureReqPacket,
782 }
783 
784 
785 /**
786  * @brief Send-Configure-Nak callback function
787  * @param[in] context PPP context
788  * @param[in] configureReqPacket Configure-Request packet received from the peer
789  * @return Error code
790  **/
791 
793  const PppConfigurePacket *configureReqPacket)
794 {
795  //Debug message
796  TRACE_INFO("IPV6CP Send-Configure-Nak callback\r\n");
797 
798  //Send Configure-Nak packet
799  return pppSendConfigureAckNak(context, configureReqPacket,
801 }
802 
803 
804 /**
805  * @brief Send-Configure-Reject callback function
806  * @param[in] context PPP context
807  * @param[in] configureReqPacket Configure-Request packet received from the peer
808  * @return Error code
809  **/
810 
812  const PppConfigurePacket *configureReqPacket)
813 {
814  //Debug message
815  TRACE_INFO("IPV6CP Send-Configure-Reject callback\r\n");
816 
817  //Send Configure-Reject packet
818  return pppSendConfigureAckNak(context, configureReqPacket,
820 }
821 
822 
823 /**
824  * @brief Send-Terminate-Request callback function
825  * @param[in] context PPP context
826  * @return Error code
827  **/
828 
830 {
831  error_t error;
832 
833  //Debug message
834  TRACE_INFO("IPV6CP Send-Terminate-Request callback\r\n");
835 
836  //On transmission, the Identifier field must be changed
837  context->ipv6cpFsm.identifier++;
838 
839  //Send Terminate-Request packet
840  error = pppSendTerminateReq(context, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
841 
842  //The restart counter is decremented each time a Terminate-Request is sent
843  if(context->ipv6cpFsm.restartCounter > 0)
844  context->ipv6cpFsm.restartCounter--;
845 
846  //Save the time at which the packet was sent
847  context->ipv6cpFsm.timestamp = osGetSystemTime();
848 
849  //Return status code
850  return error;
851 }
852 
853 
854 /**
855  * @brief Send-Terminate-Ack callback function
856  * @param[in] context PPP context
857  * @param[in] terminateReqPacket Terminate-Request packet received from the peer
858  * @return Error code
859  **/
860 
862  const PppTerminatePacket *terminateReqPacket)
863 {
864  uint8_t identifier;
865 
866  //Debug message
867  TRACE_INFO("IPV6CP Send-Terminate-Ack callback\r\n");
868 
869  //Check whether this Terminate-Ack acknowledges the reception of a
870  //Terminate-Request packet
871  if(terminateReqPacket != NULL)
872  {
873  //The Identifier field of the Terminate-Request is copied into the
874  //Identifier field of the Terminate-Ack packet
875  identifier = terminateReqPacket->identifier;
876  }
877  else
878  {
879  //This Terminate-Ack packet serves to synchronize the automatons
880  identifier = ++context->ipv6cpFsm.identifier;
881  }
882 
883  //Send Terminate-Ack packet
885 }
886 
887 
888 /**
889  * @brief Send-Code-Reject callback function
890  * @param[in] context PPP context
891  * @param[in] packet Un-interpretable packet received from the peer
892  * @return Error code
893  **/
894 
896 {
897  //Debug message
898  TRACE_INFO("IPV6CP Send-Code-Reject callback\r\n");
899 
900  //The Identifier field must be changed for each Code-Reject sent
901  context->ipv6cpFsm.identifier++;
902 
903  //Send Code-Reject packet
904  return pppSendCodeRej(context, packet, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
905 }
906 
907 
908 /**
909  * @brief Parse IPV6CP configuration option
910  * @param[in] context PPP context
911  * @param[in] option Option to be checked
912  * @param[in] inPacketLen Remaining bytes to process in the incoming packet
913  * @param[out] outPacket Pointer to the Configure-Ack, Nak or Reject packet
914  * @return Error code
915  **/
916 
918  size_t inPacketLen, PppConfigurePacket *outPacket)
919 {
920  error_t error;
921 
922  //Malformed IPV6CP packet?
923  if(inPacketLen < sizeof(PppOption))
924  return ERROR_INVALID_LENGTH;
925 
926  //Check option length
927  if(option->length < sizeof(PppOption))
928  return ERROR_INVALID_LENGTH;
929  if(option->length > inPacketLen)
930  return ERROR_INVALID_LENGTH;
931 
932  //Check option type
933  switch(option->type)
934  {
936  //Check Interface-Identifier option
937  error = ipv6cpParseInterfaceIdOption(context, (Ipv6cpInterfaceIdOption *) option, outPacket);
938  break;
939  default:
940  //If some configuration options received in the Configure-Request are not
941  //recognizable or not acceptable for negotiation, then the implementation
942  //must transmit a Configure-Reject
943  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_REJ)
944  {
945  //The options field of the Configure-Reject packet is filled
946  //with the unrecognized options from the Configure-Request
947  pppAddOption(outPacket, option->type, option->data,
948  option->length - sizeof(PppOption));
949  }
950 
951  //The option is not acceptable for negotiation
952  error = ERROR_INVALID_TYPE;
953  break;
954  }
955 
956  //Return status code
957  return error;
958 }
959 
960 
961 /**
962  * @brief Parse Interface-Identifier option
963  * @param[in] context PPP context
964  * @param[in] option Option to be checked
965  * @param[out] outPacket Pointer to the Configure-Nak or Configure-Reject packet
966  * @return Error code
967  **/
968 
970  Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
971 {
972  error_t error;
973 
974  //Check length field
975  if(option->length == sizeof(Ipv6cpInterfaceIdOption))
976  {
977  //Check whether the option value is acceptable
978  if(!eui64CompAddr(&option->interfaceId, &EUI64_UNSPECIFIED_ADDR))
979  {
980  //If every configuration option received in the Configure-Request is
981  //recognizable and all values are acceptable, then the implementation
982  //must transmit a Configure-Ack
983  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_ACK)
984  {
985  //Save interface identifier
986  context->peerConfig.interfaceId = option->interfaceId;
987 
988  //The options field of the Configure-Ack packet contains the
989  //configuration options that the sender is acknowledging
991  &option->interfaceId, option->length - sizeof(PppOption));
992  }
993 
994  //The value is acceptable
995  error = NO_ERROR;
996  }
997  else
998  {
999  //If all configuration options are recognizable, but some values are not
1000  //acceptable, then the implementation must transmit a Configure-Nak
1001  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_NAK)
1002  {
1003  //The option must be modified to a value acceptable to the
1004  //Configure-Nak sender
1006  &context->peerConfig.interfaceId, sizeof(Eui64));
1007  }
1008 
1009  //The value is not acceptable
1010  error = ERROR_INVALID_VALUE;
1011  }
1012  }
1013  else
1014  {
1015  //Invalid length field
1016  error = ERROR_INVALID_LENGTH;
1017  }
1018 
1019  //Return status code
1020  return error;
1021 }
1022 
1023 #endif
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:538
__start_packed struct @1 Eui64
EUI-64 identifier.
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:1089
uint8_t length
Definition: coap_common.h:193
#define htons(value)
Definition: cpu_endian.h:413
void ipv6cpZeroRestartCount(PppContext *context)
Zero-Restart-Count callback function.
Definition: ipv6cp.c:682
IPv6 (Internet Protocol Version 6)
Data logging functions for debugging purpose (PPP)
@ PPP_CODE_CONFIGURE_REJ
Configure-Reject.
Definition: ppp.h:219
int bool_t
Definition: compiler_port.h:53
#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:703
@ PPP_CODE_CONFIGURE_ACK
Configure-Ack.
Definition: ppp.h:217
__start_packed struct @2 PppCodeRejPacket
Code-Reject packet.
void pppRcvUnknownCodeEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks, const PppPacket *packet)
Process Receive-Unknown-Code event.
Definition: ppp_fsm.c:757
error_t ipv6cpParseOption(PppContext *context, PppOption *option, size_t inPacketLen, PppConfigurePacket *outPacket)
Parse IPV6CP configuration option.
Definition: ipv6cp.c:917
error_t ipv6cpSendTerminateReq(PppContext *context)
Send-Terminate-Request callback function.
Definition: ipv6cp.c:829
error_t ipv6cpParseInterfaceIdOption(PppContext *context, Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
Parse Interface-Identifier option.
Definition: ipv6cp.c:969
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:89
PPP finite state machine.
#define TRUE
Definition: os_port.h:50
__start_packed struct @6 PppOption
LCP/NCP option.
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:654
const PppCallbacks ipv6cpCallbacks
IPV6CP FSM callbacks.
Definition: ipv6cp.c:52
@ 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:1292
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:642
#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:153
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:792
void ipv6cpThisLayerDown(PppContext *context)
This-Layer-Down callback function.
Definition: ipv6cp.c:615
error_t ipv6cpSendCodeRej(PppContext *context, const PppPacket *packet)
Send-Code-Reject callback function.
Definition: ipv6cp.c:895
@ 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
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:139
error_t
Error codes.
Definition: error.h:43
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
uint8_t value[]
Definition: tcp.h:367
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:981
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:111
Helper functions for IPv6.
#define PPP_RESTART_TIMER
Definition: ppp.h:96
@ PPP_CODE_CONFIGURE_NAK
Configure-Nak.
Definition: ppp.h:218
@ ERROR_INVALID_TYPE
Definition: error.h:115
#define TRACE_INFO(...)
Definition: debug.h:95
__start_packed struct @1 PppTerminatePacket
Terminate-Request and Terminate-Ack packet.
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:667
@ 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
uint32_t systime_t
System time.
#define ntohs(value)
Definition: cpu_endian.h:421
uint32_t time
error_t pppSendFrame(NetInterface *interface, NetBuffer *buffer, size_t offset, uint16_t protocol)
Send a PPP frame.
Definition: ppp.c:1022
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:116
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:861
error_t pppSendConfigureAckNak(PppContext *context, const PppConfigurePacket *configureReqPacket, PppProtocol protocol, PppCode code)
Send Configure-Ack, Nak or Reject packet.
Definition: ppp_misc.c:56
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:89
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
void pppDownEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Down event.
Definition: ppp_fsm.c:84
#define PRIuSIZE
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:50
TCP/IP stack core.
#define PPP_MAX_CONF_REQ_SIZE
Definition: ppp.h:140
__start_packed struct @0 PppConfigurePacket
Configure-Request, Configure-Ack, Configure-Nak and Configure-Reject packets.
__start_packed struct @0 Ipv6Addr
IPv6 network address.
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:773
error_t ipv6cpSendConfigureRej(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Reject callback function.
Definition: ipv6cp.c:811
@ NO_ERROR
Success.
Definition: error.h:44
@ PPP_CODE_TERMINATE_REQ
Terminate-Request.
Definition: ppp.h:220
Debugging facilities.
__start_packed struct @0 Ipv6cpInterfaceIdOption
Interface-Identifier option.
void pppTimeoutEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Timeout event.
Definition: ppp_fsm.c:257
systime_t osGetSystemTime(void)
Retrieve system time.