Fix various problems in 4G NAS handling

- rename as_nas_info_t member data to nas_data to have a uniq name to
  trace between complex malloc/free of this pointer
- fix, maybe not perfect, these pais od malloc/free
- remove typedef name changes across transmission of these as_nas_info_t
  over itti, as name changing makes it even more confusing
- remove globals that are likely possible races
- in as_message_decode() fix memcpy size, it is hard to understand the
  initial idea, likely the author beleives a struct of struct makes a
  intermediate pointer
- in network_api.c remove globals (static but used globally)
- remove casts that are there to hide gcc warnings on probable unaligned
  access
- remain a weird mix of packed/unpacked versions of the same typedef,
  but gcc is happy
This commit is contained in:
Laurent THOMAS
2024-10-22 11:05:20 +02:00
parent 8df8de894b
commit e48e38650d
15 changed files with 203 additions and 330 deletions
+18 -34
View File
@@ -80,65 +80,55 @@ int as_message_decode(const char* buffer, as_message_t* msg, int length)
{
LOG_FUNC_IN;
int bytes;
/* pointers to msg fields possibly not aligned because msg points to a packed structure
* Using these possibly unaligned pointers in a function call may trigger alignment errors at run time and
* gcc, from v9, now warns about it. fix these warnings by removing the indirection on data
* (in fact i don't understand this code data seems to be useless...)
*/
Byte_t *data = NULL;
* Using these possibly unaligned pointers in a function call may trigger alignment errors at run time and
* gcc, from v9, now warns about it. fix these warnings by removing the indirection on data
* (in fact i don't understand this code data seems to be useless...)
*/
/* Get the message type */
msg->msgID = *(uint16_t*)(buffer);
bytes = sizeof(uint16_t);
int bytes = 0;
switch (msg->msgID) {
case AS_NAS_ESTABLISH_REQ:
/* NAS signalling connection establish request */
bytes += sizeof(nas_establish_req_t) - sizeof(Byte_t*);
data = msg->msg.nas_establish_req.initialNasMsg.data;
bytes = sizeof(uint16_t) + sizeof(nas_establish_req_t) - sizeof(as_nas_info_t);
break;
case AS_NAS_ESTABLISH_IND:
/* NAS signalling connection establishment indication */
bytes += sizeof(nas_establish_ind_t) - sizeof(Byte_t*);
data = msg->msg.nas_establish_ind.initialNasMsg.data;
bytes = sizeof(uint16_t) + sizeof(nas_establish_ind_t) - sizeof(as_nas_info_t);
break;
case AS_NAS_ESTABLISH_RSP:
/* NAS signalling connection establishment response */
bytes += sizeof(nas_establish_rsp_t) - sizeof(Byte_t*);
data = msg->msg.nas_establish_rsp.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(nas_establish_rsp_t) - sizeof(as_nas_info_t);
break;
case AS_NAS_ESTABLISH_CNF:
/* NAS signalling connection establishment confirm */
bytes += sizeof(nas_establish_cnf_t) - sizeof(Byte_t*);
data = msg->msg.nas_establish_cnf.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(nas_establish_cnf_t) - sizeof(as_nas_info_t);
break;
case AS_UL_INFO_TRANSFER_REQ:
/* Uplink L3 data transfer request */
bytes += sizeof(ul_info_transfer_req_t) - sizeof(Byte_t*);
data = msg->msg.ul_info_transfer_req.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(ul_info_transfer_req_t) - sizeof(as_nas_info_t);
break;
case AS_UL_INFO_TRANSFER_IND:
/* Uplink L3 data transfer indication */
bytes += sizeof(ul_info_transfer_ind_t) - sizeof(Byte_t*);
data = msg->msg.ul_info_transfer_ind.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(ul_info_transfer_ind_t) - sizeof(as_nas_info_t);
break;
case AS_DL_INFO_TRANSFER_REQ:
/* Downlink L3 data transfer request */
bytes += sizeof(dl_info_transfer_req_t) - sizeof(Byte_t*);
data = msg->msg.dl_info_transfer_req.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(dl_info_transfer_req_t) - sizeof(as_nas_info_t);
break;
case AS_DL_INFO_TRANSFER_IND:
/* Downlink L3 data transfer indication */
bytes += sizeof(dl_info_transfer_ind_t) - sizeof(Byte_t*);
data = msg->msg.dl_info_transfer_ind.nasMsg.data;
bytes = sizeof(uint16_t) + sizeof(dl_info_transfer_ind_t) - sizeof(as_nas_info_t);
break;
case AS_BROADCAST_INFO_IND:
@@ -169,14 +159,8 @@ int as_message_decode(const char* buffer, as_message_t* msg, int length)
}
if (bytes > 0) {
if (data) {
/* Set the pointer to dedicated NAS information */
/* wasn't data already computed above for specific cases here we override ?? */
data = (Byte_t *)buffer + bytes;
}
/* Decode the message */
memcpy(msg, (as_message_t*)buffer, bytes);
memcpy(msg, buffer, bytes);
LOG_FUNC_RETURN (msg->msgID);
}
@@ -254,7 +238,7 @@ int as_message_encode(char* buffer, as_message_t* msg, int length)
/* NAS signalling connection establish indication */
bytes += sizeof(nas_establish_ind_t) - sizeof(Byte_t*);
nas_msg = msg->msg.nas_establish_ind.initialNasMsg;
dataptr=(Byte_t *)&(msg->msg.nas_establish_ind.initialNasMsg.data);
dataptr = (Byte_t*)&(msg->msg.nas_establish_ind.initialNasMsg.nas_data);
len=msg->msg.nas_establish_ind.initialNasMsg.length;
break;
@@ -269,7 +253,7 @@ int as_message_encode(char* buffer, as_message_t* msg, int length)
/* NAS signalling connection establish confirm */
bytes += sizeof(nas_establish_cnf_t) - sizeof(Byte_t*);
nas_msg = msg->msg.nas_establish_cnf.nasMsg;
dataptr=(Byte_t *)&(msg->msg.nas_establish_cnf.nasMsg.data);
dataptr = (Byte_t*)&(msg->msg.nas_establish_cnf.nasMsg.nas_data);
len=msg->msg.nas_establish_ind.initialNasMsg.length;
break;
@@ -361,10 +345,10 @@ int as_message_encode(char* buffer, as_message_t* msg, int length)
if ( (dataptr!=NULL) && (len > 0) ) {
/* Copy the NAS message */
memcpy(buffer + bytes, nas_msg.data, nas_msg.length);
memcpy(buffer + bytes, nas_msg.nas_data, nas_msg.length);
bytes += len;
/* Release NAS message memory */
free(nas_msg.data);
free(nas_msg.nas_data);
len=0;
dataptr = NULL;
}
+11 -37
View File
@@ -91,19 +91,12 @@ static struct {
/*
* The buffer used to receive data from the network sublayer
*/
#define NETWORK_API_RECV_BUFFER_SIZE 4096
static char _network_api_recv_buffer[NETWORK_API_RECV_BUFFER_SIZE];
#define NETWORK_API_RECV_BUFFER_SIZE 4096
/*
* The buffer used to send data to the network sublayer
*/
#define NETWORK_API_SEND_BUFFER_SIZE NETWORK_API_RECV_BUFFER_SIZE
static char _network_api_send_buffer[NETWORK_API_SEND_BUFFER_SIZE];
/*
* The decoded data received from the network sublayer
*/
static as_message_t _as_data = {}; /* Access Stratum message */
#define NETWORK_API_SEND_BUFFER_SIZE NETWORK_API_RECV_BUFFER_SIZE
/****************************************************************************/
/****************** E X P O R T E D F U N C T I O N S ******************/
@@ -148,6 +141,7 @@ int network_api_initialize(const char* host, const char* port)
((errno < 0) ? gai_strerror(errno) : strerror(errno)));
LOG_FUNC_RETURN (RETURNerror);
}
char _network_api_send_buffer[NETWORK_API_SEND_BUFFER_SIZE];
gethostname(_network_api_send_buffer, NETWORK_API_SEND_BUFFER_SIZE);
LOG_TRACE(INFO, "NET-API - Network's UDP socket %d is BOUND to %s/%s",
@@ -177,27 +171,6 @@ int network_api_get_fd(void)
LOG_FUNC_RETURN (NETWORK_API_GETFD());
}
/****************************************************************************
** **
** Name: network_api_get_data() **
** **
** Description: Get a generic pointer to the network data structure. **
** Casting to the proper type is necessary before its usage. **
** **
** Inputs: None **
** Others: _as_data **
** **
** Outputs: Return: A generic pointer to the network data **
** structure **
** Others: None **
** **
***************************************************************************/
const void* network_api_get_data(void)
{
LOG_FUNC_IN;
LOG_FUNC_RETURN ((void*)(&_as_data));
}
/****************************************************************************
** **
** Name: network_api_read_data() **
@@ -213,7 +186,7 @@ const void* network_api_get_data(void)
** Others: _network_api_recv_buffer, _network_api_id **
** **
***************************************************************************/
int network_api_read_data(int fd)
int network_api_read_data(int fd, char* _network_api_recv_buffer)
{
LOG_FUNC_IN;
@@ -263,7 +236,7 @@ int network_api_read_data(int fd)
** Others: None **
** **
***************************************************************************/
int network_api_send_data(int fd, int length)
static int network_api_send_data(int fd, int length, char* _network_api_send_buffer)
{
LOG_FUNC_IN;
@@ -346,12 +319,12 @@ void network_api_close(int fd)
** Others: _as_data **
** **
***************************************************************************/
int network_api_decode_data(int length)
int network_api_decode_data(char* _network_api_recv_buffer, int length, as_message_t* _as_data)
{
LOG_FUNC_IN;
/* Decode the Access Stratum message received from the network */
int as_id = as_message_decode(_network_api_recv_buffer, &_as_data, length);
int as_id = as_message_decode(_network_api_recv_buffer, _as_data, length);
if (as_id != RETURNerror) {
LOG_TRACE(INFO, "NET-API - AS message id=0x%x successfully decoded",
@@ -376,7 +349,7 @@ int network_api_decode_data(int length)
** Others: _network_api_send_buffer **
** **
***************************************************************************/
int network_api_encode_data(void* data)
static int network_api_encode_data(void* data, char* _network_api_send_buffer)
{
LOG_FUNC_IN;
@@ -415,9 +388,10 @@ int as_message_send(as_message_t* as_msg)
LOG_TRACE(INFO, "NET-API - Send message 0x%.4x to the Access Stratum "
"layer", as_msg->msgID);
char _network_api_send_buffer[NETWORK_API_SEND_BUFFER_SIZE];
/* Encode the AS message */
bytes = network_api_encode_data(as_msg);
bytes = network_api_encode_data(as_msg, _network_api_send_buffer);
if (bytes > 0) {
/* Get the network file descriptor */
@@ -425,7 +399,7 @@ int as_message_send(as_message_t* as_msg)
if (fd != RETURNerror) {
/* Send the AS message to the network */
bytes = network_api_send_data(fd, bytes);
bytes = network_api_send_data(fd, bytes, _network_api_send_buffer);
}
}
@@ -55,17 +55,14 @@ Description Implements the API used by the NAS layer to send/receive
/****************************************************************************/
/****************** E X P O R T E D F U N C T I O N S ******************/
/****************************************************************************/
#include "openair2/COMMON/as_message.h"
int network_api_initialize(const char* host, const char* port);
int network_api_get_fd(void);
const void* network_api_get_data(void);
int network_api_read_data(int fd);
int network_api_send_data(int fd, int length);
int network_api_read_data(int fd, char *_network_api_recv_buffer);
void network_api_close(int fd);
int network_api_decode_data(int length);
int network_api_encode_data(void* data);
int network_api_decode_data(char *_network_api_recv_buffer, int length, as_message_t *_as_data);
#endif /* __NETWORK_API_H__*/