diff --git a/CMakeLists.txt b/CMakeLists.txt index e41bc1a..3411a4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,5 +7,7 @@ aux_source_directory(fsw/src APP_SRC_FILES) aux_source_directory(fsw/src/formats APP_SRC_FILES) aux_source_directory(fsw/src/services APP_SRC_FILES) +include_directories(${MISSION_SOURCE_DIR}/../components/cryptolib/include) + # Create the app module add_cfe_app(io_lib ${APP_SRC_FILES}) diff --git a/fsw/public_inc/io_lib.h b/fsw/public_inc/io_lib.h index 4da7433..a199554 100644 --- a/fsw/public_inc/io_lib.h +++ b/fsw/public_inc/io_lib.h @@ -22,6 +22,7 @@ #include "cfe.h" #include "common_types.h" #include "network_includes.h" +#include "crypto.h" #ifdef _VXWORKS_OS_ /* For vxworks, include ioLib for ioctl suport */ diff --git a/fsw/public_inc/tm_sdlp.h b/fsw/public_inc/tm_sdlp.h index bd9409f..ca902fe 100644 --- a/fsw/public_inc/tm_sdlp.h +++ b/fsw/public_inc/tm_sdlp.h @@ -37,6 +37,7 @@ extern "C" { *******************************************************************************/ #include "io_lib.h" #include "tmtf.h" +#include "crypto.h" /******************************************************************************* @@ -63,7 +64,7 @@ typedef struct } TM_SDLP_GlobalConfig_t; -/** Following Structure is the user defined managed /configuration parameters +/** Following Structure is the user defined managed / configuration parameters for a a specific Channel (Master or virtual) */ typedef struct { diff --git a/fsw/public_inc/trans_udp.h b/fsw/public_inc/trans_udp.h index 730bccd..534f08e 100644 --- a/fsw/public_inc/trans_udp.h +++ b/fsw/public_inc/trans_udp.h @@ -37,7 +37,7 @@ /* INADDR_NONE: 0xffffffff */ #define IO_TRANS_UDP_INADDR_NONE "255.255.255.255" /* INADDR_ANY: 0x0 */ -#define IO_TRANS_UDP_INADDR_ANY "0.0.0.0" +#define IO_TRANS_UDP_INADDR_ANY "0.0.0.0" /* INADDR_LOOPBACK */ #define IO_TRANS_UDP_INADDR_LOOPBACK "127.0.0.1" #define IO_TRANS_UDP_INPORT_ANY INPORT_ANY diff --git a/fsw/src/services/tm_sdlp.c b/fsw/src/services/tm_sdlp.c index 28b360e..1159a4f 100644 --- a/fsw/src/services/tm_sdlp.c +++ b/fsw/src/services/tm_sdlp.c @@ -104,8 +104,11 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo, uint16 dataFieldOffset; uint16 secHdrLength; uint16 gvcid = 0; + uint8 sdlsSecurityHeaderLength = 0; + uint8 sdlsSecurityTrailerLength = 0; char mutName[OS_MAX_API_NAME]; - + SecurityAssociation_t* sa_ptr = NULL; + if (pGlobalConfig == NULL || pChannelConfig == NULL || pFrameInfo == NULL || pOverflowBuffer == NULL || pTfBuffer == NULL) { @@ -116,7 +119,7 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo, iStatus = TM_SDLP_INVALID_POINTER; goto end_of_function; } - + secHdrLength = pChannelConfig->secHdrLength; /* The secHdr Length must be between 1-63 bytes if present. @@ -132,7 +135,7 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo, iStatus = TM_SDLP_INVALID_LENGTH; goto end_of_function; } - + dataFieldLength = (int32) pGlobalConfig->frameLength; dataFieldOffset = TMTF_PRIHDR_LENGTH; @@ -141,8 +144,45 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo, dataFieldOffset += secHdrLength + 1; } + // Need SA information for security parameter lengths + // Query SA DB for active SA / SDLS parameters + if (sa_if == NULL) // This should not happen, but tested here for safety + { + printf(KRED "ERROR: SA DB Not initalized! -- CRYPTO_LIB_ERR_NO_INIT, Will Exit\n" RESET); + iStatus = CRYPTO_LIB_ERR_NO_INIT; + } + else + { + // CODE REVIEW - Use of MAP_IDs seems non-correct. They exist for TC specifically, but somehow overtime + // we've morphed and have a TYPE_TC and TYPE_TM enum - realistically MAP_IDs are a set of allowable values + // this might take some figurin' + iStatus = sa_if->sa_get_operational_sa_from_gvcid(0, (uint16)pGlobalConfig->scId, (uint16)pChannelConfig->vcId, 0, &sa_ptr); + + if (iStatus != CRYPTO_LIB_SUCCESS) + { + printf(KRED "Error retrieving operational SA. Error code %d. scId = %d, vcId = %d \n" RESET, iStatus, pGlobalConfig->scId, pChannelConfig->vcId); + goto end_of_function; + } + } + + // IF using SDLS + // TODO Review this if_statement + if (1) + { + sdlsSecurityHeaderLength = Crypto_Get_Security_Header_Length(sa_ptr); + dataFieldOffset += sdlsSecurityHeaderLength; + } + + // Reduce available field length based on cumulative offset dataFieldLength -= dataFieldOffset; + // IF using SDLS + if (1) + { + sdlsSecurityTrailerLength = Crypto_Get_Security_Trailer_Length(sa_ptr); + dataFieldLength -= sdlsSecurityTrailerLength; + } + if (pChannelConfig->ocfFlag == true) { dataFieldLength -= TMTF_OCF_LENGTH; @@ -153,6 +193,22 @@ int32 TM_SDLP_InitChannel(TM_SDLP_FrameInfo_t *pFrameInfo, dataFieldLength -= TMTF_ERR_CTRL_FIELD_LENGTH; } +#ifdef TM_DEBUG + printf("TM_SDLP Initializing channel:\n"); + printf("\t Primary header length: \t%d\n", TMTF_PRIHDR_LENGTH); + printf("\t Secondary header length: \t%d\n", secHdrLength); + printf("\t\t SPI Length: 2 bytes\n"); + printf("\t\t IV Length: %d bytes\n", sa_ptr->shivf_len); + printf("\t\t SNF Length Length: %d bytes\n", sa_ptr->shsnf_len); + printf("\t\t PLF Length: %d bytes\nEnable", sa_ptr->shplf_len); + printf("\t Security header length: \t%d\n", sdlsSecurityHeaderLength); + printf("\t Data field offset: \t%d\n", dataFieldOffset); + printf("\t Data field length: \t%d\n", dataFieldLength); + printf("\t Security trailer length: \t%d\n", sdlsSecurityTrailerLength); + printf("\t OCF Length: \t%d HARDCODED - to be changed\n", TMTF_OCF_LENGTH); // Todo, currently hardcoded + printf("\t FECF length: \t%d HARDCODED - to be changed\n", TMTF_ERR_CTRL_FIELD_LENGTH); //Todo, currently hardcoded +#endif + if (dataFieldLength < 0) { CFE_EVS_SendEvent(IO_LIB_TM_SDLP_EID, CFE_EVS_EventType_ERROR, @@ -255,6 +311,12 @@ int32 TM_SDLP_FrameHasData(TM_SDLP_FrameInfo_t *pFrameInfo) hasData = TM_SDLP_INVALID_POINTER; goto end_of_function; } + +#ifdef TM_DEBUG + printf("*** DATA LENGTH INFO!***\n"); + printf("*** Free Octets: %d\n", pFrameInfo->freeOctets); + printf("*** dataFieldLength: %d\n", pFrameInfo->dataFieldLength); +#endif if (pFrameInfo->freeOctets < pFrameInfo->dataFieldLength) { @@ -447,7 +509,7 @@ int32 TM_SDLP_StartFrame(TM_SDLP_FrameInfo_t *pFrameInfo) iStatus = TM_SDLP_FRAME_NOT_INIT; goto end_of_function; } - + OS_MutSemTake(pFrameInfo->mutexId); /* If the frame is already started, issue a warning. */ diff --git a/fsw/src/services/trans_udp.c b/fsw/src/services/trans_udp.c index 9506f87..771b500 100644 --- a/fsw/src/services/trans_udp.c +++ b/fsw/src/services/trans_udp.c @@ -29,6 +29,13 @@ #include "trans_udp.h" +/* Start additional includes for hostname snippet */ +#include +#include //hostent +#include +/* End additional includes for hostname snippet */ + + /** Initialize (create, configure and bind) a UDP Socket */ int32 IO_TransUdpInit(IO_TransUdpConfig_t * config, IO_TransUdp_t * udp) { @@ -110,6 +117,11 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp) "IO_TransUDP Error: Bad config timeout input."); return IO_TRANS_UDP_BAD_INPUT_ERROR; } + + /* Initialize socket address structures */ + CFE_PSP_MemSet((void *) &udp->sockAddr, 0x0, sizeof(struct sockaddr_in)); + CFE_PSP_MemSet((void *) &udp->srcAddr, 0x0, sizeof(struct sockaddr_in)); + CFE_PSP_MemSet((void *) &udp->destAddr, 0x0, sizeof(struct sockaddr_in)); /* Get IP address from cAddr */ /* NOTE: inet_aton errors out if cAddr = "0.0.0.0", the value of @@ -122,8 +134,27 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp) } else { - //status = inet_aton(&config->cAddr[0], (struct in_addr *) &uiAddr); - status = inet_pton(AF_INET, &config->cAddr[0], (struct in_addr *) &uiAddr); + /* + Start hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c + */ + struct hostent *he; + struct in_addr **addr_list; + int i; + + if ( (he = gethostbyname(config->cAddr) ) != NULL) + { + addr_list = (struct in_addr **) he->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++) + { + //Return the first one; + strcpy(config->cAddr, inet_ntoa(*addr_list[i])); + break; + } + } + /* + End hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c + */ + status = inet_aton(&config->cAddr[0], (struct in_addr *) &uiAddr); if (status == INET_ATON_ERROR) { CFE_EVS_SendEvent(IO_LIB_TRANS_UDP_EID, CFE_EVS_EventType_ERROR, @@ -133,11 +164,6 @@ int32 IO_TransUdpConfigSocket(IO_TransUdpConfig_t *config, IO_TransUdp_t *udp) } } - /* Initialize socket address structures */ - CFE_PSP_MemSet((void *) &udp->sockAddr, 0x0, sizeof(struct sockaddr_in)); - CFE_PSP_MemSet((void *) &udp->srcAddr, 0x0, sizeof(struct sockaddr_in)); - CFE_PSP_MemSet((void *) &udp->destAddr, 0x0, sizeof(struct sockaddr_in)); - /* Save UDP Socket Addr structure */ udp->sockAddr.sin_family = AF_INET; udp->sockAddr.sin_addr.s_addr = uiAddr; @@ -245,8 +271,28 @@ int32 IO_TransUdpSetDestAddr(IO_TransUdp_t *udp, char * destAddr, } /* Get IP address from cAddr */ - //status = inet_aton(destAddr, (struct in_addr *) &uiAddr); - status = inet_pton(AF_INET, destAddr, (struct in_addr *) &uiAddr); + /* + Start hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c + */ + struct hostent *he; + struct in_addr **addr_list; + int i; + + if ( (he = gethostbyname(destAddr) ) != NULL) + { + addr_list = (struct in_addr **) he->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++) + { + // Return the first one; + strcpy(destAddr , inet_ntoa(*addr_list[i])); + break; + } + } + /* + End hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c + */ + + status = inet_aton(destAddr, (struct in_addr *) &uiAddr); if (status == INET_ATON_ERROR) { CFE_EVS_SendEvent(IO_LIB_TRANS_UDP_EID, CFE_EVS_EventType_ERROR,