void udp_exec(struct fins_module *module, struct finsFrame *ff) { PRINT_DEBUG("Entered: module=%p, ff=%p, meta=%p", module, ff, ff->metaData); uint32_t host_ip = 0; uint32_t host_port = 0; uint32_t rem_ip = 0; uint32_t rem_port = 0; switch (ff->ctrlFrame.param_id) { case UDP_EXEC_CLEAR_SENT: PRINT_DEBUG("param_id=UDP_EXEC_CLEAR_SENT (%d)", ff->ctrlFrame.param_id); secure_metadata_readFromElement(ff->metaData, "host_ip", &host_ip); secure_metadata_readFromElement(ff->metaData, "host_port", &host_port); secure_metadata_readFromElement(ff->metaData, "rem_ip", &rem_ip); secure_metadata_readFromElement(ff->metaData, "rem_port", &rem_port); udp_exec_clear_sent(module, ff, host_ip, (uint16_t) host_port, rem_ip, (uint16_t) rem_port); break; default: PRINT_ERROR("Error unknown param_id=%d", ff->ctrlFrame.param_id); PRINT_WARN("todo"); module_reply_fcf(module, ff, FCF_FALSE, 0); break; } }
void rtm_set_param_reply(struct fins_module *module, struct finsFrame *ff) { PRINT_DEBUG("Entered: module=%p, ff=%p, meta=%p", module, ff, ff->metaData); struct rtm_data *md = (struct rtm_data *) module->data; secure_sem_wait(&md->shared_sem); struct rtm_command *cmd = (struct rtm_command *) list_find1(md->cmd_list, rtm_cmd_serial_test, &ff->ctrlFrame.serial_num); if (cmd != NULL) { list_remove(md->cmd_list, cmd); struct rtm_console *console = (struct rtm_console *) list_find1(md->console_list, rtm_console_id_test, &cmd->console_id); if (console != NULL) { if (ff->ctrlFrame.ret_val == FCF_TRUE) { rtm_send_text(console->fd, "successful"); } else { //send error uint32_t ret_msg; secure_metadata_readFromElement(ff->metaData, "ret_msg", &ret_msg); char temp[100]; sprintf(temp, "unsuccessful, returned error=%u", ret_msg); rtm_send_text(console->fd, temp); } } else { PRINT_WARN("todo error"); } sem_post(&md->shared_sem); free(cmd); } else { sem_post(&md->shared_sem); PRINT_WARN("todo error"); //TODO error, drop freeFinsFrame(ff); } }
void udp_in_fdf(struct fins_module *module, struct finsFrame* ff) { struct udp_data *md = (struct udp_data *) module->data; PRINT_DEBUG("Entered: module=%p, ff=%p, meta=%p", module, ff, ff->metaData); /* point to the necessary data in the FDF */ PRINT_DEBUG("%d", (int)ff); struct udp_header* packet = (struct udp_header*) ff->dataFrame.pdu; uint32_t protocol; secure_metadata_readFromElement(ff->metaData, "recv_protocol", &protocol); uint32_t family; secure_metadata_readFromElement(ff->metaData, "recv_family", &family); uint32_t src_ip; secure_metadata_readFromElement(ff->metaData, "recv_src_ipv4", &src_ip); uint32_t dst_ip; secure_metadata_readFromElement(ff->metaData, "recv_dst_ipv4", &dst_ip); if (protocol != UDP_PT_UDP) { md->stats.wrongProtocol++; md->stats.totalBadDatagrams++; PRINT_WARN("wrong protocol: expected=%u, proto=%u", UDP_PT_UDP, protocol); freeFinsFrame(ff); return; } /* begins checking the UDP packets integrity */ /** TODO Fix the length check below , I will highlighted for now */ uint32_t hlen = ntohs(packet->u_len); if (ff->dataFrame.pduLength != hlen) { md->stats.mismatchingLengths++; md->stats.totalBadDatagrams++; PRINT_DEBUG("UDP_in"); freeFinsFrame(ff); return; } /* the packet is does have an "Ignore checksum" value and fails the checksum, it is thrown away */ uint16_t checksum = UDP_checksum((struct udp_packet*) packet, htonl(src_ip), htonl(dst_ip)); uint32_t src_port = ntohs(packet->u_src); uint32_t dst_port = ntohs(packet->u_dst); PRINT_DEBUG("proto=%u, src=%u:%u, dst=%u:%u", protocol, src_ip, (uint16_t)src_port, dst_ip, (uint16_t)dst_port); PRINT_DEBUG("UDP_checksum=%u, checksum=%u", checksum, ntohs(packet->u_cksum)); if (packet->u_cksum != IGNORE_CHEKSUM) { if (checksum != 0) { md->stats.badChecksum++; md->stats.totalBadDatagrams++; PRINT_ERROR("bad checksum=0x%x, calc=0x%x", packet->u_cksum, checksum); freeFinsFrame(ff); return; } } else { md->stats.noChecksum++; PRINT_DEBUG("ignore checksum=%d", md->stats.noChecksum); } secure_metadata_writeToElement(ff->metaData, "recv_src_port", &src_port, META_TYPE_INT32); secure_metadata_writeToElement(ff->metaData, "recv_dst_port", &dst_port, META_TYPE_INT32); PRINT_DEBUG("PDU Length including UDP header %d", ff->dataFrame.pduLength); PRINT_DEBUG("PDU Length %d", (int)(ff->dataFrame.pduLength - U_HEADER_LEN)); int leng = ff->dataFrame.pduLength; ff->dataFrame.pduLength = leng - U_HEADER_LEN; uint8_t *old = ff->dataFrame.pdu; uint8_t *pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memcpy(pdu, old + U_HEADER_LEN, ff->dataFrame.pduLength); ff->dataFrame.pdu = pdu; //######################### #ifdef DEBUG uint8_t *temp = (uint8_t *) secure_malloc(ff->dataFrame.pduLength + 1); memcpy(temp, ff->dataFrame.pdu, ff->dataFrame.pduLength); temp[ff->dataFrame.pduLength] = '\0'; PRINT_DEBUG("pduLen=%d, pdu='%s'", ff->dataFrame.pduLength, temp); free(temp); #endif //######################### md->stats.totalRecieved++; PRINT_DEBUG("UDP total recv'd=%d, ff=%p, meta=%p", md->stats.totalRecieved, ff, ff->metaData); if (!module_send_flow(module, ff, UDP_FLOW_DAEMON)) { PRINT_ERROR("send to switch error, ff=%p", ff); freeFinsFrame(ff); } PRINT_DEBUG("Freeing: pdu=%p", old); free(old); }
void rtm_read_param_reply(struct fins_module *module, struct finsFrame *ff) { PRINT_DEBUG("Entered: module=%p, ff=%p, meta=%p", module, ff, ff->metaData); struct rtm_data *md = (struct rtm_data *) module->data; secure_sem_wait(&md->shared_sem); struct rtm_command *cmd = (struct rtm_command *) list_find1(md->cmd_list, rtm_cmd_serial_test, &ff->ctrlFrame.serial_num); if (cmd != NULL) { list_remove(md->cmd_list, cmd); struct rtm_console *console = (struct rtm_console *) list_find1(md->console_list, rtm_console_id_test, &cmd->console_id); if (console != NULL) { //TODO extract answer if (ff->ctrlFrame.ret_val == FCF_TRUE) { char temp[100]; int32_t val_int32; int64_t val_int64; float val_float; char *val_str; switch (cmd->param_type) { case META_TYPE_INT32: secure_metadata_readFromElement(ff->metaData, "value", &val_int32); sprintf(temp, "'%s'=%d", cmd->param_str, val_int32); break; case META_TYPE_INT64: secure_metadata_readFromElement(ff->metaData, "value", &val_int64); sprintf(temp, "'%s'=%lld", cmd->param_str, val_int64); break; case META_TYPE_FLOAT: secure_metadata_readFromElement(ff->metaData, "value", &val_float); sprintf(temp, "'%s'=%f", cmd->param_str, val_float); break; case META_TYPE_STRING: secure_metadata_readFromElement(ff->metaData, "value", &val_str); sprintf(temp, "'%s'='%s'", cmd->param_str, val_str); break; default: PRINT_ERROR("todo error"); exit(-1); } rtm_send_text(console->fd, temp); } else { //send error uint32_t ret_msg; secure_metadata_readFromElement(ff->metaData, "ret_msg", &ret_msg); char temp[100]; sprintf(temp, "unsuccessful, returned error=%u", ret_msg); rtm_send_text(console->fd, temp); } } else { PRINT_WARN("todo error"); } sem_post(&md->shared_sem); free(cmd); } else { sem_post(&md->shared_sem); PRINT_WARN("todo error"); //TODO error, drop freeFinsFrame(ff); } }
void udp_out_fdf(struct fins_module *module, struct finsFrame* ff) { struct udp_data *md = (struct udp_data *) module->data; //struct udp_metadata_parsed parsed_meta; //struct udp_packet packet_host; struct udp_packet *packet_netw; uint16_t packet_length; /* read the FDF and make sure everything is correct*/ PRINT_DEBUG("UDP_out, ff=%p, meta=%p", ff, ff->metaData); //print_finsFrame(ff); packet_length = ff->dataFrame.pduLength + U_HEADER_LEN; if (packet_length > IP_MAXLEN) { PRINT_ERROR("todo error, data too long max 65536, len=%d", packet_length); } uint8_t *udp_dataunit = (uint8_t *) secure_malloc(packet_length); packet_netw = (struct udp_packet *) udp_dataunit; uint8_t *pdu = ff->dataFrame.pdu; /** constructs the UDP packet from the FDF and the meta data */ //######################### #ifdef DEBUG if (1) { uint8_t *temp = (uint8_t *) secure_malloc(ff->dataFrame.pduLength + 1); memcpy(temp, pdu, ff->dataFrame.pduLength); temp[ff->dataFrame.pduLength] = '\0'; PRINT_DEBUG("pduLen=%d, pdu='%s'", ff->dataFrame.pduLength, temp); free(temp); } #endif //######################### uint32_t dst_port; uint32_t src_port; uint32_t dst_ip; uint32_t src_ip; uint32_t family; secure_metadata_readFromElement(ff->metaData, "send_family", &family); secure_metadata_readFromElement(ff->metaData, "send_src_ipv4", &src_ip); secure_metadata_readFromElement(ff->metaData, "send_src_port", &src_port); secure_metadata_readFromElement(ff->metaData, "send_dst_ipv4", &dst_ip); secure_metadata_readFromElement(ff->metaData, "send_dst_port", &dst_port); uint32_t protocol = UDP_PROTOCOL; secure_metadata_writeToElement(ff->metaData, "send_protocol", &protocol, META_TYPE_INT32); /** fixing the values because of the conflict between uint16 type and * the 32 bit META_INT_TYPE */ //packet_host.u_src = srcbuf16; //packet_host.u_dst = dstbuf16; //packet_host.u_len = packet_length; //packet_host.u_cksum = 0; packet_netw->u_src = htons((uint16_t) src_port); packet_netw->u_dst = htons((uint16_t) dst_port); packet_netw->u_len = htons(packet_length); packet_netw->u_cksum = 0; memcpy(packet_netw->u_data, pdu, ff->dataFrame.pduLength); PRINT_DEBUG("src=%u:%u, dst=%u:%u, pkt_len=%u", src_ip, (uint16_t)src_port, dst_ip, (uint16_t)dst_port, packet_length); uint16_t checksum = UDP_checksum(packet_netw, htonl(src_ip), htonl(dst_ip)); packet_netw->u_cksum = htons(checksum); //packet_netw->u_cksum = 0; PRINT_DEBUG("checksum (h):0x%x", checksum); //PRINT_DEBUG("%u,%u", src_ip, dst_ip); PRINT_DEBUG("pkt_netw: %d,%d,%d,0x%x", packet_netw->u_src, packet_netw->u_dst, packet_netw->u_len, packet_netw->u_cksum); //ff->dataFrame.pdu = udp_dataunit; /* creates a new FDF to be sent out */ PRINT_DEBUG("%p", udp_dataunit); ff->dataFrame.pduLength = packet_length; ff->dataFrame.pdu = udp_dataunit; md->stats.totalSent++; struct finsFrame *ff_clone = cloneFinsFrame(ff); //TODO add switch & move between ipv4/ipv6 if (!module_send_flow(module, ff, UDP_FLOW_IPV4)) { PRINT_ERROR("send to switch error, ff=%p", ff); freeFinsFrame(ff); } struct udp_sent *sent = udp_sent_create(ff_clone, src_ip, src_port, dst_ip, dst_port); if (list_has_space(md->sent_packet_list)) { list_append(md->sent_packet_list, sent); PRINT_DEBUG("sent_packet_list=%p, len=%u, max=%u", md->sent_packet_list, md->sent_packet_list->len, md->sent_packet_list->max); gettimeofday(&sent->stamp, 0); } else { //PRINT_DEBUG("Clearing sent_packet_list"); //udp_sent_list_gc(udp_sent_packet_list, UDP_MSL_TO_DEFAULT); //if (!udp_sent_list_has_space(udp_sent_packet_list)) { PRINT_DEBUG("Dropping head of sent_packet_list"); struct udp_sent *old = (struct udp_sent *) list_remove_front(md->sent_packet_list); udp_sent_free(old); //} list_append(md->sent_packet_list, sent); PRINT_DEBUG("sent_packet_list=%p, len=%u, max=%u", md->sent_packet_list, md->sent_packet_list->len, md->sent_packet_list->max); gettimeofday(&sent->stamp, 0); } PRINT_DEBUG("Freeing: pdu=%p", pdu); free(pdu); }