From fbd4dd9c5eba6f8f10bcc0db30a72ea3378c149b Mon Sep 17 00:00:00 2001 From: Mauro Sardara Date: Mon, 11 Feb 2019 23:50:45 +0100 Subject: [HICN-51] Add static assert for ensuring correct struct size in libhicn definitions. Change-Id: Ib41e9cbdd2ea84a40eb4e7b01da131cbad9575c4 Signed-off-by: Mauro Sardara --- lib/src/protocol/ah.h | 11 +++++++++++ lib/src/protocol/icmp.h | 18 +++++++++++++++++- lib/src/protocol/icmprd.h | 29 +++++++++++++++++++++++++++-- lib/src/protocol/ipv4.h | 14 ++++++++++++++ lib/src/protocol/ipv6.h | 15 ++++++++++++++- lib/src/protocol/tcp.h | 9 ++++++++- lib/src/protocol/udp.h | 7 +++++++ 7 files changed, 98 insertions(+), 5 deletions(-) (limited to 'lib/src/protocol') diff --git a/lib/src/protocol/ah.h b/lib/src/protocol/ah.h index f8a05bf38..a59a5051a 100644 --- a/lib/src/protocol/ah.h +++ b/lib/src/protocol/ah.h @@ -20,12 +20,19 @@ #ifndef HICN_PROTOCOL_AH_H #define HICN_PROTOCOL_AH_H +#include "../common.h" + /* * The TCP PSH flag is set to indicate TCP payload in fact contains a AH header * with signature information for the packet */ #define AH_FLAG 0x10 +/* + * The length of the AH struct must be 44 bytes. + */ +#define EXPECTED_AH_HDRLEN 44 + typedef struct { u8 nh; // (to match with reserved in IPSEC AH) @@ -49,6 +56,8 @@ typedef struct }; // Unix timestamp indicating when the signature has been calculated u8 timestamp_as_u8[8]; + u16 timestamp_as_u16[4]; + u32 timestamp_as_u32[2]; }; // ICV would follow u8 keyId[32]; // Hash of the pub key @@ -57,6 +66,8 @@ typedef struct } _ah_header_t; #define AH_HDRLEN sizeof(_ah_header_t) +static_assert (EXPECTED_AH_HDRLEN == AH_HDRLEN, + "Size of AH Struct does not match its expected size."); #endif /* HICN_PROTOCOL_AH_H */ diff --git a/lib/src/protocol/icmp.h b/lib/src/protocol/icmp.h index 5a84995b6..36954bb6d 100644 --- a/lib/src/protocol/icmp.h +++ b/lib/src/protocol/icmp.h @@ -22,6 +22,11 @@ #include "../common.h" +/* + * The length of the ICMP header struct must be 4 bytes. + */ +#define EXPECTED_ICMP_HDRLEN 4 + typedef struct { u8 type; @@ -29,6 +34,15 @@ typedef struct u16 csum; } _icmp_header_t; +#define ICMP_HDRLEN sizeof(_icmp_header_t) +static_assert (EXPECTED_ICMP_HDRLEN == ICMP_HDRLEN, + "Size of ICMP struct does not match its expected size."); + +/* + * The length of the ICMPWLDR header struct must be 4 bytes. + */ +#define EXPECTED_ICMPWLDR_HDRLEN 8 + typedef struct { u8 type; @@ -55,7 +69,9 @@ typedef struct }; } _icmp_wldr_header_t; -#define ICMP_HDRLEN sizeof(_icmp_header_t) +#define ICMPWLDR_HDRLEN sizeof(_icmp_wldr_header_t) +static_assert (EXPECTED_ICMPWLDR_HDRLEN == ICMPWLDR_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); #endif /* HICN_PROTOCOL_ICMP_H */ diff --git a/lib/src/protocol/icmprd.h b/lib/src/protocol/icmprd.h index c2f27d673..aa1fa01ae 100644 --- a/lib/src/protocol/icmprd.h +++ b/lib/src/protocol/icmprd.h @@ -21,21 +21,46 @@ #define HICN_PROTOCOL_ICMPRD_H #include "../common.h" +#include "ipv4.h" -typedef struct __attribute__ ((__packed__)) +/* + * The length of the ICMPRD4 header struct must be 92 bytes. + */ +#define EXPECTED_ICMPRD4_HDRLEN 92 + +typedef struct { + u8 type; + u8 code; + u16 csum; ip4_address_t ip; _ipv4_header_t iph; u8 data[64]; } _icmprd4_header_t; -typedef struct __attribute__ ((__packed__)) +#define ICMPRD4_HDRLEN sizeof(_icmprd4_header_t) +static_assert (EXPECTED_ICMPRD4_HDRLEN == ICMPRD4_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); + +/* + * The length of the ICMPRD header struct must be 40 bytes. + */ +#define EXPECTED_ICMPRD_HDRLEN 40 + +typedef struct { + u8 type; + u8 code; + u16 csum; u32 res; ip6_address_t tgt; ip6_address_t dst; } _icmprd_header_t; +#define ICMPRD_HDRLEN sizeof(_icmprd_header_t) +static_assert (EXPECTED_ICMPRD_HDRLEN == ICMPRD_HDRLEN, + "Size of ICMPWLDR struct does not match its expected size."); + #endif /* HICN_PROTOCOL_ICMPRD_H */ /* diff --git a/lib/src/protocol/ipv4.h b/lib/src/protocol/ipv4.h index c57485b6c..8a5b6683b 100644 --- a/lib/src/protocol/ipv4.h +++ b/lib/src/protocol/ipv4.h @@ -22,6 +22,11 @@ /* Headers were adapted from linux' definitions in netinet/ip.h */ +/* + * The length of the IPV4 header struct must be 20 bytes. + */ +#define EXPECTED_IPV4_HDRLEN 20 + typedef struct { union @@ -55,6 +60,13 @@ typedef struct #define ipv4_header_bytes(ipv4_header) (sizeof(u32) * (ipv4_header->version_ihl & 0xf)) #define IPV4_HDRLEN sizeof(_ipv4_header_t) +static_assert (EXPECTED_IPV4_HDRLEN == IPV4_HDRLEN, + "Size of IPV4 struct does not match its expected size."); + +/* + * The length of the IPV4 pseudo header struct must be 12 bytes. + */ +#define EXPECTED_IPV4_PSHDRLEN 12 typedef struct { @@ -66,6 +78,8 @@ typedef struct } ipv4_pseudo_header_t; #define IPV4_PSHDRLEN sizeof(ipv4_pseudo_header_t) +static_assert (EXPECTED_IPV4_PSHDRLEN == IPV4_PSHDRLEN, + "Size of IPV4_PSHDR struct does not match its expected size."); /* Default field values */ #define IPV4_DEFAULT_VERSION 4 diff --git a/lib/src/protocol/ipv6.h b/lib/src/protocol/ipv6.h index 28a1aa47f..5a83abcae 100644 --- a/lib/src/protocol/ipv6.h +++ b/lib/src/protocol/ipv6.h @@ -18,6 +18,11 @@ #include "../common.h" +/* + * The length of the IPV6 header struct must be 40 bytes. + */ +#define EXPECTED_IPV6_HDRLEN 40 + typedef struct { union @@ -35,8 +40,14 @@ typedef struct ip6_address_t daddr; /* destination address */ } _ipv6_header_t; - #define IPV6_HDRLEN sizeof(_ipv6_header_t) +static_assert (EXPECTED_IPV6_HDRLEN == IPV6_HDRLEN, + "Size of IPV6 struct does not match its expected size."); + +/* + * The length of the IPV6 pseudo header struct must be 40 bytes. + */ +#define EXPECTED_IPV6_PSHDRLEN 40 typedef struct { @@ -49,6 +60,8 @@ typedef struct } ipv6_pseudo_header_t; #define IPV6_PSHDRLEN sizeof(ipv6_pseudo_header_t) +static_assert (EXPECTED_IPV6_PSHDRLEN == IPV6_PSHDRLEN, + "Size of IPV6_PSHDR struct does not match its expected size."); /* Default field values */ #define IPV6_DEFAULT_VERSION 6 diff --git a/lib/src/protocol/tcp.h b/lib/src/protocol/tcp.h index 68f4bf8f9..3a15a93b3 100644 --- a/lib/src/protocol/tcp.h +++ b/lib/src/protocol/tcp.h @@ -20,12 +20,17 @@ #include "../common.h" #include "../name.h" +/* + * The length of the TCP header struct must be 20 bytes. + */ +#define EXPECTED_TCP_HDRLEN 20 + /* * NOTE: bitfields are problematic for portability reasons. There are provided * here for reference and documentation purposes, we might just provide a macro * to disable and use it instead of __BYTE_ORDER__. */ -typedef struct __attribute__ ((packed)) +typedef struct { u16 sport; u16 dport; @@ -120,6 +125,8 @@ typedef struct __attribute__ ((packed)) } _tcp_header_t; #define TCP_HDRLEN sizeof(_tcp_header_t) +static_assert (EXPECTED_TCP_HDRLEN == TCP_HDRLEN, + "Size of TCP struct does not match its expected size."); #ifndef HICN_VPP_PLUGIN diff --git a/lib/src/protocol/udp.h b/lib/src/protocol/udp.h index 58cd65095..75d1ea98c 100644 --- a/lib/src/protocol/udp.h +++ b/lib/src/protocol/udp.h @@ -16,6 +16,11 @@ #ifndef HICN_PROTOCOL_UDP_H #define HICN_PROTOCOL_UDP_H +/* + * The length of the UDP header struct must be 8 bytes. + */ +#define EXPECTED_UDP_HDRLEN 8 + typedef struct { u16 src_port; @@ -25,6 +30,8 @@ typedef struct } _udp_header_t; #define UDP_HDRLEN sizeof(_udp_header_t) +static_assert (EXPECTED_UDP_HDRLEN == UDP_HDRLEN, + "Size of UDP struct does not match its expected size."); #endif /* HICN_PROTOCOL_UDP_H */ -- cgit 1.2.3-korg