ah.c
Go to the documentation of this file.
1 /**
2  * @file ah.c
3  * @brief AH (IP Authentication Header)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneIPSEC 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.5.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL AH_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ipsec/ipsec.h"
36 #include "ipsec/ipsec_inbound.h"
38 #include "ipsec/ipsec_misc.h"
39 #include "ah/ah.h"
40 #include "core/tcp_fsm.h"
41 #include "core/raw_socket.h"
42 #include "ipv4/icmp.h"
43 #include "debug.h"
44 
45 //Check IPsec library configuration
46 #if (AH_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Process AH protected packet
51  * @param[in] interface Underlying network interface
52  * @param[in] ipv4Header Pointer to the IPv4 header
53  * @param[in] buffer Multi-part buffer containing the AH protected packet
54  * @param[in] offset Offset to the first byte of the AH header
55  * @param[in] ancillary Additional options passed to the stack along with
56  * the packet
57  * @return Error code
58  **/
59 
61  const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset,
62  NetRxAncillary *ancillary)
63 {
64  error_t error;
65  size_t n;
66  size_t length;
67  uint64_t seq;
68  IpsecSadEntry *sa;
69  AhHeader *ahHeader;
70  IpsecSelector selector;
71  IpPseudoHeader pseudoHeader;
72 
73  //Retrieve the length of the payload
74  length = netBufferGetLength(buffer) - offset;
75 
76  //Malformed packet?
77  if(length < sizeof(AhHeader))
78  return ERROR_INVALID_HEADER;
79 
80  //Point to the AH header
81  ahHeader = netBufferAt(buffer, offset, 0);
82  //Sanity check
83  if(ahHeader == NULL)
84  return ERROR_FAILURE;
85 
86  //If a packet offered to AH for processing appears to be an IP fragment,
87  //the receiver must discard the packet (refer to RFC 4302, section 3.4.1)
88  if((ntohs(ipv4Header->fragmentOffset) & IPV4_OFFSET_MASK) != 0 ||
89  (ntohs(ipv4Header->fragmentOffset) & IPV4_FLAG_MF) != 0)
90  {
91  return ERROR_INVALID_HEADER;
92  }
93 
94  //Upon receipt of a packet containing an IP Authentication Header, the
95  //receiver determines the appropriate (unidirectional) SA via lookup in
96  //the SAD (refer to RFC 4302, section 3.4.2)
98  ntohl(ahHeader->spi));
99 
100  //If no valid Security Association exists for this packet the receiver
101  //must discard the packet. This is an auditable event
102  if(sa == NULL)
103  {
104  //Debug message
105  TRACE_WARNING("AH: No matching SA found!\r\n");
106  //Report an error
107  return ERROR_POLICY_FAILURE;
108  }
109 
110  //The Payload Length field specifies the length of AH header in 32-bit
111  //words (4-byte units), minus 2
112  n = (ahHeader->payloadLen + 2) * sizeof(uint32_t);
113 
114  //Check the length of the AH header
115  if(n > length || n != (sizeof(AhHeader) + sa->icvLen))
116  return ERROR_INVALID_HEADER;
117 
118  //Debug message
119  TRACE_INFO("Parsing AH header...\r\n");
120  //Dump AH header contents for debugging purpose
121  ahDumpHeader(ahHeader);
122 
123  //Because only the low-order 32 bits are transmitted with the packet, the
124  //receiver must deduce and track the sequence number subspace into which
125  //each packet falls
126  seq = ipsecGetSeqNum(sa, ntohl(ahHeader->seqNum));
127 
128  //For each received packet, the receiver must verify that the packet
129  //contains a Sequence Number that does not duplicate the Sequence Number of
130  //any other packets received during the life of this SA. This should be the
131  //first AH check applied to a packet after it has been matched to an SA, to
132  //speed rejection of duplicate packets (refer to RFC 4302, section 3.4.3)
133  error = ipsecCheckReplayWindow(sa, seq);
134 
135  //Duplicate packets are rejected
136  if(error)
137  {
138  //Debug message
139  TRACE_WARNING("AH: Invalid sequence number!\r\n");
140  //Report an error
142  }
143 
144  //If the received packet falls within the window and is not a duplicate, or
145  //if the packet is to the right of the window, then the receiver proceeds to
146  //ICV verification
147  error = ahVerifyIcv(sa, ipv4Header, ahHeader, buffer,
148  offset + sizeof(AhHeader) + sa->icvLen);
149 
150  //If the ICV validation fails, the receiver must discard the received IP
151  //datagram as invalid. This is is an auditable event (refer to RFC 4302,
152  //section 3.4.3)
153  if(error)
154  {
155  //Debug message
156  TRACE_WARNING("AH: ICV validation failed!\r\n");
157  //Report an error
159  }
160 
161  //The receive window is updated only if the ICV verification succeeds
162  ipsecUpdateReplayWindow(sa, seq);
163 
164  //Point to the payload
165  offset += n;
166  length -= n;
167 
168  //Match the packet against the inbound selectors identified by the SAD entry
169  //to verify that the received packet is appropriate for the SA via which it
170  //was received (refer to RFC 4301, section 5.2)
171  error = ipsecGetInboundIpv4PacketSelector(ipv4Header, ahHeader->nextHeader,
172  buffer, offset, &selector);
173  //Any error to report?
174  if(error)
175  return error;
176 
177  //If an IPsec system receives an inbound packet on an SA and the packet's
178  //header fields are not consistent with the selectors for the SA, it must
179  //discard the packet. This is an auditable event
180  if(!ipsecIsSubsetSelector(&selector, &sa->selector))
181  return ERROR_POLICY_FAILURE;
182 
183  //Form the IPv4 pseudo header
184  pseudoHeader.length = sizeof(Ipv4PseudoHeader);
185  pseudoHeader.ipv4Data.srcAddr = ipv4Header->srcAddr;
186  pseudoHeader.ipv4Data.destAddr = ipv4Header->destAddr;
187  pseudoHeader.ipv4Data.reserved = 0;
188  pseudoHeader.ipv4Data.protocol = ahHeader->nextHeader;
189  pseudoHeader.ipv4Data.length = htons(length);
190 
191  //If the computed and received ICVs match, then the datagram is valid, and
192  //it is accepted (refer to RFC 4302, section 3.4.4)
193  switch(ahHeader->nextHeader)
194  {
195  //ICMP protocol?
196  case IPV4_PROTOCOL_ICMP:
197  //Process incoming ICMP message
198  icmpProcessMessage(interface, &pseudoHeader.ipv4Data, buffer, offset);
199 
200 #if (RAW_SOCKET_SUPPORT == ENABLED)
201  //Allow raw sockets to process ICMP messages
202  rawSocketProcessIpPacket(interface, &pseudoHeader, buffer, offset,
203  ancillary);
204 #endif
205 
206  //Continue processing
207  break;
208 
209 #if (IGMP_HOST_SUPPORT == ENABLED || IGMP_ROUTER_SUPPORT == ENABLED || \
210  IGMP_SNOOPING_SUPPORT == ENABLED)
211  //IGMP protocol?
212  case IPV4_PROTOCOL_IGMP:
213  //Process incoming IGMP message
214  igmpProcessMessage(interface, &pseudoHeader.ipv4Data, buffer, offset,
215  ancillary);
216 
217 #if (RAW_SOCKET_SUPPORT == ENABLED)
218  //Allow raw sockets to process IGMP messages
219  rawSocketProcessIpPacket(interface, &pseudoHeader, buffer, offset,
220  ancillary);
221 #endif
222 
223  //Continue processing
224  break;
225 #endif
226 
227 #if (TCP_SUPPORT == ENABLED)
228  //TCP protocol?
229  case IPV4_PROTOCOL_TCP:
230  //Process incoming TCP segment
231  tcpProcessSegment(interface, &pseudoHeader, buffer, offset, ancillary);
232  //Continue processing
233  break;
234 #endif
235 
236 #if (UDP_SUPPORT == ENABLED)
237  //UDP protocol?
238  case IPV4_PROTOCOL_UDP:
239  //Process incoming UDP datagram
240  error = udpProcessDatagram(interface, &pseudoHeader, buffer, offset,
241  ancillary);
242  //Continue processing
243  break;
244 #endif
245 
246  //Unknown protocol?
247  default:
248 #if (RAW_SOCKET_SUPPORT == ENABLED)
249  //Allow raw sockets to process IPv4 packets
250  error = rawSocketProcessIpPacket(interface, &pseudoHeader, buffer, offset,
251  ancillary);
252 #else
253  //Report an error
255 #endif
256  //Continue processing
257  break;
258  }
259 
260  //Return status code
261  return error;
262 }
263 
264 
265 /**
266  * @brief ICV generation
267  * @param[in] sa Pointer to the SA
268  * @param[in] ipv4Header Pointer to the IPv4 header
269  * @param[in,out] ahHeader Pointer to the AH header
270  * @param[in] buffer Multi-part buffer containing the payload
271  * @param[in] offset Offset to the first byte of the payload
272  * @return Error code
273  **/
274 
276  AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
277 {
278  error_t error;
279  uint_t i;
280  size_t n;
281  uint8_t *p;
282  IpsecContext *context;
283 
284  //Point to the IPsec context
285  context = netContext.ipsecContext;
286  //Invalid IPsec context?
287  if(context == NULL)
288  return ERROR_FAILURE;
289 
290 #if (AH_CMAC_SUPPORT == ENABLED)
291  //CMAC integrity algorithm?
292  if(sa->authCipherAlgo != NULL)
293  {
294  CmacContext *cmacContext;
295 
296  //Point to the CMAC context
297  cmacContext = &context->cmacContext;
298 
299  //The SAD entry specifies the algorithm employed for ICV computation
300  error = cmacInit(cmacContext, sa->authCipherAlgo, sa->authKey,
301  sa->authKeyLen);
302 
303  //Check status code
304  if(!error)
305  {
306  //Compute CMAC over the IP or extension header fields before the AH
307  //header that are either immutable in transit or that are predictable
308  //in value upon arrival at the endpoint for the AH SA
309  cmacUpdate(cmacContext, ipv4Header, sizeof(Ipv4Header));
310 
311  //Compute CMAC over the Next Header, Payload Length, Reserved, SPI,
312  //Sequence Number (low-order 32 bits) fields, and the ICV (which is
313  //set to zero for this computation)
314  cmacUpdate(cmacContext, ahHeader, sizeof(AhHeader) + sa->icvLen);
315 
316  //Everything after AH is assumed to be immutable in transit
317  for(i = 0; i < buffer->chunkCount; i++)
318  {
319  //Is there any data to process from the current chunk?
320  if(offset < buffer->chunk[i].length)
321  {
322  //Point to the first byte to be processed
323  p = (uint8_t *) buffer->chunk[i].address + offset;
324  //Compute the number of bytes to process at a time
325  n = buffer->chunk[i].length - offset;
326 
327  //Update CMAC calculation
328  cmacUpdate(cmacContext, p, n);
329 
330  //Process the next block from the start
331  offset = 0;
332  }
333  else
334  {
335  //Skip the current chunk
336  offset -= buffer->chunk[i].length;
337  }
338  }
339 
340  //Extended sequence numbers?
341  if(sa->esn)
342  {
343  //If the ESN option is elected for an SA, then the high-order 32
344  //bits of the ESN must be included in the ICV computation
345  uint32_t seqh = htonl(sa->seqNum >> 32);
346 
347  //For purposes of ICV computation, these bits are appended
348  //(implicitly) immediately after the end of the payload
349  cmacUpdate(cmacContext, (uint8_t *) &seqh, 4);
350  }
351 
352  //Finalize CMAC calculation
353  cmacFinal(cmacContext, ahHeader->icv, sa->icvLen);
354  }
355  }
356  else
357 #endif
358 #if (AH_HMAC_SUPPORT == ENABLED)
359  //HMAC integrity algorithm?
360  if(sa->authHashAlgo != NULL)
361  {
362  HmacContext *hmacContext;
363 
364  //Point to the HMAC context
365  hmacContext = &context->hmacContext;
366 
367  //The SAD entry specifies the algorithm employed for ICV computation
368  error = hmacInit(hmacContext, sa->authHashAlgo, sa->authKey,
369  sa->authKeyLen);
370 
371  //Check status code
372  if(!error)
373  {
374  //Compute HMAC over the IP or extension header fields before the AH
375  //header that are either immutable in transit or that are predictable
376  //in value upon arrival at the endpoint for the AH SA
377  hmacUpdate(hmacContext, ipv4Header, sizeof(Ipv4Header));
378 
379  //Compute HMAC over the Next Header, Payload Length, Reserved, SPI,
380  //Sequence Number (low-order 32 bits) fields, and the ICV (which is
381  //set to zero for this computation)
382  hmacUpdate(hmacContext, ahHeader, sizeof(AhHeader) + sa->icvLen);
383 
384  //Everything after AH is assumed to be immutable in transit
385  for(i = 0; i < buffer->chunkCount; i++)
386  {
387  //Is there any data to process from the current chunk?
388  if(offset < buffer->chunk[i].length)
389  {
390  //Point to the first byte to be processed
391  p = (uint8_t *) buffer->chunk[i].address + offset;
392  //Compute the number of bytes to process at a time
393  n = buffer->chunk[i].length - offset;
394 
395  //Update HMAC calculation
396  hmacUpdate(hmacContext, p, n);
397 
398  //Process the next block from the start
399  offset = 0;
400  }
401  else
402  {
403  //Skip the current chunk
404  offset -= buffer->chunk[i].length;
405  }
406  }
407 
408  //Extended sequence numbers?
409  if(sa->esn)
410  {
411  //If the ESN option is elected for an SA, then the high-order 32
412  //bits of the ESN must be included in the ICV computation
413  uint32_t seqh = htonl(sa->seqNum >> 32);
414 
415  //For purposes of ICV computation, these bits are appended
416  //(implicitly) immediately after the end of the payload
417  hmacUpdate(hmacContext, (uint8_t *) &seqh, 4);
418  }
419 
420  //Finalize HMAC calculation
421  hmacFinal(hmacContext, NULL);
422  //The output of the HMAC can be truncated
423  osMemcpy(ahHeader->icv, hmacContext->digest, sa->icvLen);
424  }
425  }
426  else
427 #endif
428  //Unknown integrity algorithm?
429  {
430  //Report an error
431  error = ERROR_FAILURE;
432  }
433 
434  //Return status code
435  return error;
436 }
437 
438 
439 /**
440  * @brief ICV verification
441  * @param[in] sa Pointer to the SA
442  * @param[in] ipv4Header Pointer to the IPv4 header
443  * @param[in] ahHeader Pointer to the AH header
444  * @param[in] buffer Multi-part buffer containing the payload
445  * @param[in] offset Offset to the first byte of the payload
446  * @return Error code
447  **/
448 
450  const AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
451 {
452  error_t error;
453  uint8_t mask;
454  uint_t i;
455  size_t n;
456  uint8_t *p;
457  IpsecContext *context;
458  Ipv4Header *ipv4Header2;
459  AhHeader *ahHeader2;
460  uint8_t temp[IPV4_MAX_HEADER_LENGTH];
461  uint8_t checksum[AH_MAX_DIGEST_SIZE];
462 
463  //Point to the IPsec context
464  context = netContext.ipsecContext;
465  //Invalid IPsec context?
466  if(context == NULL)
467  return ERROR_FAILURE;
468 
469  //Calculate the length of the IPv4 header
470  n = ipv4Header->headerLength * 4;
471  //Copy the IPv4 header
472  osMemcpy(temp, ipv4Header, n);
473  //Point to the IPv4 header
474  ipv4Header2 = (Ipv4Header *) temp;
475 
476  //If a field may be modified during transit, the value of the field is set
477  //to zero for purposes of the ICV computation
478  ipv4Header2->typeOfService = 0;
479  ipv4Header2->fragmentOffset = 0;
480  ipv4Header2->timeToLive = 0;
481  ipv4Header2->headerChecksum = 0;
482 
483  //Mutable options are zeroed before performing the ICV calculation
484  ahProcessMutableIpv4Options(ipv4Header2);
485 
486 #if (AH_CMAC_SUPPORT == ENABLED)
487  //CMAC integrity algorithm?
488  if(sa->authCipherAlgo != NULL)
489  {
490  CmacContext *cmacContext;
491 
492  //Point to the CMAC context
493  cmacContext = &context->cmacContext;
494 
495  //The SAD entry specifies the algorithm employed for ICV computation,
496  //and indicates the key required to validate the ICV
497  error = cmacInit(cmacContext, sa->authCipherAlgo, sa->authKey,
498  sa->authKeyLen);
499 
500  //Check status code
501  if(!error)
502  {
503  //Compute CMAC over the IP or extension header fields before the AH
504  //header that are either immutable in transit or that are predictable
505  //in value upon arrival at the endpoint for the AH SA
506  cmacUpdate(cmacContext, temp, n);
507 
508  //The Payload Length field specifies the length of AH header in 32-bit
509  //words (4-byte units), minus 2
510  n = (ahHeader->payloadLen + 2) * sizeof(uint32_t);
511 
512  //Copy the AH header
513  osMemcpy(temp, ahHeader, n);
514  //Point to the AH header
515  ahHeader2 = (AhHeader *) temp;
516 
517  //The Integrity Check Value field is also set to zero in preparation
518  //for this computation (refer to RFC 4302, section 3.3.3.1)
519  osMemset(ahHeader2->icv, 0, sa->icvLen);
520 
521  //Compute CMAC over the Next Header, Payload Length, Reserved, SPI,
522  //Sequence Number (low-order 32 bits) fields, and the ICV (which is
523  //set to zero for this computation)
524  cmacUpdate(cmacContext, temp, n);
525 
526  //Everything after AH is assumed to be immutable in transit
527  for(i = 0; i < buffer->chunkCount; i++)
528  {
529  //Is there any data to process from the current chunk?
530  if(offset < buffer->chunk[i].length)
531  {
532  //Point to the first byte to be processed
533  p = (uint8_t *) buffer->chunk[i].address + offset;
534  //Compute the number of bytes to process at a time
535  n = buffer->chunk[i].length - offset;
536 
537  //Update CMAC calculation
538  cmacUpdate(cmacContext, p, n);
539 
540  //Process the next block from the start
541  offset = 0;
542  }
543  else
544  {
545  //Skip the current chunk
546  offset -= buffer->chunk[i].length;
547  }
548  }
549 
550  //Extended sequence numbers?
551  if(sa->esn)
552  {
553  //If the ESN option is elected for an SA, then the high-order 32
554  //bits of the ESN must be included in the ICV computation
555  uint32_t seqh = ipsecGetSeqNum(sa, ntohl(ahHeader->seqNum)) >> 32;
556 
557  //Convert the 32-bit value to network byte order
558  seqh = htonl(seqh);
559 
560  //For purposes of ICV computation, these bits are appended
561  //(implicitly) immediately after the end of the payload
562  cmacUpdate(cmacContext, (uint8_t *) &seqh, 4);
563  }
564 
565  //Finalize CMAC computation
566  cmacFinal(cmacContext, checksum, sa->icvLen);
567  }
568  }
569  else
570 #endif
571 #if (AH_HMAC_SUPPORT == ENABLED)
572  //HMAC integrity algorithm?
573  if(sa->authHashAlgo != NULL)
574  {
575  HmacContext *hmacContext;
576 
577  //Point to the HMAC context
578  hmacContext = &context->hmacContext;
579 
580  //The SAD entry specifies the algorithm employed for ICV computation,
581  //and indicates the key required to validate the ICV
582  error = hmacInit(hmacContext, sa->authHashAlgo, sa->authKey,
583  sa->authKeyLen);
584 
585  //Check status code
586  if(!error)
587  {
588  //Compute HMAC over the IP or extension header fields before the AH
589  //header that are either immutable in transit or that are predictable
590  //in value upon arrival at the endpoint for the AH SA
591  hmacUpdate(hmacContext, temp, n);
592 
593  //The Payload Length field specifies the length of AH header in 32-bit
594  //words (4-byte units), minus 2
595  n = (ahHeader->payloadLen + 2) * 4;
596 
597  //Copy the AH header
598  osMemcpy(temp, ahHeader, n);
599  //Point to the AH header
600  ahHeader2 = (AhHeader *) temp;
601 
602  //The Integrity Check Value field is also set to zero in preparation
603  //for this computation (refer to RFC 4302, section 3.3.3.1)
604  osMemset(ahHeader2->icv, 0, sa->icvLen);
605 
606  //Compute HMAC over the Next Header, Payload Length, Reserved, SPI,
607  //Sequence Number (low-order 32 bits) fields, and the ICV (which is
608  //set to zero for this computation)
609  hmacUpdate(hmacContext, temp, n);
610 
611  //Everything after AH is assumed to be immutable in transit
612  for(i = 0; i < buffer->chunkCount; i++)
613  {
614  //Is there any data to process from the current chunk?
615  if(offset < buffer->chunk[i].length)
616  {
617  //Point to the first byte to be processed
618  p = (uint8_t *) buffer->chunk[i].address + offset;
619  //Compute the number of bytes to process at a time
620  n = buffer->chunk[i].length - offset;
621 
622  //Update HMAC calculation
623  hmacUpdate(hmacContext, p, n);
624 
625  //Process the next block from the start
626  offset = 0;
627  }
628  else
629  {
630  //Skip the current chunk
631  offset -= buffer->chunk[i].length;
632  }
633  }
634 
635  //Extended sequence numbers?
636  if(sa->esn)
637  {
638  //If the ESN option is elected for an SA, then the high-order 32
639  //bits of the ESN must be included in the ICV computation
640  uint32_t seqh = ipsecGetSeqNum(sa, ntohl(ahHeader->seqNum)) >> 32;
641 
642  //Convert the 32-bit value to network byte order
643  seqh = htonl(seqh);
644 
645  //For purposes of ICV computation, these bits are appended
646  //(implicitly) immediately after the end of the payload
647  hmacUpdate(hmacContext, (uint8_t *) &seqh, 4);
648  }
649 
650  //Finalize HMAC computation
651  hmacFinal(hmacContext, checksum);
652  }
653  }
654  else
655 #endif
656  //Unknown integrity algorithm?
657  {
658  //Report an error
659  error = ERROR_INVALID_MAC;
660  }
661 
662  //Check status code
663  if(!error)
664  {
665  //Debug message
666  TRACE_DEBUG_ARRAY("Calculated ICV = ", checksum, sa->icvLen);
667 
668  //The calculated checksum is bitwise compared to the received ICV
669  for(mask = 0, i = 0; i < sa->icvLen; i++)
670  {
671  mask |= checksum[i] ^ ahHeader->icv[i];
672  }
673 
674  //If the ICV validation fails, the receiver must discard the received IP
675  //datagram as invalid. This is is an auditable event (refer to RFC 4302,
676  //section 3.4.3)
677  error = (mask == 0) ? NO_ERROR : ERROR_INVALID_MAC;
678  }
679 
680  //Return status code
681  return error;
682 }
683 
684 
685 /**
686  * @brief Zeroize mutable IPv4 options
687  * @param[in] header Pointer to the IPv4 header
688  **/
689 
691 {
692  size_t i;
693  size_t n;
694  size_t length;
695  Ipv4Option *option;
696 
697  //Compute the length of the options field
698  length = (header->headerLength * 4) - sizeof(TcpHeader);
699 
700  //Point to the very first option
701  i = 0;
702 
703  //Loop through the list of options
704  while(i < length)
705  {
706  //Point to the current option
707  option = (Ipv4Option *) (header->options + i);
708 
709  //Check option type
710  if(option->type == IPV4_OPTION_EEOL)
711  {
712  //This option code indicates the end of the option list
713  break;
714  }
715  else if(option->type == IPV4_OPTION_NOP)
716  {
717  //This option consists of a single octet
718  i++;
719  }
720  else
721  {
722  //Malformed option?
723  if((i + 1) >= length)
724  break;
725 
726  //The option code is followed by a one-byte length field
727  n = option->length;
728 
729  //Check the length of the option
730  if(n < sizeof(Ipv4Option) || (i + n) > length)
731  break;
732 
733  //Mutable option?
734  if(option->type != IPV4_OPTION_SEC &&
735  option->type != IPV4_OPTION_ESEC &&
736  option->type != IPV4_OPTION_CIPSO &&
737  option->type != IPV4_OPTION_RTRALT &&
738  option->type != IPV4_OPTION_SDB)
739  {
740  //The entire option is zeroed before performing the ICV calculation
741  osMemset(option, 0, n);
742  }
743 
744  //Jump to the next option
745  i += n;
746  }
747  }
748 }
749 
750 
751 /**
752  * @brief Dump AH header for debugging purpose
753  * @param[in] ahHeader Pointer to the AH header
754  **/
755 
756 void ahDumpHeader(const AhHeader *ahHeader)
757 {
758  size_t n;
759 
760  //The Payload Length field specifies the length of AH header in 32-bit
761  //words (4-byte units), minus 2
762  n = (ahHeader->payloadLen + 2) * sizeof(uint32_t);
763 
764  //Check the length of the AH header
765  if(n >= sizeof(AhHeader))
766  {
767  //Retrieve the length of the ICV tag
768  n -= sizeof(AhHeader);
769 
770  //Dump AH header contents
771  TRACE_DEBUG(" Next Header = %" PRIu8 "\r\n", ahHeader->nextHeader);
772  TRACE_DEBUG(" Payload Length = %" PRIu8 "\r\n", ahHeader->payloadLen);
773  TRACE_DEBUG(" SPI = 0x%08" PRIX32 "\r\n", ntohl(ahHeader->spi));
774  TRACE_DEBUG(" Sequence Number = 0x%08" PRIX32 "\r\n", ntohl(ahHeader->seqNum));
775  TRACE_DEBUG_ARRAY(" ICV = ", ahHeader->icv, n);
776  }
777 }
778 
779 #endif
#define htons(value)
Definition: cpu_endian.h:413
error_t ahGenerateIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header, AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV generation.
Definition: ah.c:275
error_t ahVerifyIcv(IpsecSadEntry *sa, const Ipv4Header *ipv4Header, const AhHeader *ahHeader, const NetBuffer *buffer, size_t offset)
ICV verification.
Definition: ah.c:449
HMAC algorithm context.
Definition: hmac.h:59
#define AH_MAX_DIGEST_SIZE
Definition: ah.h:151
#define Ipv4Header
Definition: ipv4.h:36
@ IPV4_PROTOCOL_ICMP
Definition: ipv4.h:251
@ IPV4_OFFSET_MASK
Definition: ipv4.h:240
@ IPV4_OPTION_SDB
Selective Directed Broadcast.
Definition: ipv4.h:287
@ ERROR_WRONG_SEQUENCE_NUMBER
Definition: error.h:183
@ IPSEC_PROTOCOL_AH
Definition: ipsec.h:192
error_t rawSocketProcessIpPacket(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Process incoming IP packet.
Definition: raw_socket.c:68
IPsec selector.
Definition: ipsec.h:302
uint8_t p
Definition: ndp.h:300
uint16_t checksum
Definition: tcp.h:362
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint_t chunkCount
Definition: net_mem.h:90
@ ERROR_INVALID_HEADER
Definition: error.h:87
@ IPV4_OPTION_ESEC
Extended Security.
Definition: ipv4.h:278
NetContext netContext
Definition: net.c:75
error_t udpProcessDatagram(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Incoming UDP datagram processing.
Definition: udp.c:124
size_t length
Definition: ip.h:111
@ IPV4_OPTION_NOP
No Operation.
Definition: ipv4.h:267
@ ERROR_INVALID_MAC
Definition: error.h:113
error_t ipsecCheckReplayWindow(const IpsecSadEntry *sa, uint64_t seqNum)
Perform replay detection.
uint64_t ipsecGetSeqNum(IpsecSadEntry *sa, uint32_t seql)
Determine the higher-order bits of the sequence number.
IP pseudo header.
Definition: ip.h:110
void icmpProcessMessage(NetInterface *interface, const Ipv4PseudoHeader *requestPseudoHeader, const NetBuffer *buffer, size_t offset)
Incoming ICMP message processing.
Definition: icmp.c:111
@ IPV4_PROTOCOL_TCP
Definition: ipv4.h:253
uint16_t length
Definition: net_mem.h:79
#define htonl(value)
Definition: cpu_endian.h:414
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
ICMP (Internet Control Message Protocol)
error_t
Error codes.
Definition: error.h:43
@ ERROR_PROTOCOL_UNREACHABLE
Definition: error.h:84
AhHeader
Definition: ah.h:199
TCP finite state machine.
void * address
Definition: net_mem.h:78
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ IPV4_FLAG_MF
Definition: ipv4.h:239
Helper routines for IPsec.
bool_t ipsecIsSubsetSelector(const IpsecSelector *selector1, const IpsecSelector *selector2)
Test if a selector is a subset of another selector.
Definition: ipsec_misc.c:362
#define NetRxAncillary
Definition: net_misc.h:40
#define NetInterface
Definition: net.h:36
uint8_t mask
Definition: web_socket.h:319
IPsec context.
Definition: ipsec.h:434
#define Ipv4PseudoHeader
Definition: ipv4.h:39
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
IPsec processing of inbound IP traffic.
IpsecSadEntry * ipsecFindInboundSadEntry(IpsecContext *context, IpsecProtocol protocol, uint32_t spi)
Search the SAD database for a matching inbound entry.
Definition: ipsec_misc.c:134
Anti-replay mechanism.
AH (IP Authentication Header)
error_t ipv4ProcessAhHeader(NetInterface *interface, const Ipv4Header *ipv4Header, const NetBuffer *buffer, size_t offset, NetRxAncillary *ancillary)
Process AH protected packet.
Definition: ah.c:60
void * ipsecContext
IPsec context.
Definition: net.h:339
CMAC algorithm context.
Definition: cmac.h:54
#define IpsecSadEntry
Definition: ipsec.h:36
TCP/IP raw sockets.
@ IPV4_PROTOCOL_IGMP
Definition: ipv4.h:252
#define ntohs(value)
Definition: cpu_endian.h:421
__weak_func void hmacUpdate(HmacContext *context, const void *data, size_t length)
Update the HMAC context with a portion of the message being hashed.
Definition: hmac.c:201
#define TRACE_WARNING(...)
Definition: debug.h:93
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint8_t digest[MAX_HASH_DIGEST_SIZE]
Definition: hmac.h:63
@ IPV4_OPTION_EEOL
End of Options List.
Definition: ipv4.h:266
ChunkDesc chunk[]
Definition: net_mem.h:92
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
IPsec (IP security)
uint8_t n
@ IPV4_PROTOCOL_UDP
Definition: ipv4.h:254
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
__weak_func void hmacFinal(HmacContext *context, uint8_t *digest)
Finish the HMAC calculation.
Definition: hmac.c:218
Ipv4Option
Definition: ipv4.h:358
CmacContext cmacContext
CMAC context.
Definition: ipsec.h:444
#define IPV4_MAX_HEADER_LENGTH
Definition: ipv4.h:104
error_t cmacInit(CmacContext *context, const CipherAlgo *cipher, const void *key, size_t keyLen)
Initialize CMAC calculation.
Definition: cmac.c:107
void cmacUpdate(CmacContext *context, const void *data, size_t dataLen)
Update the CMAC context with a portion of the message being hashed.
Definition: cmac.c:191
@ IPV4_OPTION_RTRALT
Router Alert.
Definition: ipv4.h:286
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:418
@ IPV4_OPTION_SEC
Security.
Definition: ipv4.h:276
error_t ipsecGetInboundIpv4PacketSelector(const Ipv4Header *ipv4Header, uint8_t nextHeader, const NetBuffer *buffer, size_t offset, IpsecSelector *selector)
Extract packet's selector from inbound IPv4 packet.
void tcpProcessSegment(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Incoming TCP segment processing.
Definition: tcp_fsm.c:73
@ IPV4_OPTION_CIPSO
Commercial Security.
Definition: ipv4.h:279
HmacContext hmacContext
HMAC context.
Definition: ipsec.h:447
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:138
TcpHeader
Definition: tcp.h:365
__weak_func error_t hmacInit(HmacContext *context, const HashAlgo *hash, const void *key, size_t keyLen)
Initialize HMAC calculation.
Definition: hmac.c:140
error_t cmacFinal(CmacContext *context, uint8_t *mac, size_t macLen)
Finish the CMAC calculation.
Definition: cmac.c:237
void ipsecUpdateReplayWindow(IpsecSadEntry *sa, uint64_t seqNum)
Update sliding window.
@ ERROR_POLICY_FAILURE
Definition: error.h:300
#define ntohl(value)
Definition: cpu_endian.h:422
Ipv4PseudoHeader ipv4Data
Definition: ip.h:115
void ahDumpHeader(const AhHeader *ahHeader)
Dump AH header for debugging purpose.
Definition: ah.c:756
void igmpProcessMessage(NetInterface *interface, const Ipv4PseudoHeader *pseudoHeader, const NetBuffer *buffer, size_t offset, const NetRxAncillary *ancillary)
Process incoming IGMP message.
Definition: igmp_common.c:292
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void ahProcessMutableIpv4Options(Ipv4Header *header)
Zeroize mutable IPv4 options.
Definition: ah.c:690