From c5efeba476ae46c9374baeecd1bed988bf7ff10a Mon Sep 17 00:00:00 2001 From: emanuel Date: Wed, 8 Nov 2023 15:50:34 +0000 Subject: [PATCH] Merge transport to networking --- src/CMakeLists.txt | 1 - src/cam.c | 76 +++++++------- src/cam.h | 2 +- src/config.c | 11 ++- src/cpm.c | 67 ++++++------- src/evm.c | 109 ++++++++++----------- src/evm.h | 3 +- src/facilities.c | 107 +++++--------------- src/facilities.h | 2 +- src/indications.c | 29 ++++-- src/indications.h | 4 +- src/requests.c | 239 ++++++++++++++++++++++++++++----------------- src/requests.h | 6 +- src/saem.c | 63 ++++++------ src/tpm.c | 233 +++++++++++++++++++++++-------------------- src/vcm.c | 82 ++++++++-------- 16 files changed, 524 insertions(+), 510 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55e4e32..2b1a3b3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,7 +16,6 @@ ADD_EXECUTABLE(it2s-itss-facilities TARGET_LINK_LIBRARIES(it2s-itss-facilities -lit2s-asn-itss-facilities - -lit2s-asn-itss-transport -lit2s-asn-itss-security -lit2s-asn-itss-management -lit2s-asn-itss-networking diff --git a/src/cam.c b/src/cam.c index a4d1a32..94e40ac 100644 --- a/src/cam.c +++ b/src/cam.c @@ -1,7 +1,7 @@ #include "cam.h" #include "facilities.h" -#include +#include #include #include #include @@ -804,76 +804,70 @@ static int check_pz() { void* ca_service() { int rv = 0; - TransportRequest_t* tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t* tpr = &tr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; - bpr->btpType = BTPType_btpB; - - bpr->gn.destinationAddress.buf = malloc(6); + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; } - bpr->gn.destinationAddress.size = 6; - - bpr->gn.packetTransportType = PacketTransportType_shb; - - bpr->destinationPort = Port_cam; - - bpr->gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = Port_cam; if (facilities.edm.enabled) { - bpr->destinationPortInfo = calloc(1, sizeof(OCTET_STRING_t)); - bpr->destinationPortInfo->size = 2; - bpr->destinationPortInfo->buf = malloc(2); - *(uint16_t*)bpr->destinationPortInfo->buf = 0xed; + npr->transport.choice.btp.destinationPortInfo = calloc(1, sizeof(OCTET_STRING_t)); + npr->transport.choice.btp.destinationPortInfo->size = 2; + npr->transport.choice.btp.destinationPortInfo->buf = malloc(2); + *(uint16_t*)npr->transport.choice.btp.destinationPortInfo->buf = 0xed; } const int buf_len = 1024; - - bpr->data.buf = malloc(buf_len); + npr->data.buf = malloc(buf_len); // Fill header for FacilitiesIndication and FacilitiesMessageIndication structs - bpr->gn.securityProfile.sign = true; - FacilitiesIndication_t* fi = calloc(1,sizeof(FacilitiesIndication_t)); fi->present = FacilitiesIndication_PR_message; FacilitiesMessageIndication_t* fmi = &fi->choice.message; fmi->itsMessageType = ItsMessageType_cam; fmi->data.buf = malloc(buf_len); - uint8_t tr_oer[buf_len]; + uint8_t nr_oer[buf_len]; uint8_t fi_oer[buf_len]; - tr_oer[0] = 4; // Facilities + nr_oer[0] = 4; // Facilities fi_oer[0] = 4; while (!facilities.exit) { usleep(1000*50); if (lightship_check() && facilities.lightship.active) { - rv = mk_cam(bpr->data.buf, (uint16_t*) &bpr->data.size); + rv = mk_cam(npr->data.buf, (uint16_t*) &npr->data.size); if (rv) { continue; } if (facilities.edm.enabled) { - edm_encap(bpr->data.buf, (uint16_t*) &bpr->data.size, buf_len, Port_cam); + edm_encap(npr->data.buf, (uint16_t*) &npr->data.size, buf_len, Port_cam); } - memcpy(fmi->data.buf, bpr->data.buf, bpr->data.size); - fmi->data.size = bpr->data.size; + memcpy(fmi->data.buf, npr->data.buf, npr->data.size); + fmi->data.size = npr->data.size; // Check if inside PZ - bpr->gn.communicationProfile = 0; - if (facilities.station_type != 15 && check_pz()) bpr->gn.communicationProfile = 1; + npr->network.choice.gn.communicationProfile = 0; + if (facilities.station_type != 15 && check_pz()) npr->network.choice.gn.communicationProfile = 1; - uint32_t id = itss_id(bpr->data.buf, bpr->data.size); - bpr->id = id; + uint32_t id = itss_id(npr->data.buf, npr->data.size); + npr->id = id; fmi->id = id; - asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer+1, 1023); + asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, nr_oer+1, 1023); if (enc.encoded == -1) { log_error("encoding TR for cam failed"); continue; @@ -885,7 +879,7 @@ void* ca_service() { continue; } - itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded+1, ITSS_TRANSPORT, id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, nr_oer, enc.encoded+1, ITSS_NETWORKING, id, "NR.packet.btp"); itss_queue_send(facilities.tx_queue, fi_oer, enc_fdi.encoded+1, ITSS_APPLICATIONS, id, "FI.message"); @@ -896,7 +890,7 @@ void* ca_service() { pthread_mutex_lock(&facilities.id.lock); uint32_t station_id = facilities.id.station_id; pthread_mutex_unlock(&facilities.id.lock); - itss_db_add(facilities.logging.dbms, station_id, bpr->id, true, messageID_cam, NULL, bpr->data.buf, bpr->data.size); + itss_db_add(facilities.logging.dbms, station_id, npr->id, true, messageID_cam, NULL, npr->data.buf, npr->data.size); } if (facilities.logging.recorder) { @@ -905,8 +899,8 @@ void* ca_service() { int e = itss_management_record_packet_sdu( buffer, buffer_len, - bpr->data.buf, - bpr->data.size, + npr->data.buf, + npr->data.size, id, itss_time_get(), ITSS_FACILITIES, @@ -919,7 +913,7 @@ void* ca_service() { } - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); return NULL; diff --git a/src/cam.h b/src/cam.h index 2777d31..8385290 100644 --- a/src/cam.h +++ b/src/cam.h @@ -7,7 +7,7 @@ #include #include -#include +#include #include diff --git a/src/config.c b/src/config.c index 42ee2ca..e6aef44 100644 --- a/src/config.c +++ b/src/config.c @@ -147,17 +147,18 @@ int facilities_config() { goto cleanup; } - // Fetch [transport] address - int index = fetch_target_address(etsi_its_cfg->transport.zmq.addresses, etsi_its_cfg->transport.zmq.addresses_len); + // Fetch [networking] address + int index = fetch_target_address(etsi_its_cfg->networking.zmq.addresses, etsi_its_cfg->networking.zmq.addresses_len); if (index != -1) { - facilities.zmq.transport_address = malloc(strlen(etsi_its_cfg->transport.zmq.addresses[index])+1); - strcpy(facilities.zmq.transport_address, etsi_its_cfg->transport.zmq.addresses[index]); + facilities.zmq.networking_address = malloc(strlen(etsi_its_cfg->networking.zmq.addresses[index])+1); + strcpy(facilities.zmq.networking_address, etsi_its_cfg->networking.zmq.addresses[index]); } else { - log_error("[config] a valid address for [transport] was not found"); + log_error("[config] a valid address for [networking] was not found"); rv = 1; goto cleanup; } + // Fetch [applications] address index = fetch_target_address(itss_cfg->applications.zmq.addresses, itss_cfg->applications.zmq.addresses_len); if (index != -1) { diff --git a/src/cpm.c b/src/cpm.c index 78d960e..7f27963 100644 --- a/src/cpm.c +++ b/src/cpm.c @@ -2,7 +2,7 @@ #include "facilities.h" #include -#include +#include #include #include #include @@ -708,11 +708,23 @@ void *cp_service(){ tr_oer[0] = 4; //Facilities fi_oer[0] = 4; - TransportRequest_t* tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t* tpr = &tr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; + + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); + for (int i = 0; i < 6; ++i) { + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; + } + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; + + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = Port_cpm; FacilitiesIndication_t* fi = calloc(1, sizeof(FacilitiesIndication_t)); fi->present = FacilitiesIndication_PR_message; @@ -721,23 +733,8 @@ void *cp_service(){ roadRotationSin = sin(((facilities.dissemination.radar_rotation + 90.0) * PI) / 180); roadRotationCos = cos(((facilities.dissemination.radar_rotation + 90.0) * PI) / 180); - /*--- Fill mandatory BTP Data Request parameters ---*/ - bpr->gn.destinationAddress.buf = malloc(6); - bpr->gn.destinationAddress.size = 6; - for(int i = 0; i < 6; i++) - bpr->gn.destinationAddress.buf[i] = 0xff; //Broadcast addr - - - bpr->btpType = BTPType_btpB; //BTP Type B is for non-interactive packet transport | BTP Type A is for interactive packet transport - //The former doesn't have a source port and the latter have - - bpr->destinationPort = Port_cpm; //CPM entity port for communication between Facilities and Transport - bpr->gn.packetTransportType = PacketTransportType_shb; //shb = Single Hop Broadcast packet - bpr->gn.trafficClass = 2; //Identifier assigned to a GeoNetworking packet that expresses its requirements on data transport - bpr->data.buf = malloc(1500); //CPM Data to be sent to the Transport layer - - bpr->gn.securityProfile.sign = true; + npr->data.buf = malloc(1500); //CPM Data to be sent to the Networking layer /*--- Fill mandatory Facilities Message Indication parameters ---*/ fmi->itsMessageType = ItsMessageType_cpm; @@ -763,15 +760,15 @@ void *cp_service(){ parse_input(au8_readBuffer,i32_recv_bytes); /* CPM build and encoding to BDR and FDI */ - if(mk_cpm(bpr->data.buf, (uint32_t *) &bpr->data.size, fmi->data.buf, (uint32_t *) &fmi->data.size, history_list, valid_array, history_timestamp) == 1) + if(mk_cpm(npr->data.buf, (uint32_t *) &npr->data.size, fmi->data.buf, (uint32_t *) &fmi->data.size, history_list, valid_array, history_timestamp) == 1) continue; - uint32_t id = itss_id(bpr->data.buf, bpr->data.size); - bpr->id = id; + uint32_t id = itss_id(npr->data.buf, npr->data.size); + npr->id = id; fmi->id = id; - /* Encode TransportRequest */ - asn_enc_rval_t enc_tdr = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer+1, 2047); + /* Encode NetworkingRequest */ + asn_enc_rval_t enc_tdr = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, tr_oer+1, 2047); if(enc_tdr.encoded == -1){ log_error("encoding TR for cpm failed"); continue; @@ -784,8 +781,8 @@ void *cp_service(){ continue; } - /* Create thread to send packet to the Transport Layer (=3) */ - itss_queue_send(facilities.tx_queue, tr_oer, enc_tdr.encoded+1, ITSS_TRANSPORT, id, "TR.packet.btp"); + /* Create thread to send packet to the Networking Layer (=3) */ + itss_queue_send(facilities.tx_queue, tr_oer, enc_tdr.encoded+1, ITSS_NETWORKING, id, "NR.packet.btp"); /* Create thread to send packet to the Applications Layer (=5) */ itss_queue_send(facilities.tx_queue, fi_oer, enc_fdi.encoded+1, ITSS_APPLICATIONS, id, "FI.message"); @@ -798,7 +795,7 @@ void *cp_service(){ pthread_mutex_lock(&facilities.id.lock); uint32_t station_id = facilities.id.station_id; pthread_mutex_unlock(&facilities.id.lock); - itss_db_add(facilities.logging.dbms, station_id, bpr->id, true, 14, NULL, bpr->data.buf, bpr->data.size); + itss_db_add(facilities.logging.dbms, station_id, npr->id, true, 14, NULL, npr->data.buf, npr->data.size); } if (facilities.logging.recorder) { uint16_t buffer_len = 2048; @@ -806,14 +803,14 @@ void *cp_service(){ int e = itss_management_record_packet_sdu( buffer, buffer_len, - bpr->data.buf, - bpr->data.size, - bpr->id, + npr->data.buf, + npr->data.size, + npr->id, itss_time_get(), ITSS_FACILITIES, true); if (e != -1) { - itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, bpr->id, "MReq.packet.set"); + itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, npr->id, "MReq.packet.set"); } } @@ -823,7 +820,7 @@ void *cp_service(){ } } - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); /* Close sockets */ diff --git a/src/evm.c b/src/evm.c index b24abaa..df6ea25 100644 --- a/src/evm.c +++ b/src/evm.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include @@ -119,31 +119,29 @@ static uint64_t reservation_id = 1; int evrsrm_recv(EV_RSR_t *evrsr_request) { int rv = 0; - TransportRequest_t *tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t *tpr = &tr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; - bpr->btpType = BTPType_btpB; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; - bpr->gn.destinationAddress.buf = malloc(6); + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; } - bpr->gn.destinationAddress.size = 6; + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; - bpr->gn.packetTransportType = PacketTransportType_shb; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = Port_evrsr; - bpr->destinationPort = Port_evrsr; - - bpr->gn.trafficClass = 2; - - bpr->data.buf = malloc(512); + npr->data.buf = malloc(512); // Fill header for FacilitiesIndication and FacilitiesMessageIndication structs - bpr->gn.securityProfile.sign = true; FacilitiesIndication_t *fi = calloc(1, sizeof(FacilitiesIndication_t)); fi->present = FacilitiesIndication_PR_message; @@ -164,29 +162,25 @@ int evrsrm_recv(EV_RSR_t *evrsr_request) { } int evrsr_response; if (evrsr_request->messageBody.present == EV_RSR_MessageBody_PR_preReservationRequestMessage) - evrsr_response = evrsrm_pre_reservation_response(bpr->data.buf, (uint32_t *)&bpr->data.size); + evrsr_response = evrsrm_pre_reservation_response(npr->data.buf, (uint32_t *)&npr->data.size); else if (evrsr_request->messageBody.present == EV_RSR_MessageBody_PR_reservationRequestMessage) - evrsr_response = evrsrm_reservation_response(evrsr_request, bpr->data.buf, (uint32_t *)&bpr->data.size); + evrsr_response = evrsrm_reservation_response(evrsr_request, npr->data.buf, (uint32_t *)&npr->data.size); else if (evrsr_request->messageBody.present == EV_RSR_MessageBody_PR_cancellationRequestMessage) - evrsr_response = evrsrm_cancellation_response(evrsr_request, bpr->data.buf, (uint32_t *)&bpr->data.size); + evrsr_response = evrsrm_cancellation_response(evrsr_request, npr->data.buf, (uint32_t *)&npr->data.size); else if (evrsr_request->messageBody.present == EV_RSR_MessageBody_PR_updateRequestMessage) - evrsr_response = evrsrm_update_response(evrsr_request, bpr->data.buf, (uint32_t *)&bpr->data.size); + evrsr_response = evrsrm_update_response(evrsr_request, npr->data.buf, (uint32_t *)&npr->data.size); if (evrsr_response != 0) { rv = 1; goto cleanup; } - memcpy(fmi->data.buf, bpr->data.buf, bpr->data.size); - fmi->data.size = bpr->data.size; + memcpy(fmi->data.buf, npr->data.buf, npr->data.size); + fmi->data.size = npr->data.size; - // Check if inside PZ - bpr->gn.communicationProfile = 0; - if (facilities.station_type != 15) bpr->gn.communicationProfile = 1; - - uint32_t id = itss_id(bpr->data.buf, bpr->data.size); - bpr->id = id; + uint32_t id = itss_id(npr->data.buf, npr->data.size); + npr->id = id; fmi->id = id; - asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer + 1, 1023); + asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, tr_oer + 1, 1023); if (enc.encoded == -1) { log_error("[ev] failed encoding transport request (%s)", enc.failed_type->name); rv = 1; @@ -199,7 +193,7 @@ int evrsrm_recv(EV_RSR_t *evrsr_request) { rv = 1; goto cleanup; } - itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_TRANSPORT, id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_NETWORKING, id, "NR.packet.btp"); itss_queue_send(facilities.tx_queue, fi_oer, enc_fdi.encoded + 1, ITSS_APPLICATIONS, id, "FI.message"); @@ -382,32 +376,29 @@ enum EVM_CHECK_R check_evrsrm(BTPPacketIndication_t *bpi, EV_RSR_t *evrsrm, uint void *evcsn_service() { int rv = 0; - TransportRequest_t *tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t *tpr = &tr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; - bpr->btpType = BTPType_btpB; - - bpr->gn.destinationAddress.buf = malloc(6); + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; } - bpr->gn.destinationAddress.size = 6; + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; - bpr->gn.packetTransportType = PacketTransportType_shb; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = Port_poi; - bpr->destinationPort = Port_poi; - bpr->gn.trafficClass = 2; - - bpr->data.buf = malloc(512); + npr->data.buf = malloc(512); // Fill header for FacilitiesIndication and FacilitiesMessageIndication structs - bpr->gn.securityProfile.sign = true; - FacilitiesIndication_t *fi = calloc(1, sizeof(FacilitiesIndication_t)); fi->present = FacilitiesIndication_PR_message; FacilitiesMessageIndication_t *fmi = &fi->choice.message; @@ -421,18 +412,18 @@ void *evcsn_service() { while (!facilities.exit) { usleep(1000 * 1000); if (facilities.evm_args.activate) { - rv = mk_evcsnm(bpr->data.buf, (uint32_t *)&bpr->data.size); + rv = mk_evcsnm(npr->data.buf, (uint32_t *)&npr->data.size); if (rv) { continue; } - memcpy(fmi->data.buf, bpr->data.buf, bpr->data.size); - fmi->data.size = bpr->data.size; + memcpy(fmi->data.buf, npr->data.buf, npr->data.size); + fmi->data.size = npr->data.size; - uint32_t id = itss_id(bpr->data.buf, bpr->data.size); - bpr->id = id; + uint32_t id = itss_id(npr->data.buf, npr->data.size); + npr->id = id; fmi->id = id; - asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer + 1, 1023); + asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, tr_oer + 1, 1023); if (enc.encoded == -1) { log_error("encoding TR for evcsnm failed"); continue; @@ -444,7 +435,7 @@ void *evcsn_service() { continue; } - itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_TRANSPORT, id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_NETWORKING, id, "NR.packet.btp"); itss_queue_send(facilities.tx_queue, fi_oer, enc_fdi.encoded + 1, ITSS_APPLICATIONS, id, "FI.message"); @@ -453,7 +444,7 @@ void *evcsn_service() { pthread_mutex_lock(&facilities.id.lock); uint32_t station_id = facilities.id.station_id; pthread_mutex_unlock(&facilities.id.lock); - itss_db_add(facilities.logging.dbms, station_id, bpr->id, true, messageID_evcsn, NULL, bpr->data.buf, bpr->data.size); + itss_db_add(facilities.logging.dbms, station_id, npr->id, true, messageID_evcsn, NULL, npr->data.buf, npr->data.size); } if (facilities.logging.recorder) { @@ -462,8 +453,8 @@ void *evcsn_service() { int e = itss_management_record_packet_sdu( buffer, buffer_len, - bpr->data.buf, - bpr->data.size, + npr->data.buf, + npr->data.size, id, itss_time_get(), ITSS_FACILITIES, @@ -475,7 +466,7 @@ void *evcsn_service() { } } - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); return NULL; } diff --git a/src/evm.h b/src/evm.h index da7918c..eee40dc 100644 --- a/src/evm.h +++ b/src/evm.h @@ -3,13 +3,14 @@ #include #include -#include #include #include #include #include #include +#include + enum EVM_CHECK_R { EVM_OK, EVM_INVALID, diff --git a/src/facilities.c b/src/facilities.c index e2e3ea7..b8b1028 100644 --- a/src/facilities.c +++ b/src/facilities.c @@ -13,8 +13,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -43,14 +43,14 @@ facilities_t facilities = {0}; -static int transport_indication(void *responder, void **security_socket, uint8_t *msg, uint32_t msg_len) { +static int networking_indication(void *responder, void **security_socket, uint8_t *msg, uint32_t msg_len) { int rv = 0; uint8_t code = 0; bool stored = false; - TransportIndication_t *ti = calloc(1, sizeof(TransportIndication_t)); + NetworkingIndication_t *ni = calloc(1, sizeof(NetworkingIndication_t)); - asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_TransportIndication, (void **)&ti, msg, msg_len); + asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_NetworkingIndication, (void **)&ni, msg, msg_len); if (dec.code) { log_error("<- invalid TI received"); rv = 1; @@ -61,45 +61,21 @@ static int transport_indication(void *responder, void **security_socket, uint8_t itss_0send(responder, &code, 1); - switch (ti->present) { - case TransportIndication_PR_packet: + switch (ni->present) { + case NetworkingIndication_PR_packet: + networking_packet_indication(&ni->choice.packet, security_socket); break; - case TransportIndication_PR_data: - transport_data_indication(&ti->choice.data, security_socket); + case NetworkingIndication_PR_data: + networking_data_indication(&ni->choice.data, security_socket); goto cleanup; default: - log_debug("<- unrecognized TI.choice received"); + log_debug("<- unrecognized NI.choice received"); rv = 1; goto cleanup; } - TransportPacketIndication_t *tpi = &ti->choice.packet; // TODO - // - - - switch (tpi->present) { - case TransportPacketIndication_PR_btp: - log_debug("<- TI.packet.btp | id:%08x size:%dB", tpi->choice.btp.id, msg_len); - transport_indication_btp(&tpi->choice.btp, security_socket); - break; - - case TransportPacketIndication_PR_tcp: - log_debug("<- TI.packet.tcp | id:%ld size:%dB", tpi->choice.tcp.id, msg_len); - transport_indication_tcp(&tpi->choice.tcp, security_socket); - break; - - case TransportPacketIndication_PR_udp: - log_debug("<- TI.packet.udp | id:%ld size:%dB", tpi->choice.udp.id, msg_len); - // TODO - break; - - default: - break; - } - - cleanup: - ASN_STRUCT_FREE(asn_DEF_TransportIndication, ti); + ASN_STRUCT_FREE(asn_DEF_NetworkingIndication, ni); return rv; } @@ -280,39 +256,6 @@ cleanup: return rv; } -static int networking_indication(void *responder, uint8_t *msg, uint32_t msg_len) { - int rv = 0; - - uint8_t code = 0; - - NetworkingIndication_t *ni = calloc(1, sizeof(NetworkingIndication_t)); - - asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_NetworkingIndication, (void **)&ni, msg, msg_len); - if (dec.code) { - rv = 1; - code = 1; - itss_0send(responder, &code, 1); - goto cleanup; - } - itss_0send(responder, &code, 1); - - if (ni->present != NetworkingIndication_PR_data) { - goto cleanup; - } - - if (ni->choice.data.mobileNeighbour && - *ni->choice.data.mobileNeighbour) { - pthread_mutex_lock(&facilities.lightship.lock); - facilities.lightship.t_last_vehicle = itss_time_get(); - facilities.lightship.is_vehicle_near = true; - pthread_mutex_unlock(&facilities.lightship.lock); - } - -cleanup: - ASN_STRUCT_FREE(asn_DEF_NetworkingIndication, ni); - - return rv; -} static int management_indication(void *responder, uint8_t *msg, uint32_t msg_len) { int rv = 0; @@ -374,7 +317,7 @@ void *tx() { uint8_t code; void *applications_socket = itss_0connect(facilities.zmq.applications_address, ZMQ_REQ); - void *transport_socket = itss_0connect(facilities.zmq.transport_address, ZMQ_REQ); + void *networking_socket = itss_0connect(facilities.zmq.networking_address, ZMQ_REQ); void *management_socket = itss_0connect(facilities.zmq.management_address, ZMQ_REQ); itss_queue_t *stream = itss_queue_new(); @@ -398,13 +341,13 @@ void *tx() { for (int i = 0; i < stream->len; ++i) { switch (stream->destination[i]) { - case ITSS_TRANSPORT: - log_debug("-> %s ->[transport] | id:%08x size:%dB", + case ITSS_NETWORKING: + log_debug("-> %s ->[networking] | id:%08x size:%dB", stream->info_msg[i], (uint32_t)stream->id[i], stream->packet_len[i]); - itss_0send(transport_socket, stream->packet[i], stream->packet_len[i]); - rv = itss_0recv_rt(&transport_socket, &code, 1, stream->packet[i], stream->packet_len[i], 1000); + itss_0send(networking_socket, stream->packet[i], stream->packet_len[i]); + rv = itss_0recv_rt(&networking_socket, &code, 1, stream->packet[i], stream->packet_len[i], 1000); if (rv == -1) { - log_error("-> %s ->[transport] | id:%08x size:%dB ", + log_error("-> %s ->[networking] | id:%08x size:%dB ", stream->info_msg[i], (uint32_t)stream->id[i], stream->packet_len[i]); } break; @@ -430,7 +373,7 @@ void *tx() { } } - itss_0close(transport_socket); + itss_0close(networking_socket); itss_0close(management_socket); itss_0close(applications_socket); @@ -523,10 +466,6 @@ int main() { break; case ITSS_NETWORKING: - networking_indication(facilities.zmq.responders[i].socket, buffer + 1, rl); - break; - - case ITSS_TRANSPORT: in_idchange = true; pthread_mutex_lock(&facilities.id.change.lock); if (facilities.id.change.stage == ID_CHANGE_INACTIVE) { @@ -536,7 +475,7 @@ int main() { pthread_mutex_unlock(&facilities.id.change.lock); if (!in_idchange) { - transport_indication(facilities.zmq.responders[i].socket, &security_socket, buffer + 1, rl); + networking_indication(facilities.zmq.responders[i].socket, &security_socket, buffer + 1, rl-1); pthread_mutex_lock(&facilities.id.change.lock); facilities.id.change.stage = ID_CHANGE_INACTIVE; @@ -557,7 +496,7 @@ int main() { pthread_mutex_unlock(&facilities.id.change.lock); if (!in_idchange) { - facilities_request(facilities.zmq.responders[i].socket, buffer + 1, rl); + facilities_request(facilities.zmq.responders[i].socket, buffer + 1, rl-1); pthread_mutex_lock(&facilities.id.change.lock); facilities.id.change.stage = ID_CHANGE_INACTIVE; @@ -570,11 +509,11 @@ int main() { break; case ITSS_MANAGEMENT: - management_indication(facilities.zmq.responders[i].socket, buffer + 1, rl); + management_indication(facilities.zmq.responders[i].socket, buffer + 1, rl-1); break; case ITSS_SECURITY: - security_indication(facilities.zmq.responders[i].socket, buffer + 1, rl); + security_indication(facilities.zmq.responders[i].socket, buffer + 1, rl-1); break; default: diff --git a/src/facilities.h b/src/facilities.h index f39260f..67e6996 100644 --- a/src/facilities.h +++ b/src/facilities.h @@ -43,8 +43,8 @@ typedef struct facilities { zmq_pollitem_t* responders; uint16_t n_responders; + char* networking_address; char* applications_address; - char* transport_address; char* security_address; char* management_address; } zmq; diff --git a/src/indications.c b/src/indications.c index 642d3a3..89acf26 100644 --- a/src/indications.c +++ b/src/indications.c @@ -43,20 +43,37 @@ static void tcp_conn_reset(TCPConnRSTInfo_t* cri, void** security_socket) { ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq); } -int transport_data_indication(TransportDataIndication_t* tdi, void** security_socket) { +int networking_data_indication(NetworkingDataIndication_t* ndi, void** security_socket) { int rv = 0; - switch (tdi->present) { - case TransportDataIndication_PR_tcp: - switch (tdi->choice.tcp.present) { - case TCPDataIndication_PR_connInfoReset: - tcp_conn_reset(&tdi->choice.tcp.choice.connInfoReset, security_socket); + switch (ndi->present) { + case NetworkingDataIndication_PR_transport: + switch (ndi->choice.transport.present) { + case NetworkingDataIndicationTP_PR_tcp: + switch (ndi->choice.transport.choice.tcp.present) { + case TCPDataIndication_PR_connInfoReset: + tcp_conn_reset(&ndi->choice.transport.choice.tcp.choice.connInfoReset, security_socket); + break; + default: + rv = 1; + goto cleanup; + } break; default: rv = 1; goto cleanup; } break; + case NetworkingDataIndication_PR_network: + if (ndi->choice.network.mobileNeighbour && + *ndi->choice.network.mobileNeighbour) { + pthread_mutex_lock(&facilities.lightship.lock); + facilities.lightship.t_last_vehicle = itss_time_get(); + facilities.lightship.is_vehicle_near = true; + pthread_mutex_unlock(&facilities.lightship.lock); + } + break; + default: rv = 1; goto cleanup; diff --git a/src/indications.h b/src/indications.h index 11e0420..0987e38 100644 --- a/src/indications.h +++ b/src/indications.h @@ -1,6 +1,6 @@ #pragma once #include "facilities.h" -#include +#include -int transport_data_indication(TransportDataIndication_t* tpi, void** security_socket); +int networking_data_indication(NetworkingDataIndication_t* tpi, void** security_socket); diff --git a/src/requests.c b/src/requests.c index 95083f6..12badb4 100644 --- a/src/requests.c +++ b/src/requests.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include @@ -84,11 +84,11 @@ int facilities_request_result_rejected(void *responder) { int facilities_request_single_message(void *responder, FacilitiesMessageRequest_t *frm) { int rv = 0; - TransportRequest_t *tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t *tpr = &tr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; void *its_msg = NULL; asn_TYPE_descriptor_t *its_msg_def = NULL; @@ -103,42 +103,42 @@ int facilities_request_single_message(void *responder, FacilitiesMessageRequest_ case ItsMessageType_cam: its_msg_def = &asn_DEF_CAM; its_msg = calloc(1, sizeof(CAM_t)); - bpr->destinationPort = Port_cam; - bpr->gn.packetTransportType = PacketTransportType_shb; - bpr->gn.trafficClass = 1; + npr->transport.choice.btp.destinationPort = Port_cam; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.trafficClass = 1; break; case ItsMessageType_denm: its_msg_def = &asn_DEF_DENM; its_msg = calloc(1, sizeof(DENM_t)); - bpr->destinationPort = Port_denm; - bpr->gn.packetTransportType = PacketTransportType_gbc; - bpr->gn.trafficClass = 2; + npr->transport.choice.btp.destinationPort = Port_denm; + npr->network.choice.gn.packetTransportType = PacketTransportType_gbc; + npr->network.choice.gn.trafficClass = 2; break; case ItsMessageType_ivim: its_msg_def = &asn_DEF_IVIM; its_msg = calloc(1, sizeof(IVIM_t)); - bpr->destinationPort = Port_ivim; - bpr->gn.packetTransportType = PacketTransportType_shb; - bpr->gn.trafficClass = 1; + npr->transport.choice.btp.destinationPort = Port_ivim; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.trafficClass = 1; break; case ItsMessageType_cpm: its_msg_def = &asn_DEF_CPM; its_msg = calloc(1, sizeof(CPM_t)); - bpr->destinationPort = Port_cpm; - bpr->gn.packetTransportType = PacketTransportType_shb; - bpr->gn.trafficClass = 2; + npr->transport.choice.btp.destinationPort = Port_cpm; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.trafficClass = 2; break; case ItsMessageType_evrsr: its_msg_def = &asn_DEF_EV_RSR; its_msg = calloc(1, sizeof(EV_RSR_t)); - bpr->destinationPort = Port_evrsr; - bpr->gn.packetTransportType = PacketTransportType_shb; - bpr->gn.trafficClass = 1; + npr->transport.choice.btp.destinationPort = Port_evrsr; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.trafficClass = 1; break; default: @@ -323,58 +323,58 @@ int facilities_request_single_message(void *responder, FacilitiesMessageRequest_ // Respond to [applications] facilities_request_result_accepted(responder); - // Forward message to [transport] + // Forward message to [networking] if (fwd) { - bpr->btpType = BTPType_btpB; + npr->transport.choice.btp.btpType = BTPType_btpB; - bpr->id = id; + npr->id = id; - bpr->data.buf = malloc(2048); - asn_enc_rval_t enc = uper_encode_to_buffer(its_msg_def, NULL, its_msg, bpr->data.buf, 2048); + npr->data.buf = malloc(2048); + asn_enc_rval_t enc = uper_encode_to_buffer(its_msg_def, NULL, its_msg, npr->data.buf, 2048); if (enc.encoded == -1) { log_error("failed encoding ITS message into UPER (%s)", enc.failed_type->name); rv = 1; goto cleanup; } - bpr->data.size = (enc.encoded + 7) / 8; + npr->data.size = (enc.encoded + 7) / 8; - bpr->gn.destinationAddress.buf = malloc(6); + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) - bpr->gn.destinationAddress.buf[i] = 0xff; - bpr->gn.destinationAddress.size = 6; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.size = 6; if (transmission_start) { - bpr->gn.repetitionStart = malloc(sizeof(long)); - *bpr->gn.repetitionStart = transmission_start; + npr->network.choice.gn.repetitionStart = malloc(sizeof(long)); + *npr->network.choice.gn.repetitionStart = transmission_start; } if (transmission_interval) { - bpr->gn.repetitionInterval = malloc(sizeof(long)); - *bpr->gn.repetitionInterval = transmission_interval; + npr->network.choice.gn.repetitionInterval = malloc(sizeof(long)); + *npr->network.choice.gn.repetitionInterval = transmission_interval; } if (transmission_duration) { - bpr->gn.maximumRepetitionTime = malloc(sizeof(long)); - *bpr->gn.maximumRepetitionTime = transmission_duration; + npr->network.choice.gn.maximumRepetitionTime = malloc(sizeof(long)); + *npr->network.choice.gn.maximumRepetitionTime = transmission_duration; } if (is_update) { - bpr->gn.isUpdate = malloc(sizeof(long)); - *bpr->gn.isUpdate = 1; + npr->network.choice.gn.isUpdate = malloc(sizeof(long)); + *npr->network.choice.gn.isUpdate = 1; } - bpr->gn.securityProfile.sign = true; + npr->network.choice.gn.securityProfile.sign = true; uint8_t tr_oer[2048]; tr_oer[0] = 4; // [facilities] service id - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer + 1, 2047); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, tr_oer + 1, 2047); if (enc.encoded == -1) { log_error("failed encoding TR (%s)", enc.failed_type->name); rv = 1; goto cleanup; } - itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_TRANSPORT, id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded + 1, ITSS_NETWORKING, id, "NR.packet.btp"); } // Logging @@ -398,7 +398,7 @@ int facilities_request_single_message(void *responder, FacilitiesMessageRequest_ cleanup: if (its_msg_def && !managed_msg) ASN_STRUCT_FREE(*its_msg_def, its_msg); - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); return rv; } @@ -629,9 +629,11 @@ cleanup: return rv; } -int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) { +static int networking_packet_indication_btp(NetworkingPacketIndication_t* npi, void** security_socket) { int rv = 0; + BTPPacketIndication_t* bpi = &npi->transport.choice.btp; + FacilitiesIndication_t *fi = NULL; asn_TYPE_descriptor_t *its_msg_descriptor = NULL; @@ -707,7 +709,7 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) goto cleanup; } - asn_dec_rval_t dec = uper_decode_complete(NULL, its_msg_descriptor, (void **)&its_msg, bpi->data.buf, bpi->data.size); + asn_dec_rval_t dec = uper_decode_complete(NULL, its_msg_descriptor, (void **)&its_msg, npi->data.buf, npi->data.size); if (dec.code) { log_debug("<- invalid %s received", its_msg_descriptor->name); rv = 1; @@ -717,13 +719,30 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) // Get permisisons uint8_t *ssp = NULL; uint16_t ssp_len; - if (bpi->gn.securityPermissions) { - ssp = bpi->gn.securityPermissions->ssp.buf; - ssp_len = bpi->gn.securityPermissions->ssp.size; - } + uint8_t* neighbour_cert = NULL; - // Get neighbour certificate ID - uint8_t *neighbour_cert = bpi->gn.securityNeighbour ? bpi->gn.securityNeighbour->buf : NULL; + switch (npi->network.present) { + case NetworkingPacketRequestNW_PR_gn: + if (npi->network.choice.gn.securityPermissions) { + ssp = npi->network.choice.gn.securityPermissions->ssp.buf; + ssp_len = npi->network.choice.gn.securityPermissions->ssp.size; + } + if (npi->network.choice.gn.securityNeighbour) { + neighbour_cert = npi->network.choice.gn.securityNeighbour->buf; + } + break; + case NetworkingPacketRequestNW_PR_gn6a: + if (npi->network.choice.gn6a.gn.securityPermissions) { + ssp = npi->network.choice.gn6a.gn.securityPermissions->ssp.buf; + ssp_len = npi->network.choice.gn6a.gn.securityPermissions->ssp.size; + } + if (npi->network.choice.gn6a.gn.securityNeighbour) { + neighbour_cert = npi->network.choice.gn6a.gn.securityNeighbour->buf; + } + break; + default: + break; + } // Manage message switch (bpi->destinationPort) { @@ -794,7 +813,7 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) case 7011: if (facilities.tolling.protocol.p == TOLLING_PROTOCOL_GN_SPKI) { /* do not wait for facilities process if spki */ - fwd_to_apps(bpi->data.buf, bpi->data.size, bpi->destinationPort, bpi->id); + fwd_to_apps(npi->data.buf, npi->data.size, bpi->destinationPort, npi->id); fwd = false; } else { fwd = true; @@ -805,7 +824,7 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) break; case 2043: - fwd_to_apps(bpi->data.buf, bpi->data.size, bpi->destinationPort, bpi->id); + fwd_to_apps(npi->data.buf, npi->data.size, bpi->destinationPort, npi->id); fwd = false; if (facilities.coordination.active) { vcm_check(its_msg); @@ -834,7 +853,7 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) // Forward to [applications] if (fwd) { - fwd_to_apps(bpi->data.buf, bpi->data.size, bpi->destinationPort, bpi->id); + fwd_to_apps(npi->data.buf, npi->data.size, bpi->destinationPort, npi->id); } // Logging @@ -842,20 +861,20 @@ int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket) pthread_mutex_lock(&facilities.id.lock); uint32_t station_id = facilities.id.station_id; pthread_mutex_unlock(&facilities.id.lock); - itss_db_add(facilities.logging.dbms, station_id, bpi->id, false, its_msg_type, NULL, bpi->data.buf, bpi->data.size); + itss_db_add(facilities.logging.dbms, station_id, npi->id, false, its_msg_type, NULL, npi->data.buf, npi->data.size); } if (facilities.logging.recorder) { int e = itss_management_record_packet_sdu( buf, buf_len, - bpi->data.buf, - bpi->data.size, - bpi->id, + npi->data.buf, + npi->data.size, + npi->id, itss_time_get(), ITSS_FACILITIES, false); if (e != -1) { - itss_queue_send(facilities.tx_queue, buf, e, ITSS_MANAGEMENT, bpi->id, "MReq.packet.set"); + itss_queue_send(facilities.tx_queue, buf, e, ITSS_MANAGEMENT, npi->id, "MReq.packet.set"); } } @@ -869,26 +888,28 @@ cleanup: } -int transport_indication_tcp(TCPPacketIndication_t* tpi, void** security_socket) { +static int networking_packet_indication_tcp(NetworkingPacketIndication_t* npi, void** security_socket) { int rv = 0; + TCPPacketIndication_t* tpi = &npi->transport.choice.tcp; + uint16_t buf_len = 2048; uint8_t buf[buf_len]; SecurityRequest_t* sreq = NULL; SecurityReply_t* srep = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* nr = NULL; FacilitiesIndication_t* fi = NULL; - void* its_msg; + void* its_msg = NULL; sreq = calloc(1, sizeof(SecurityRequest_t)); sreq->present = SecurityRequest_PR_tlsRecv; - sreq->choice.tlsRecv.data.size = tpi->data.size; - sreq->choice.tlsRecv.data.buf = malloc(tpi->data.size); - memcpy(sreq->choice.tlsRecv.data.buf, tpi->data.buf, tpi->data.size); + sreq->choice.tlsRecv.data.size = npi->data.size; + sreq->choice.tlsRecv.data.buf = malloc(npi->data.size); + memcpy(sreq->choice.tlsRecv.data.buf, npi->data.buf, npi->data.size); - uint64_t id = tpi->id; + uint64_t id = npi->id; pthread_mutex_lock(&facilities.tolling.lock); tlsc_t *tlsc = tolling_tlsc_get(tpi->sourceAddress->buf, 7011); if (tlsc) { @@ -935,45 +956,59 @@ int transport_indication_tcp(TCPPacketIndication_t* tpi, void** security_socket) log_debug("[tolling] tls n-msg:%d state:%d", tlsc->nmsg, tlsc->state); - // Forward to [transport] + // Forward to [networking] if (srep->data->choice.tlsRecv.state != 1) { - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - tr->choice.packet.present = TransportPacketRequest_PR_tcp; - TCPPacketRequest_t *tpr = &tr->choice.packet.choice.tcp; - tpr->data.size = srep->data->choice.tlsRecv.data.size; - tpr->data.buf = malloc(srep->data->choice.tlsRecv.data.size); - memcpy(tpr->data.buf, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size); + nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; + npr->transport.present = NetworkingPacketRequestTP_PR_tcp; + npr->network.present = NetworkingPacketRequestNW_PR_gn; - tpr->sourcePort = tpi->destinationPort; - tpr->destinationPort = tpi->sourcePort; + npr->data.size = srep->data->choice.tlsRecv.data.size; + npr->data.buf = malloc(srep->data->choice.tlsRecv.data.size); + memcpy(npr->data.buf, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size); - tpr->destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); - tpr->destinationAddress->buf = malloc(16); - tpr->destinationAddress->size = 16; - memcpy(tpr->destinationAddress->buf, tpi->sourceAddress->buf, 16); - tpr->destinationPort = 7011; - tpr->sourcePort = 7011; + npr->transport.present = NetworkingPacketRequestTP_PR_tcp; + npr->transport.choice.tcp.sourcePort = tpi->destinationPort; + npr->transport.choice.tcp.destinationPort = tpi->sourcePort; + + npr->transport.choice.tcp.destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); + npr->transport.choice.tcp.destinationAddress->buf = malloc(16); + npr->transport.choice.tcp.destinationAddress->size = 16; + memcpy(npr->transport.choice.tcp.destinationAddress->buf, tpi->sourceAddress->buf, 16); + npr->transport.choice.tcp.destinationPort = 7011; + npr->transport.choice.tcp.sourcePort = 7011; if (facilities.tolling.protocol.p == TOLLING_PROTOCOL_TLS_GN || - (facilities.tolling.protocol.p == TOLLING_PROTOCOL_TLS_SHS && tlsc->nmsg < 2)) { - tpr->gn = calloc(1, sizeof(GeonetworkingOutboundOptions_t)); - tpr->gn->packetTransportType = PacketTransportType_shb; - tpr->gn->destinationAddress.buf = calloc(1, 6); - tpr->gn->destinationAddress.size = 6; + (facilities.tolling.protocol.p == TOLLING_PROTOCOL_TLS_SHS && tlsc->nmsg < 2)) { + npr->network.present = NetworkingPacketRequestNW_PR_gn6a; + npr->network.choice.gn6a.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn6a.gn.destinationAddress.buf = calloc(1, 6); + npr->network.choice.gn6a.gn.destinationAddress.size = 6; + npr->network.choice.gn6a.gn.securityProfile.encrypt = false; + npr->network.choice.gn6a.gn.securityProfile.sign = true; + + npr->network.choice.gn6a.ip.destinationAddress.buf = malloc(16); + npr->network.choice.gn6a.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.gn6a.ip.destinationAddress.buf, tpi->sourceAddress->buf, 16); + } else { + npr->network.present = NetworkingPacketRequestNW_PR_ip; + npr->network.choice.ip.destinationAddress.buf = malloc(16); + npr->network.choice.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.ip.destinationAddress.buf, tpi->sourceAddress->buf, 16); } - tpr->id = itss_id(tpr->data.buf, tpr->data.size); + npr->id = itss_id(npr->data.buf, npr->data.size); buf[0] = 4; - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf + 1, buf_len - 1); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, buf + 1, buf_len - 1); if (enc.encoded == -1) { - log_error("TransportRequest encoding fail"); + log_error("NR encoding error (%s)", enc.failed_type->name); rv = 1; goto cleanup; } - itss_queue_send(facilities.tx_queue, buf, enc.encoded + 1, ITSS_TRANSPORT, tpr->id, "TR.packet.tcp"); + itss_queue_send(facilities.tx_queue, buf, enc.encoded + 1, ITSS_NETWORKING, npr->id, "NR.packet.tcp"); } else { if (facilities.tolling.enabled && srep->data->choice.tlsRecv.data.size) { asn_dec_rval_t dec = uper_decode_complete(NULL, &asn_DEF_TPM, (void **)&its_msg, srep->data->choice.tlsRecv.data.buf, srep->data->choice.tlsRecv.data.size); @@ -1033,9 +1068,29 @@ cleanup: ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq); ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep); - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); return rv; } -int transport_indication_udp() {} +int networking_packet_indication(NetworkingPacketIndication_t* npi, void** security_socket) { + + int rv = 0; + + switch (npi->transport.present) { + case NetworkingPacketIndicationTP_PR_btp: + networking_packet_indication_btp(npi, security_socket); + break; + case NetworkingPacketIndicationTP_PR_tcp: + networking_packet_indication_tcp(npi, security_socket); + break; + default: + log_error("unhandled transport protocol"); + rv = 1; + goto cleanup; + + } + +cleanup: + return rv; +} diff --git a/src/requests.h b/src/requests.h index 7fad584..c257d57 100644 --- a/src/requests.h +++ b/src/requests.h @@ -3,7 +3,7 @@ #include "facilities.h" #include -#include +#include int facilities_request_result_accepted(void* responder); int facilities_request_result_rejected(void* responder); @@ -14,8 +14,6 @@ int facilities_request_attribute_types(void* responder, FacilitiesRequest_t* fr) int facilities_request_loaded_protected_zones(void* responder, FacilitiesRequest_t* fr); int facilities_request_chaininfo_set(void* responder, ChainInformationSet_t* cis); -int transport_indication_data(TransportDataIndication_t* tdi); -int transport_indication_btp(BTPPacketIndication_t* bpi, void** security_socket); -int transport_indication_tcp(TCPPacketIndication_t* tpi, void** security_socket); +int networking_packet_indication(NetworkingPacketIndication_t* npi, void** security_socket); #endif diff --git a/src/saem.c b/src/saem.c index fd9c18e..023a943 100644 --- a/src/saem.c +++ b/src/saem.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -301,33 +301,28 @@ void *sa_service() { pthread_mutex_init(&facilities.bulletin.lock, NULL); - TransportRequest_t *tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - TransportPacketRequest_t* tpr = &tr->choice.packet; + NetworkingRequest_t* nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; - tpr->present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t *bpr = &tpr->choice.btp; + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); + for (int i = 0; i < 6; ++i) { + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; + } + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; + + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = Port_saem; bulletin_t* bulletin = &facilities.bulletin; - bpr->btpType = BTPType_btpB; - - bpr->gn.destinationAddress.buf = malloc(6); - for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; - } - bpr->gn.destinationAddress.size = 6; - - bpr->gn.packetTransportType = PacketTransportType_shb; - - bpr->destinationPort = Port_saem; - - bpr->gn.trafficClass = 2; - - bpr->data.buf = malloc(512); - bpr->data.size = 512; - - bpr->gn.securityProfile.sign = true; + npr->data.buf = malloc(512); + npr->data.size = 512; int linger_ms = 0; void* security_socket = itss_0connect(facilities.zmq.security_address, ZMQ_REQ); @@ -343,23 +338,23 @@ void *sa_service() { while (!facilities.exit) { if (bulletin->to_provide_len && sleep_ms*mk_saem_n_sleep >= 1000) { - rv = mk_saem(bpr->data.buf, (uint32_t *) &bpr->data.size); + rv = mk_saem(npr->data.buf, (uint32_t *) &npr->data.size); if (!rv) { - bpr->id = itss_id(bpr->data.buf, bpr->data.size); + npr->id = itss_id(npr->data.buf, npr->data.size); - asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, tr_oer+1, 1023); + asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, tr_oer+1, 1023); if (enc.encoded == -1) { log_error("encoding TR for SAEM failed"); continue; } else { - itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded+1, ITSS_TRANSPORT, bpr->id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, tr_oer, enc.encoded+1, ITSS_NETWORKING, npr->id, "NR.packet.btp"); // Logging if (facilities.logging.dbms) { pthread_mutex_lock(&facilities.id.lock); uint32_t station_id = facilities.id.station_id; pthread_mutex_unlock(&facilities.id.lock); - itss_db_add(facilities.logging.dbms, station_id, bpr->id, true, messageID_saem, NULL, bpr->data.buf, bpr->data.size); + itss_db_add(facilities.logging.dbms, station_id, npr->id, true, messageID_saem, NULL, npr->data.buf, npr->data.size); } if (facilities.logging.recorder) { uint16_t buffer_len = 2048; @@ -367,14 +362,14 @@ void *sa_service() { int e = itss_management_record_packet_sdu( buffer, buffer_len, - bpr->data.buf, - bpr->data.size, - bpr->id, + npr->data.buf, + npr->data.size, + npr->id, itss_time_get(), ITSS_FACILITIES, true); if (e != -1) { - itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, bpr->id, "MReq.packet.set"); + itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, npr->id, "MReq.packet.set"); } } } @@ -443,7 +438,7 @@ void *sa_service() { usleep(sleep_ms*1000); } - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); itss_0close(security_socket); diff --git a/src/tpm.c b/src/tpm.c index 8ddca5d..0ff7c49 100644 --- a/src/tpm.c +++ b/src/tpm.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -78,7 +78,7 @@ int tpm_pay(tolling_info_t* info, void** security_socket, uint8_t* neighbour, ui } TPM_t* tpm = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* nr = NULL; SecurityRequest_t* sreq = NULL; SecurityReply_t* srep = NULL; FacilitiesIndication_t* fi = NULL; @@ -257,42 +257,42 @@ int tpm_pay(tolling_info_t* info, void** security_socket, uint8_t* neighbour, ui } // [transport] request (TR) - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; + nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; uint64_t id = 0; switch (facilities.tolling.protocol.p) { case TOLLING_PROTOCOL_GN_SPKI: case TOLLING_PROTOCOL_GN_DPKI: - tr->choice.packet.present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t* bpr = &tr->choice.packet.choice.btp; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.securityProfile.encrypt = true; + npr->network.choice.gn.securityProfile.sign = true; - bpr->gn.securityProfile.encrypt = true; - bpr->gn.securityProfile.sign = true; + npr->network.choice.gn.securityNeighbour = calloc(1, sizeof(OCTET_STRING_t)); + npr->network.choice.gn.securityNeighbour->size = 8; + npr->network.choice.gn.securityNeighbour->buf = malloc(8); + memcpy(npr->network.choice.gn.securityNeighbour->buf, neighbour, 8); - bpr->gn.securityNeighbour = calloc(1, sizeof(OCTET_STRING_t)); - bpr->gn.securityNeighbour->size = 8; - bpr->gn.securityNeighbour->buf = malloc(8); - memcpy(bpr->gn.securityNeighbour->buf, neighbour, 8); + npr->data.size = tpm_uper_len; + npr->data.buf = malloc(tpm_uper_len); + memcpy(npr->data.buf, tpm_uper, tpm_uper_len); - bpr->data.size = tpm_uper_len; - bpr->data.buf = malloc(tpm_uper_len); - memcpy(bpr->data.buf, tpm_uper, tpm_uper_len); + npr->id = itss_id(npr->data.buf, npr->data.size); + id = npr->id; - bpr->id = itss_id(bpr->data.buf, bpr->data.size); - id = bpr->id; - - bpr->destinationPort = 7011; - bpr->btpType = BTPType_btpB; - bpr->gn.destinationAddress.buf = malloc(6); + npr->transport.choice.btp.destinationPort = 7011; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; } - bpr->gn.destinationAddress.size = 6; - bpr->gn.trafficClass = 2; - bpr->gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; break; case TOLLING_PROTOCOL_TLS: @@ -340,46 +340,54 @@ int tpm_pay(tolling_info_t* info, void** security_socket, uint8_t* neighbour, ui } // TR TCP - tr->choice.packet.present = TransportPacketRequest_PR_tcp; - TCPPacketRequest_t* tcp = &tr->choice.packet.choice.tcp; + npr->transport.present = NetworkingPacketRequestTP_PR_tcp; - tcp->id = id; + npr->id = id; - tcp->destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); - tcp->destinationAddress->buf = malloc(16); - tcp->destinationAddress->size = 16; - memcpy(tcp->destinationAddress->buf, dst_addr, 16); - tcp->destinationPort = 7011; - tcp->sourcePort = 7011; + npr->transport.choice.tcp.destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); + npr->transport.choice.tcp.destinationAddress->buf = malloc(16); + npr->transport.choice.tcp.destinationAddress->size = 16; + memcpy(npr->transport.choice.tcp.destinationAddress->buf, dst_addr, 16); + npr->transport.choice.tcp.destinationPort = 7011; + npr->transport.choice.tcp.sourcePort = 7011; - tcp->data.buf = malloc(srep->data->choice.tlsSend.data.size); - tcp->data.size = srep->data->choice.tlsSend.data.size; - memcpy(tcp->data.buf, srep->data->choice.tlsSend.data.buf, srep->data->choice.tlsSend.data.size); + npr->data.buf = malloc(srep->data->choice.tlsSend.data.size); + npr->data.size = srep->data->choice.tlsSend.data.size; + memcpy(npr->data.buf, srep->data->choice.tlsSend.data.buf, srep->data->choice.tlsSend.data.size); if (tolling->protocol.p == TOLLING_PROTOCOL_TLS_GN || - tolling->protocol.p == TOLLING_PROTOCOL_TLS_SHS) { - tcp->gn = calloc(1, sizeof(GeonetworkingOutboundOptions_t)); - tcp->gn->packetTransportType = PacketTransportType_shb; - tcp->gn->destinationAddress.buf = calloc(1, 6); - tcp->gn->destinationAddress.size = 6; - tcp->gn->securityProfile.encrypt = false; - tcp->gn->securityProfile.sign = true; + tolling->protocol.p == TOLLING_PROTOCOL_TLS_SHS) { + npr->network.present = NetworkingPacketRequestNW_PR_gn6a; + npr->network.choice.gn6a.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn6a.gn.destinationAddress.buf = calloc(1, 6); + npr->network.choice.gn6a.gn.destinationAddress.size = 6; + npr->network.choice.gn6a.gn.securityProfile.encrypt = false; + npr->network.choice.gn6a.gn.securityProfile.sign = true; + + npr->network.choice.gn6a.ip.destinationAddress.buf = malloc(16); + npr->network.choice.gn6a.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.gn6a.ip.destinationAddress.buf, dst_addr, 16); + } else { + npr->network.present = NetworkingPacketRequestNW_PR_ip; + npr->network.choice.ip.destinationAddress.buf = malloc(16); + npr->network.choice.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.ip.destinationAddress.buf, dst_addr, 16); } break; } // encode TR buf1[0] = 4; - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf1+1, buf_len-1); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, buf1+1, buf_len-1); if (enc.encoded == -1) { log_error("[tolling] error encoding TR TPM.request (%s)", enc.failed_type->name); rv = 1; goto cleanup; } - itss_queue_send(facilities.tx_queue, buf1, enc.encoded+1, ITSS_TRANSPORT, id, + itss_queue_send(facilities.tx_queue, buf1, enc.encoded+1, ITSS_NETWORKING, id, (tolling->protocol.p == TOLLING_PROTOCOL_GN_SPKI || tolling->protocol.p == TOLLING_PROTOCOL_GN_DPKI) - ? "TR.packet.btp" : "TR.packet.tcp"); + ? "NR.packet.btp" : "NR.packet.tcp"); // Retransmission uint64_t now = itss_ts_get(TIME_MICROSECONDS); @@ -433,7 +441,7 @@ int tpm_pay(tolling_info_t* info, void** security_socket, uint8_t* neighbour, ui cleanup: pthread_mutex_unlock(&tolling->lock); ASN_STRUCT_FREE(asn_DEF_TPM, tpm); - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq); ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep); ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); @@ -605,7 +613,7 @@ static int rsu_handle_recv(TPM_t* tpm_rx, void** security_socket, uint8_t* neigh asn_enc_rval_t enc; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* nr = NULL; FacilitiesIndication_t* fi = NULL; TPM_t* tpm = NULL; @@ -868,42 +876,48 @@ static int rsu_handle_recv(TPM_t* tpm_rx, void** security_socket, uint8_t* neigh uint64_t id = 0; - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; + nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; // [transport] request (TR) switch (tolling->protocol.p) { case TOLLING_PROTOCOL_GN_SPKI: case TOLLING_PROTOCOL_GN_DPKI: - tr->choice.packet.present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t* bpr = &tr->choice.packet.choice.btp; + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.securityProfile.encrypt = true; + npr->network.choice.gn.securityProfile.sign = true; - bpr->gn.securityProfile.encrypt = true; - bpr->gn.securityProfile.sign = true; + npr->network.choice.gn.securityNeighbour = calloc(1, sizeof(OCTET_STRING_t)); + npr->network.choice.gn.securityNeighbour->size = 8; + npr->network.choice.gn.securityNeighbour->buf = malloc(8); + memcpy(npr->network.choice.gn.securityNeighbour->buf, neighbour, 8); - bpr->data.size = tpm_uper_len; - bpr->data.buf = malloc(tpm_uper_len); - memcpy(bpr->data.buf, tpm_uper, tpm_uper_len); + npr->data.size = tpm_uper_len; + npr->data.buf = malloc(tpm_uper_len); + memcpy(npr->data.buf, tpm_uper, tpm_uper_len); - bpr->id = itss_id(bpr->data.buf, bpr->data.size); - id = bpr->id; + npr->id = itss_id(npr->data.buf, npr->data.size); + id = npr->id; - bpr->destinationPort = 7011; - bpr->btpType = BTPType_btpB; - bpr->gn.destinationAddress.buf = malloc(6); + npr->transport.choice.btp.destinationPort = 7011; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->network.choice.gn.destinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; } - bpr->gn.destinationAddress.size = 6; - bpr->gn.trafficClass = 2; - bpr->gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + if (neighbour) { /* neighbour ID for [security] encryption */ - bpr->gn.securityNeighbour = calloc(1, sizeof(OCTET_STRING_t)); - bpr->gn.securityNeighbour->size = 8; - bpr->gn.securityNeighbour->buf = malloc(8); - memcpy(bpr->gn.securityNeighbour->buf, neighbour, 8); + npr->network.choice.gn.securityNeighbour = calloc(1, sizeof(OCTET_STRING_t)); + npr->network.choice.gn.securityNeighbour->size = 8; + npr->network.choice.gn.securityNeighbour->buf = malloc(8); + memcpy(npr->network.choice.gn.securityNeighbour->buf, neighbour, 8); } break; @@ -951,44 +965,53 @@ static int rsu_handle_recv(TPM_t* tpm_rx, void** security_socket, uint8_t* neigh } // TR TCP - tr->choice.packet.present = TransportPacketRequest_PR_tcp; - TCPPacketRequest_t* tcp = &tr->choice.packet.choice.tcp; + npr->transport.present = NetworkingPacketRequestTP_PR_tcp; - tcp->id = id; + npr->id = id; - tcp->destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); - tcp->destinationAddress->buf = malloc(16); - tcp->destinationAddress->size = 16; - memcpy(tcp->destinationAddress->buf, src_addr, 16); - tcp->destinationPort = 7011; - tcp->sourcePort = 7011; + npr->transport.choice.tcp.destinationAddress = calloc(1, sizeof(OCTET_STRING_t)); + npr->transport.choice.tcp.destinationAddress->buf = malloc(16); + npr->transport.choice.tcp.destinationAddress->size = 16; + memcpy(npr->transport.choice.tcp.destinationAddress->buf,src_addr, 16); + npr->transport.choice.tcp.destinationPort = 7011; + npr->transport.choice.tcp.sourcePort = 7011; - tcp->data.buf = malloc(srep->data->choice.tlsSend.data.size); - tcp->data.size = srep->data->choice.tlsSend.data.size; - memcpy(tcp->data.buf, srep->data->choice.tlsSend.data.buf, srep->data->choice.tlsSend.data.size); + npr->data.buf = malloc(srep->data->choice.tlsSend.data.size); + npr->data.size = srep->data->choice.tlsSend.data.size; + memcpy(npr->data.buf, srep->data->choice.tlsSend.data.buf, srep->data->choice.tlsSend.data.size); if (tolling->protocol.p == TOLLING_PROTOCOL_TLS_GN) { - tcp->gn = calloc(1, sizeof(GeonetworkingOutboundOptions_t)); - tcp->gn->packetTransportType = PacketTransportType_shb; - tcp->gn->destinationAddress.buf = calloc(1, 6); - tcp->gn->destinationAddress.size = 6; - tcp->gn->securityProfile.encrypt = false; - tcp->gn->securityProfile.sign = true; + npr->network.present = NetworkingPacketRequestNW_PR_gn6a; + npr->network.choice.gn6a.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn6a.gn.destinationAddress.buf = calloc(1, 6); + npr->network.choice.gn6a.gn.destinationAddress.size = 6; + npr->network.choice.gn6a.gn.securityProfile.encrypt = false; + npr->network.choice.gn6a.gn.securityProfile.sign = true; + + npr->network.choice.gn6a.ip.destinationAddress.buf = malloc(16); + npr->network.choice.gn6a.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.gn6a.ip.destinationAddress.buf, src_addr, 16); + } else { + npr->network.present = NetworkingPacketRequestNW_PR_ip; + npr->network.choice.ip.destinationAddress.buf = malloc(16); + npr->network.choice.ip.destinationAddress.size = 16; + memcpy(npr->network.choice.ip.destinationAddress.buf, src_addr, 16); } + break; } // encode TR buf1[0] = 4; - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf1+1, buf_len-1); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, nr, buf1+1, buf_len-1); if (enc.encoded == -1) { log_error("[tolling] error encoding TR TPM.reply (%s)", enc.failed_type->name); rv = 1; goto cleanup; } - itss_queue_send(facilities.tx_queue, buf1, enc.encoded+1, ITSS_TRANSPORT, id, - (tolling->protocol.p == TOLLING_PROTOCOL_GN_SPKI || tolling->protocol.p == TOLLING_PROTOCOL_GN_DPKI) ? "TR.packet.btp" : "TR.packet.tcp"); + itss_queue_send(facilities.tx_queue, buf1, enc.encoded+1, ITSS_NETWORKING, id, + (tolling->protocol.p == TOLLING_PROTOCOL_GN_SPKI || tolling->protocol.p == TOLLING_PROTOCOL_GN_DPKI) ? "NR.packet.btp" : "NR.packet.tcp"); // Logging if (facilities.logging.dbms) { @@ -1033,7 +1056,7 @@ static int rsu_handle_recv(TPM_t* tpm_rx, void** security_socket, uint8_t* neigh cleanup: ASN_STRUCT_FREE(asn_DEF_TPM, tpm); - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); ASN_STRUCT_FREE(asn_DEF_SecurityRequest, sreq); ASN_STRUCT_FREE(asn_DEF_SecurityReply, srep); ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); @@ -1056,7 +1079,7 @@ static int veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void** security_so SecurityRequest_t* sreq = NULL; SecurityReply_t* srep = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* tr = NULL; const uint16_t buf_len = 2048; uint8_t buf1[buf_len], buf2[buf_len]; @@ -1235,9 +1258,9 @@ static int veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void** security_so log_debug("<-[security] SecurityReply.tlsClose (%dB)", rl); uint32_t id = rand(); - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_data; - tr->choice.data.present = TransportDataRequest_PR_tcp; + tr = calloc(1, sizeof(NetworkingRequest_t)); + tr->present = NetworkingRequest_PR_data; + tr->choice.data.present = NetworkingDataRequest_PR_tcp; tr->choice.data.choice.tcp.present = TCPDataRequest_PR_connCloseReq; tr->choice.data.choice.tcp.choice.connCloseReq.closeByPeer = false; tr->choice.data.choice.tcp.choice.connCloseReq.destinationPort = 7011; @@ -1247,8 +1270,8 @@ static int veh_handle_recv(tolling_t* tolling, TPM_t* tpm_rx, void** security_so tr->choice.data.choice.tcp.choice.connCloseReq.id = id; buf1[0] = 4; - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf1+1, buf_len-1); - itss_queue_send(tx_queue, buf1, enc.encoded+1, ITSS_TRANSPORT, id, "TR.data.tcp.connClose"); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, tr, buf1+1, buf_len-1); + itss_queue_send(tx_queue, buf1, enc.encoded+1, ITSS_NETWORKING, id, "NR.data.tcp.connClose"); free(tlsc); for (int i = 0; i < tolling->protocol.c.tls.n_tlsc; ++i) { @@ -1471,7 +1494,7 @@ void tolling_tlsc_mgmt(itss_queue_t* tx_queue, void** security_socket) { tlsc_t* tlsc = tolling->protocol.c.tls.tls_conns[i]; if (tlsc->ts + TOLLING_CONN_TIMEOUT_MS < now) { SecurityRequest_t* sreq = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* tr = NULL; const uint32_t buf_len = 1024; uint8_t buf1[buf_len], buf2[buf_len]; log_debug("[tolling] closing TCP/TLS connection"); @@ -1488,9 +1511,9 @@ void tolling_tlsc_mgmt(itss_queue_t* tx_queue, void** security_socket) { log_debug("<-[security] SecurityReply.tlsClose (%dB)", rl); uint64_t id = rand() + 1; - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_data; - tr->choice.data.present = TransportDataRequest_PR_tcp; + tr = calloc(1, sizeof(NetworkingRequest_t)); + tr->present = NetworkingRequest_PR_data; + tr->choice.data.present = NetworkingDataRequest_PR_tcp; tr->choice.data.choice.tcp.present = TCPDataRequest_PR_connCloseReq; tr->choice.data.choice.tcp.choice.connCloseReq.closeByPeer = false; tr->choice.data.choice.tcp.choice.connCloseReq.destinationPort = 7011; @@ -1500,8 +1523,8 @@ void tolling_tlsc_mgmt(itss_queue_t* tx_queue, void** security_socket) { tr->choice.data.choice.tcp.choice.connCloseReq.id = id; buf1[0] = 4; - enc = oer_encode_to_buffer(&asn_DEF_TransportRequest, NULL, tr, buf1+1, buf_len-1); - itss_queue_send(tx_queue, buf1, enc.encoded+1, ITSS_TRANSPORT, id, "TR.data.tcp.connClose"); + enc = oer_encode_to_buffer(&asn_DEF_NetworkingRequest, NULL, tr, buf1+1, buf_len-1); + itss_queue_send(tx_queue, buf1, enc.encoded+1, ITSS_NETWORKING, id, "NR.data.tcp.connClose"); free(tlsc); for (int j = i; j < tolling->protocol.c.tls.n_tlsc-1; ++j) { diff --git a/src/vcm.c b/src/vcm.c index c357f18..35f7678 100644 --- a/src/vcm.c +++ b/src/vcm.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include #include @@ -76,7 +76,7 @@ static mc_neighbour_s* get_neighbour(uint32_t station_id) { static void tx_vcm(VCM_t* vcm) { const uint16_t buf_len = 2048; uint8_t buf[buf_len]; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* nr = NULL; FacilitiesIndication_t* fi = NULL; asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_VCM, NULL, vcm, buf, buf_len); if (enc.encoded == -1) { @@ -85,51 +85,55 @@ static void tx_vcm(VCM_t* vcm) { } uint16_t vcm_len = (enc.encoded + 7) / 8; - tr = calloc(1, sizeof(TransportRequest_t)); - tr->present = TransportRequest_PR_packet; - tr->choice.packet.present = TransportPacketRequest_PR_btp; - BTPPacketRequest_t* bpr = &tr->choice.packet.choice.btp; - bpr->btpType = BTPType_btpB; + nr = calloc(1, sizeof(NetworkingRequest_t)); + nr->present = NetworkingRequest_PR_packet; + NetworkingPacketRequest_t* npr = &nr->choice.packet; + + npr->network.present = NetworkingPacketRequestNW_PR_gn; + npr->network.choice.gn.trafficClass = 2; + npr->network.choice.gn.destinationAddress.buf = malloc(6); + for (int i = 0; i < 6; ++i) { + npr->network.choice.gn.destinationAddress.buf[i] = 0xff; + } + npr->network.choice.gn.destinationAddress.size = 6; + npr->network.choice.gn.packetTransportType = PacketTransportType_shb; + npr->network.choice.gn.securityProfile.sign = true; + + npr->transport.present = NetworkingPacketRequestTP_PR_btp; + npr->transport.choice.btp.btpType = BTPType_btpB; + npr->transport.choice.btp.destinationPort = 2043; if (facilities.edm.enabled && vcm->vcm.maneuverContainer.present == ManeuverContainer_PR_vehicle && vcm->vcm.maneuverContainer.choice.vehicle.negotiation ) { edm_encap(buf, &vcm_len, buf_len, 2043); - bpr->destinationPortInfo = calloc(1, sizeof(OCTET_STRING_t)); - bpr->destinationPortInfo->size = 2; - bpr->destinationPortInfo->buf = malloc(2); - *(uint16_t*)bpr->destinationPortInfo->buf = 0xed; + npr->transport.choice.btp.destinationPortInfo = calloc(1, sizeof(OCTET_STRING_t)); + npr->transport.choice.btp.destinationPortInfo->size = 2; + npr->transport.choice.btp.destinationPortInfo->buf = malloc(2); + *(uint16_t*)npr->transport.choice.btp.destinationPortInfo->buf = 0xed; } - bpr->id = itss_id(buf, vcm_len); - bpr->gn.destinationAddress.buf = malloc(6); - for (int i = 0; i < 6; ++i) { - bpr->gn.destinationAddress.buf[i] = 0xff; - } - bpr->gn.destinationAddress.size = 6; - bpr->gn.packetTransportType = PacketTransportType_shb; - bpr->destinationPort = 2043; - bpr->gn.trafficClass = 2; - bpr->data.buf = malloc(vcm_len); - memcpy(bpr->data.buf, buf, vcm_len); - bpr->data.size = vcm_len; + npr->id = itss_id(buf, vcm_len); + npr->data.buf = malloc(vcm_len); + memcpy(npr->data.buf, buf, vcm_len); + npr->data.size = vcm_len; buf[0] = 4; - enc = asn_encode_to_buffer(NULL, ATS_CANONICAL_OER, &asn_DEF_TransportRequest, tr, buf+1, buf_len-1); + enc = asn_encode_to_buffer(NULL, ATS_CANONICAL_OER, &asn_DEF_NetworkingRequest, nr, buf+1, buf_len-1); if (enc.encoded == -1) { - log_error("[vc] TR VCM.reply encode failure (%s)", enc.failed_type->name); + log_error("[vc] NR VCM.reply encode failure (%s)", enc.failed_type->name); goto cleanup; } - itss_queue_send(facilities.tx_queue, buf, enc.encoded+1, ITSS_TRANSPORT, bpr->id, "TR.packet.btp"); + itss_queue_send(facilities.tx_queue, buf, enc.encoded+1, ITSS_NETWORKING, npr->id, "NR.packet.btp"); fi = calloc(1, sizeof(FacilitiesIndication_t)); fi->present = FacilitiesIndication_PR_message; - fi->choice.message.id = bpr->id; + fi->choice.message.id = npr->id; fi->choice.message.itsMessageType = 2043; - fi->choice.message.data.size = bpr->data.size; - fi->choice.message.data.buf = malloc(bpr->data.size); - memcpy(fi->choice.message.data.buf, bpr->data.buf, bpr->data.size); + fi->choice.message.data.size = npr->data.size; + fi->choice.message.data.buf = malloc(npr->data.size); + memcpy(fi->choice.message.data.buf, npr->data.buf, npr->data.size); buf[0] = 4; enc = asn_encode_to_buffer(NULL, ATS_CANONICAL_OER, &asn_DEF_FacilitiesIndication, fi, buf+1, buf_len-1); if (enc.encoded == -1) { @@ -137,7 +141,7 @@ static void tx_vcm(VCM_t* vcm) { goto cleanup; } - itss_queue_send(facilities.tx_queue, buf, enc.encoded+1, ITSS_APPLICATIONS, bpr->id, "FI.message"); + itss_queue_send(facilities.tx_queue, buf, enc.encoded+1, ITSS_APPLICATIONS, npr->id, "FI.message"); facilities.coordination.t_last_send_vcm = itss_time_get(); @@ -147,19 +151,19 @@ static void tx_vcm(VCM_t* vcm) { int e = itss_management_record_packet_sdu( buffer, buffer_len, - bpr->data.buf, - bpr->data.size, - bpr->id, + npr->data.buf, + npr->data.size, + npr->id, itss_time_get(), ITSS_FACILITIES, true); if (e != -1) { - itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, bpr->id, "MReq.packet.set"); + itss_queue_send(facilities.tx_queue, buffer, e, ITSS_MANAGEMENT, npr->id, "MReq.packet.set"); } } cleanup: - ASN_STRUCT_FREE(asn_DEF_TransportRequest, tr); + ASN_STRUCT_FREE(asn_DEF_NetworkingRequest, nr); ASN_STRUCT_FREE(asn_DEF_FacilitiesIndication, fi); } @@ -187,7 +191,7 @@ static void vcm_reject(VCM_t* vcm, mc_neighbour_s* neighbour) { itss_st_t trajectoryA[TRAJECTORY_MAX_LEN+1]; /* ego trajectory */ ssize_t trajectoryA_len = 0; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* tr = NULL; FacilitiesIndication_t* fi = NULL; VCM_t* vcm_rep = calloc(1, sizeof(VCM_t)); @@ -506,7 +510,7 @@ static int vcm_check_handle_request(VCM_t* vcm, mc_neighbour_s* neighbour) { // Respond VCM_t* vcm_rep = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* tr = NULL; FacilitiesIndication_t* fi = NULL; @@ -741,7 +745,7 @@ static int intersection_detected(VCM_t* vcm, mc_neighbour_s* neighbour) { coordination_t* coordination = &facilities.coordination; VCM_t* vcm_req = NULL; - TransportRequest_t* tr = NULL; + NetworkingRequest_t* tr = NULL; FacilitiesIndication_t* fi = NULL; const ssize_t buf_len = 1024;