#include "facilities.h" #include "cam.h" #include "config.h" #include "denm.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define syslog_info(msg, ...) syslog(LOG_INFO, msg, ##__VA_ARGS__) #define syslog_emerg(msg, ...) syslog(LOG_EMERG, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #define syslog_err(msg, ...) syslog(LOG_ERR, "%s:%d [" msg "]", __func__, __LINE__, ##__VA_ARGS__) #ifndef NDEBUG #define syslog_debug(msg, ...) syslog(LOG_DEBUG, "%s:%d " msg "", __func__, __LINE__, ##__VA_ARGS__) #else #define syslog_debug(msg, ...) #endif static int transport_indication(facilities_t *facilities, void* responder, uint8_t *msg, uint32_t msg_len) { int rv = 0, code = 0; FacilitiesDataIndication_t *fdi = NULL; BTPDataIndication_t *bdi = calloc(1, sizeof(BTPDataIndication_t)); asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_BTPDataIndication, (void**) &bdi, msg, msg_len); if (dec.code) { syslog_err("[facilities]<- invalid bdi received"); rv = 1; code = 1; zmq_send(responder, &code, 1, 0); goto cleanup; } zmq_send(responder, &code, 1, 0); syslog_debug("[facilities]<- BDI (%ldB)", bdi->data.size); // Parse message asn_TYPE_descriptor_t *its_msg_descriptor; void *its_msg; switch (bdi->destinationPort) { case Port_cam: its_msg_descriptor = &asn_DEF_CAM; its_msg = calloc(1, sizeof(CAM_t)); break; case Port_denm: its_msg_descriptor = &asn_DEF_DENM; its_msg = calloc(1, sizeof(DENM_t)); break; case Port_ivim: its_msg_descriptor = &asn_DEF_IVIM; its_msg = calloc(1, sizeof(IVIM_t)); break; default: syslog_debug("[facilities] messsage with unhandled BTP port received, ignoring"); goto cleanup; } dec = uper_decode_complete(NULL, its_msg_descriptor, (void**) &its_msg, bdi->data.buf, bdi->data.size); if (dec.code) { syslog_debug("[facilities]<- invalid %s received", its_msg_descriptor->name); rv = 1; goto cleanup; } // Manage message switch (bdi->destinationPort) { case Port_cam: if (facilities->station_type == 15) { // Currently only RSUs need to check CAMs check_cam(facilities, bdi, its_msg); } break; case Port_denm: ; #ifdef DEBUG uint8_t* xml_denm = malloc(32768); asn_enc_rval_t rve = xer_encode_to_buffer(xml_denm, 32768, 0x02, &asn_DEF_DENM, its_msg); syslog_debug("DENM XER %d: %.*s", (int)rve.encoded, (int)rve.encoded , xml_denm); free(xml_denm); #endif int64_t id = -1; event_manage(facilities->den, its_msg, &id); break; default: break; } // Forward to application fdi = calloc(1, sizeof(FacilitiesDataIndication_t)); fdi->itssMessageType = bdi->destinationPort; fdi->data.size = bdi->data.size; fdi->data.buf = malloc(bdi->data.size); memcpy(fdi->data.buf, bdi->data.buf, bdi->data.size); uint8_t buffer[PACKET_MAX_LEN]; buffer[0] = 4; // Facilities asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesDataIndication, NULL, fdi, buffer+1, PACKET_MAX_LEN-1); queue_add(facilities->tx_queue, buffer, enc.encoded+1, 5); pthread_cond_signal(&facilities->tx_queue->trigger); cleanup: if (bdi->destinationPort != Port_denm) ASN_STRUCT_FREE(*its_msg_descriptor, its_msg); ASN_STRUCT_FREE(asn_DEF_BTPDataIndication, bdi); ASN_STRUCT_FREE(asn_DEF_FacilitiesDataIndication, fdi); return rv; } static int facilities_request(facilities_t *facilities, void* responder, uint8_t *msg, uint32_t msg_len) { int rv = 0; int code = 0; uint8_t *fdres_oer = NULL; asn_enc_rval_t enc; void *its_msg = NULL; asn_TYPE_descriptor_t *its_msg_def = NULL; bool managed_msg = false; FacilitiesDataRequest_t *fdreq = calloc(1, sizeof(FacilitiesDataRequest_t)); FacilitiesDataResult_t *fdres = calloc(1, sizeof(FacilitiesDataResult_t)); BTPDataRequest_t *bdr = calloc(1, sizeof(BTPDataRequest_t)); asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_FacilitiesDataRequest, (void**) &fdreq, msg, msg_len); if (dec.code) { syslog_err("[facilities]<- invalid FDRequest received"); rv = 1; code = 1; goto cleanup; } switch (fdreq->present) { case FacilitiesDataRequest_PR_singleMessage: ; bool fwd = true; uint64_t transmission_duration = 0; uint32_t transmission_interval = 0; uint64_t transmission_start = 0; switch (fdreq->choice.singleMessage.itssMessageType) { case ItssMessageType_cam: its_msg_def = &asn_DEF_CAM; its_msg = calloc(1, sizeof(CAM_t)); bdr->destinationPort = Port_cam; bdr->gnPacketTransportType = PacketTransportType_shb; bdr->gnTrafficClass = 1; break; case ItssMessageType_denm: its_msg_def = &asn_DEF_DENM; its_msg = calloc(1, sizeof(DENM_t)); bdr->destinationPort = Port_denm; bdr->gnPacketTransportType = PacketTransportType_gbc; bdr->gnTrafficClass = 2; break; case ItssMessageType_ivim: its_msg_def = &asn_DEF_IVIM; its_msg = calloc(1, sizeof(IVIM_t)); bdr->destinationPort = Port_ivim; bdr->gnPacketTransportType = PacketTransportType_shb; bdr->gnTrafficClass = 1; break; default: syslog_err("[facilities] unrecognized FDRequest message type (%ld)", fdreq->choice.singleMessage.itssMessageType); rv = 1; goto cleanup; } dec = uper_decode_complete(NULL, its_msg_def, (void**) &its_msg, fdreq->choice.singleMessage.data.buf, fdreq->choice.singleMessage.data.size); if (dec.code) { syslog_debug("[facilities] invalid FDRequest %s received", its_msg_def->name); rv = 1; goto cleanup; } int64_t id = -1; bool is_update = false; if (fdreq->choice.singleMessage.itssMessageType == ItssMessageType_denm) { managed_msg = true; uint8_t event_type = event_manage(facilities->den, its_msg, &id); // Do not free its_msg! event_manage takes care of the msg // id will get set to another val if EVENT NEW or UPDATE or CANCELLATION or NEGATION if (event_type != EVENT_NEW && event_type != EVENT_UPDATE && event_type != EVENT_CANCELLATION && event_type != EVENT_NEGATION) { fwd = false; } if (event_type == EVENT_UPDATE || event_type == EVENT_CANCELLATION || event_type == EVENT_NEGATION) { is_update = true; } if (fwd) { if ( ((DENM_t*)its_msg)->denm.management.transmissionInterval ) { transmission_interval = *( (uint32_t*) ((DENM_t*)its_msg)->denm.management.transmissionInterval ); if ( ((DENM_t*)its_msg)->denm.management.validityDuration ) { transmission_duration = *( (uint32_t*) ((DENM_t*)its_msg)->denm.management.validityDuration ) * 1000; } else { transmission_duration = facilities->den->default_event_duration * 1000; } } } } else if (fdreq->choice.singleMessage.itssMessageType == ItssMessageType_ivim) { managed_msg = true; uint8_t service_type = service_eval(facilities->infrastructure, SERVICE_IVI, its_msg, &id); if (service_type != SERVICE_NEW && service_type != SERVICE_UPDATE && service_type != SERVICE_CANCELLATION && service_type != SERVICE_NEGATION) { fwd = false; } if (service_type == SERVICE_UPDATE || service_type == SERVICE_CANCELLATION || service_type == SERVICE_NEGATION) { is_update = true; } if (fwd) { uint64_t valid_to, valid_from; if (!((IVIM_t*) its_msg)->ivi.mandatory.validFrom) { struct timespec systemtime; clock_gettime(CLOCK_REALTIME, &systemtime); long now = (long) (systemtime.tv_sec * 1000 + systemtime.tv_nsec / 1E6); now = now - 1072915200000; // Convert EPOCH to 2004/01/01 00:00:000 valid_from = now; } else { asn_INTEGER2ulong((INTEGER_t*) ((IVIM_t*) its_msg)->ivi.mandatory.validFrom, &valid_from); } if (!((IVIM_t*) its_msg)->ivi.mandatory.validTo) { valid_to = valid_from + facilities->infrastructure->default_service_duration; } else { asn_INTEGER2ulong((INTEGER_t*) ((IVIM_t*) its_msg)->ivi.mandatory.validTo, &valid_to); } transmission_start = valid_from; transmission_interval = facilities->infrastructure->replay_interval; transmission_duration = valid_to - valid_from; } } if (!facilities->replay) { transmission_interval = 0; transmission_duration = 0; } // Respond to [applications] fdres->code = FacilitiesResultCode_accepted; fdres_oer = malloc(16); enc = oer_encode_to_buffer(&asn_DEF_FacilitiesDataResult, NULL, fdres, fdres_oer, 16); if (enc.encoded == -1) { syslog_err("[facilities] failed encoding FDResult (%s)", enc.failed_type->name); rv = 1; goto cleanup; } zmq_send(responder, fdres_oer, enc.encoded, 0); // Forward message to [transport] if (fwd) { if (id != -1) { bdr->id = id; } else { bdr->id = 0; } bdr->btpType = BTPType_btpB; bdr->data.buf = malloc(fdreq->choice.singleMessage.data.size); memcpy(bdr->data.buf, fdreq->choice.singleMessage.data.buf, fdreq->choice.singleMessage.data.size); bdr->data.size = fdreq->choice.singleMessage.data.size; bdr->gnDestinationAddress.buf = malloc(6); for (int i = 0; i < 6; ++i) bdr->gnDestinationAddress.buf[i] = 0xff; bdr->gnDestinationAddress.size = 6; if (transmission_start) { bdr->gnRepetitionStart = malloc(sizeof(long)); *bdr->gnRepetitionStart = transmission_start; } if (transmission_interval) { bdr->gnRepetitionInterval = malloc(sizeof(long)); *bdr->gnRepetitionInterval = transmission_interval; } if (transmission_duration) { bdr->gnMaximumRepetitionTime = malloc(sizeof(long)); *bdr->gnMaximumRepetitionTime = transmission_duration; } if (is_update) { bdr->gnIsUpdate = malloc(sizeof(long)); *bdr->gnIsUpdate = 1; } if (facilities->use_security) { bdr->gnSecurityProfile = malloc(sizeof(long)); *bdr->gnSecurityProfile = 1; } // Encode ITS message into OER uint8_t bdr_oer[2048]; bdr_oer[0] = 4; // [facilities] service id enc = oer_encode_to_buffer(&asn_DEF_BTPDataRequest, NULL, bdr, bdr_oer + 1, 2047); if (enc.encoded == -1) { syslog_err("[facilities] failed encoding BDR (%s)", enc.failed_type->name); rv = 1; goto cleanup; } queue_add(facilities->tx_queue, bdr_oer, enc.encoded+1, 3); pthread_cond_signal(&facilities->tx_queue->trigger); } break; case FacilitiesDataRequest_PR_activeEvents: pthread_mutex_lock(&facilities->den->lock); fdres->code = FacilitiesResultCode_accepted; fdres->result = calloc(1, sizeof(FacilitiesResult_t)); fdres->result->present = FacilitiesResult_PR_events; uint16_t nae = facilities->den->n_active_events; switch (fdreq->choice.activeEvents) { case EventType_active: fdres->result->choice.events.list.count = nae; fdres->result->choice.events.list.size = nae * sizeof(DENM_t *); fdres->result->choice.events.list.array = malloc(nae * sizeof(DENM_t *)); for (int i = 0, j = 0; j < nae; ++i) { if (facilities->den->events[i]->state == EVENT_ACTIVE) { fdres->result->choice.events.list.array[j] = calloc(1, sizeof(ItssMessage_t)); fdres->result->choice.events.list.array[j]->itssMessageType = ItssMessageType_denm; fdres->result->choice.events.list.array[j]->data.buf = malloc(2048); asn_enc_rval_t enc = uper_encode_to_buffer(&asn_DEF_DENM, NULL, facilities->den->events[i]->denm, fdres->result->choice.events.list.array[j]->data.buf, 2048); if (enc.encoded == -1) { syslog_err("[facilities] failed encoding DENM for FDResult (%s)", enc.failed_type->name); continue; } fdres->result->choice.events.list.array[j]->data.size = (enc.encoded + 7) / 8; ++j; } } break; default: syslog_err("[facilities] unrecognized FDR event type (%ld)", fdreq->choice.activeEvents); pthread_mutex_unlock(&facilities->den->lock); rv = 1; goto cleanup; } fdres_oer = malloc(16384); enc = oer_encode_to_buffer(&asn_DEF_FacilitiesDataResult, NULL, fdres, fdres_oer, 16384); if (enc.encoded == -1) { syslog_err("[facilities] failed encoding FDResult (%s)", enc.failed_type->name); pthread_mutex_unlock(&facilities->den->lock); rv = 1; goto cleanup; } zmq_send(responder, fdres_oer, enc.encoded, 0); pthread_mutex_unlock(&facilities->den->lock); break; case FacilitiesDataRequest_PR_attributeTypes: fdres->code = FacilitiesResultCode_accepted; int nra = fdreq->choice.attributeTypes.list.count; fdres->result = calloc(1, sizeof(FacilitiesResult_t)); fdres->result->present = FacilitiesResult_PR_attributes; fdres->result->choice.attributes.list.count = nra; fdres->result->choice.attributes.list.size = sizeof(void*) * nra; fdres->result->choice.attributes.list.array = malloc(sizeof(void*) * nra); for (int j = 0; j < nra; ++j) { switch (*fdreq->choice.attributeTypes.list.array[j]) { case FacilitiesAttributeType_stationId: fdres->result->choice.attributes.list.array[j] = calloc(1, sizeof(FacilitiesAttribute_t) ); fdres->result->choice.attributes.list.array[j]->data.size = 8; fdres->result->choice.attributes.list.array[j]->data.buf = malloc(8); pthread_mutex_lock(&facilities->lock); *((uint64_t*) fdres->result->choice.attributes.list.array[j]->data.buf) = facilities->station_id; pthread_mutex_unlock(&facilities->lock); break; default: syslog_debug("[facilities] unrecognized FDR attribute type request"); goto cleanup; } } fdres_oer = malloc(256); enc = oer_encode_to_buffer(&asn_DEF_FacilitiesDataResult, NULL, fdres, fdres_oer, 256); if (enc.encoded == -1) { syslog_err("[facilities] failed encoding FDResult (%s)", enc.failed_type->name); rv = 1; goto cleanup; } zmq_send(responder, fdres_oer, enc.encoded, 0); break; default: syslog_err("[facilities] unrecognized FDR type received (%d)", fdreq->present); rv = 1; goto cleanup; } cleanup: if (rv) { fdres->code = FacilitiesResultCode_rejected; uint8_t fdres_oer[32]; asn_enc_rval_t enc = oer_encode_to_buffer(&asn_DEF_FacilitiesDataResult, NULL, fdres, fdres_oer, 32); zmq_send(responder, fdres_oer, enc.encoded, 0); } free(fdres_oer); if (its_msg_def && !managed_msg) ASN_STRUCT_FREE(*its_msg_def, its_msg); ASN_STRUCT_FREE(asn_DEF_FacilitiesDataResult, fdres); ASN_STRUCT_FREE(asn_DEF_FacilitiesDataRequest, fdreq); ASN_STRUCT_FREE(asn_DEF_BTPDataRequest, bdr); return rv; } static int security_indication(facilities_t *facilities, void* responder_secured, uint8_t *msg, uint32_t msg_len) { int rv = 0; SecurityIndication_t* si = calloc(1, sizeof(SecurityIndication_t)); SecurityResponse_t* sr = calloc(1, sizeof(SecurityResponse_t)); uint8_t buffer[64]; asn_enc_rval_t enc; asn_dec_rval_t dec = oer_decode(NULL, &asn_DEF_SecurityIndication, (void**) &si, msg, msg_len); if (dec.code) { syslog_err("[networking]<- invalid SIndication received"); rv = 1; goto cleanup; } switch (si->choice.idChangeEvent.command) { case SecurityIdChangeEventType_prepare: pthread_mutex_lock(&facilities->lock); break; case SecurityIdChangeEventType_commit: ; facilities->station_id = rand(); pthread_mutex_unlock(&facilities->lock); break; case SecurityIdChangeEventType_abort: pthread_mutex_unlock(&facilities->lock); break; default: syslog_err("[networking]<- unhandled idChangeEvent command type"); rv = 1; goto cleanup; } sr->present = SecurityResponse_PR_idChangeEvent; sr->choice.idChangeEvent.returnCode = 0; enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64); zmq_send(responder_secured, buffer, enc.encoded, 0); cleanup: if (rv) { sr->present = SecurityResponse_PR_idChangeEvent; sr->choice.idChangeEvent.returnCode = 1; enc = oer_encode_to_buffer(&asn_DEF_SecurityResponse, NULL, sr, buffer, 64); zmq_send(responder_secured, buffer, enc.encoded, 0); zmq_recv(responder_secured, buffer, 64, 0); } ASN_STRUCT_FREE(asn_DEF_SecurityResponse, sr); ASN_STRUCT_FREE(asn_DEF_SecurityIndication, si); return rv; } static void* securer(void *fc) { int rv = 0; facilities_t *facilities = (facilities_t*) fc; void *responder_secured = zmq_socket(facilities->ctx, ZMQ_REP); int rc = zmq_bind(responder_secured, FACILITIES_SECURED_ADDRESS); uint8_t buffer[PACKET_MAX_LEN]; while (!facilities->exit) { zmq_recv(responder_secured, buffer, PACKET_MAX_LEN, 0); rv = security_indication(facilities, responder_secured, buffer, PACKET_MAX_LEN); } return NULL; } void* tx(void* fc) { facilities_t *facilities = (facilities_t*) fc; queue_t* queue = facilities->tx_queue; uint8_t code; void* applications_socket = zmq_socket(facilities->ctx, ZMQ_REQ); zmq_connect(applications_socket, APPLICATION_ADDRESS); void* transport_socket = zmq_socket(facilities->ctx, ZMQ_REQ); zmq_connect(transport_socket, TRANSPORT_ADDRESS); queue_t* stream = queue_init(); while (!facilities->exit) { pthread_mutex_lock(&queue->lock); while (!queue->len) { pthread_cond_wait(&queue->trigger, &queue->lock); } for (int i = 0; i < queue->len; ++i) { memcpy(stream->packet[i], queue->packet[i], queue->packet_len[i]); stream->packet_len[i] = queue->packet_len[i]; stream->destination[i] = queue->destination[i]; } stream->len = queue->len; queue->len = 0; pthread_mutex_unlock(&queue->lock); for (int i = 0; i < stream->len; ++i) { switch (stream->destination[i]) { case 3: syslog_debug("[facilities]-> sending BDR to ->[transport] (%dB)", stream->packet_len[i]); zmq_send(transport_socket, stream->packet[i], stream->packet_len[i], 0); zmq_recv(transport_socket, &code, 1, 0); break; case 5: syslog_debug("[facilities]-> sending FDI to ->[application] (%dB)", stream->packet_len[i]); zmq_send(applications_socket, stream->packet[i], stream->packet_len[i], 0); zmq_recv(applications_socket, &code, 1, 0); break; } } } return NULL; } int main() { syslog_info("[facilities] starting"); facilities_t facilities; facilities.exit = false; facilities.lightship = lightship_init(); facilities.tx_queue = queue_init(); facilities.den = calloc(1, sizeof(den_t)); facilities.infrastructure = calloc(1, sizeof(infrastructure_t)); pthread_mutex_init(&facilities.lock, NULL); struct stat st = {0}; if (stat("/tmp/itss", &st) == -1) { mkdir("/tmp/itss", 0777); } time_t t; srand((unsigned) time(&t)); if (itss_config(&facilities, "/etc/it2s/itss.toml")) return 1; void *context = zmq_ctx_new(); facilities.ctx = context; void *responder = zmq_socket(context, ZMQ_REP); int rc = zmq_bind(responder, FACILITIES_ADDRESS); void *security_socket = zmq_socket(context, ZMQ_REQ); rc = zmq_bind(security_socket, SECURITY_ADDRESS); facilities.lightship->type = facilities.station_type; // Tx pthread_create(&facilities.transmitting, NULL, tx, (void*) &facilities); // Securing pthread_create(&facilities.securing, NULL, securer, (void*) &facilities); // CA pthread_create(&facilities.ca_service, NULL, ca_service, (void*) &facilities); // DEN pthread_create(&facilities.den_service, NULL, den_service, (void*) &facilities); // Infrastructure pthread_create(&facilities.infrastructure_service, NULL, infrastructure_service, (void*) &facilities); uint8_t buffer[PACKET_MAX_LEN]; uint8_t code; syslog_info("[facilities] listening"); while (!facilities.exit) { zmq_recv(responder, buffer, PACKET_MAX_LEN, 0); switch (buffer[0]) { case 3: code = transport_indication(&facilities, responder, buffer+1, PACKET_MAX_LEN-1); break; case 5: code = facilities_request(&facilities, responder, buffer+1, PACKET_MAX_LEN-1); break; default: syslog_debug("[facilities] unrecognized service"); code = 1; zmq_send(responder, &code, 1, 0); continue; } } return 0; }