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-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL 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, 0);
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 
762  //Return status code
763  return error;
764 }
765 
766 
767 /**
768  * @brief Send-Configure-Ack callback function
769  * @param[in] context PPP context
770  * @param[in] configureReqPacket Configure-Request packet received from the peer
771  * @return Error code
772  **/
773 
775  const PppConfigurePacket *configureReqPacket)
776 {
777  //Debug message
778  TRACE_INFO("IPV6CP Send-Configure-Ack callback\r\n");
779 
780  //Send Configure-Ack packet
781  return pppSendConfigureAckNak(context, configureReqPacket,
783 }
784 
785 
786 /**
787  * @brief Send-Configure-Nak callback function
788  * @param[in] context PPP context
789  * @param[in] configureReqPacket Configure-Request packet received from the peer
790  * @return Error code
791  **/
792 
794  const PppConfigurePacket *configureReqPacket)
795 {
796  //Debug message
797  TRACE_INFO("IPV6CP Send-Configure-Nak callback\r\n");
798 
799  //Send Configure-Nak packet
800  return pppSendConfigureAckNak(context, configureReqPacket,
802 }
803 
804 
805 /**
806  * @brief Send-Configure-Reject callback function
807  * @param[in] context PPP context
808  * @param[in] configureReqPacket Configure-Request packet received from the peer
809  * @return Error code
810  **/
811 
813  const PppConfigurePacket *configureReqPacket)
814 {
815  //Debug message
816  TRACE_INFO("IPV6CP Send-Configure-Reject callback\r\n");
817 
818  //Send Configure-Reject packet
819  return pppSendConfigureAckNak(context, configureReqPacket,
821 }
822 
823 
824 /**
825  * @brief Send-Terminate-Request callback function
826  * @param[in] context PPP context
827  * @return Error code
828  **/
829 
831 {
832  error_t error;
833 
834  //Debug message
835  TRACE_INFO("IPV6CP Send-Terminate-Request callback\r\n");
836 
837  //On transmission, the Identifier field must be changed
838  context->ipv6cpFsm.identifier++;
839 
840  //Send Terminate-Request packet
841  error = pppSendTerminateReq(context, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
842 
843  //The restart counter is decremented each time a Terminate-Request is sent
844  if(context->ipv6cpFsm.restartCounter > 0)
845  context->ipv6cpFsm.restartCounter--;
846 
847  //Save the time at which the packet was sent
848  context->ipv6cpFsm.timestamp = osGetSystemTime();
849 
850  //Return status code
851  return error;
852 }
853 
854 
855 /**
856  * @brief Send-Terminate-Ack callback function
857  * @param[in] context PPP context
858  * @param[in] terminateReqPacket Terminate-Request packet received from the peer
859  * @return Error code
860  **/
861 
863  const PppTerminatePacket *terminateReqPacket)
864 {
865  uint8_t identifier;
866 
867  //Debug message
868  TRACE_INFO("IPV6CP Send-Terminate-Ack callback\r\n");
869 
870  //Check whether this Terminate-Ack acknowledges the reception of a
871  //Terminate-Request packet
872  if(terminateReqPacket != NULL)
873  {
874  //The Identifier field of the Terminate-Request is copied into the
875  //Identifier field of the Terminate-Ack packet
876  identifier = terminateReqPacket->identifier;
877  }
878  else
879  {
880  //This Terminate-Ack packet serves to synchronize the automatons
881  identifier = ++context->ipv6cpFsm.identifier;
882  }
883 
884  //Send Terminate-Ack packet
886 }
887 
888 
889 /**
890  * @brief Send-Code-Reject callback function
891  * @param[in] context PPP context
892  * @param[in] packet Un-interpretable packet received from the peer
893  * @return Error code
894  **/
895 
897 {
898  //Debug message
899  TRACE_INFO("IPV6CP Send-Code-Reject callback\r\n");
900 
901  //The Identifier field must be changed for each Code-Reject sent
902  context->ipv6cpFsm.identifier++;
903 
904  //Send Code-Reject packet
905  return pppSendCodeRej(context, packet, context->ipv6cpFsm.identifier, PPP_PROTOCOL_IPV6CP);
906 }
907 
908 
909 /**
910  * @brief Parse IPV6CP configuration option
911  * @param[in] context PPP context
912  * @param[in] option Option to be checked
913  * @param[in] inPacketLen Remaining bytes to process in the incoming packet
914  * @param[out] outPacket Pointer to the Configure-Ack, Nak or Reject packet
915  * @return Error code
916  **/
917 
919  size_t inPacketLen, PppConfigurePacket *outPacket)
920 {
921  error_t error;
922 
923  //Malformed IPV6CP packet?
924  if(inPacketLen < sizeof(PppOption))
925  return ERROR_INVALID_LENGTH;
926 
927  //Check option length
928  if(option->length < sizeof(PppOption))
929  return ERROR_INVALID_LENGTH;
930  if(option->length > inPacketLen)
931  return ERROR_INVALID_LENGTH;
932 
933  //Check option type
934  switch(option->type)
935  {
937  //Check Interface-Identifier option
938  error = ipv6cpParseInterfaceIdOption(context, (Ipv6cpInterfaceIdOption *) option, outPacket);
939  break;
940  default:
941  //If some configuration options received in the Configure-Request are not
942  //recognizable or not acceptable for negotiation, then the implementation
943  //must transmit a Configure-Reject
944  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_REJ)
945  {
946  //The options field of the Configure-Reject packet is filled
947  //with the unrecognized options from the Configure-Request
948  pppAddOption(outPacket, option->type, option->data,
949  option->length - sizeof(PppOption));
950  }
951 
952  //The option is not acceptable for negotiation
953  error = ERROR_INVALID_TYPE;
954  break;
955  }
956 
957  //Return status code
958  return error;
959 }
960 
961 
962 /**
963  * @brief Parse Interface-Identifier option
964  * @param[in] context PPP context
965  * @param[in] option Option to be checked
966  * @param[out] outPacket Pointer to the Configure-Nak or Configure-Reject packet
967  * @return Error code
968  **/
969 
971  Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
972 {
973  error_t error;
974 
975  //Check length field
976  if(option->length == sizeof(Ipv6cpInterfaceIdOption))
977  {
978  //Check whether the option value is acceptable
979  if(!eui64CompAddr(&option->interfaceId, &EUI64_UNSPECIFIED_ADDR))
980  {
981  //If every configuration option received in the Configure-Request is
982  //recognizable and all values are acceptable, then the implementation
983  //must transmit a Configure-Ack
984  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_ACK)
985  {
986  //Save interface identifier
987  context->peerConfig.interfaceId = option->interfaceId;
988 
989  //The options field of the Configure-Ack packet contains the
990  //configuration options that the sender is acknowledging
992  &option->interfaceId, option->length - sizeof(PppOption));
993  }
994 
995  //The value is acceptable
996  error = NO_ERROR;
997  }
998  else
999  {
1000  //If all configuration options are recognizable, but some values are not
1001  //acceptable, then the implementation must transmit a Configure-Nak
1002  if(outPacket != NULL && outPacket->code == PPP_CODE_CONFIGURE_NAK)
1003  {
1004  //The option must be modified to a value acceptable to the
1005  //Configure-Nak sender
1007  &context->peerConfig.interfaceId, sizeof(Eui64));
1008  }
1009 
1010  //The value is not acceptable
1011  error = ERROR_INVALID_VALUE;
1012  }
1013  }
1014  else
1015  {
1016  //Invalid length field
1017  error = ERROR_INVALID_LENGTH;
1018  }
1019 
1020  //Return status code
1021  return error;
1022 }
1023 
1024 #endif
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:1095
#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
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:918
error_t ipv6cpSendTerminateReq(PppContext *context)
Send-Terminate-Request callback function.
Definition: ipv6cp.c:830
error_t ipv6cpParseInterfaceIdOption(PppContext *context, Ipv6cpInterfaceIdOption *option, PppConfigurePacket *outPacket)
Parse Interface-Identifier option.
Definition: ipv6cp.c:970
Eui64
Definition: ethernet.h:210
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
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
Ipv6Addr
Definition: ipv6.h:260
PppConfigurePacket
Definition: ppp.h:274
@ 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:1305
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:268
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:793
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:896
PppCodeRejPacket
Definition: ppp.h:300
@ 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:454
@ 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 pppRcvConfigureAckEvent(PppContext *context, PppFsm *fsm, const PppCallbacks *callbacks)
Process Receive-Configure-Ack event.
Definition: ppp_fsm.c:524
const Eui64 EUI64_UNSPECIFIED_ADDR
Definition: ethernet.c:985
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
uint8_t length
Definition: tcp.h:368
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:217
@ 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:1035
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:862
error_t pppSendConfigureAckNak(PppContext *context, const PppConfigurePacket *configureReqPacket, PppProtocol protocol, PppCode code)
Send Configure-Ack, Nak or Reject packet.
Definition: ppp_misc.c:56
PppTerminatePacket
Definition: ppp.h:287
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:322
uint8_t value[]
Definition: tcp.h:369
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:89
uint8_t identifier[]
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
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
Ipv4Addr ipAddr
Definition: ipcp.h:105
error_t pppSendTerminateReq(PppContext *context, uint8_t identifier, PppProtocol protocol)
Send Terminate-Request packet.
Definition: ppp_misc.c:167
PppOption
Definition: ppp.h:354
@ 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
error_t ipv6cpSendConfigureAck(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Ack callback function.
Definition: ipv6cp.c:774
error_t ipv6cpSendConfigureRej(PppContext *context, const PppConfigurePacket *configureReqPacket)
Send-Configure-Reject callback function.
Definition: ipv6cp.c:812
@ 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
Ipv6cpInterfaceIdOption
Definition: ipv6cp.h:72
systime_t osGetSystemTime(void)
Retrieve system time.