void module_reply_fcf(struct fins_module *module, struct finsFrame *ff, uint32_t ret_val, uint32_t ret_msg) { PRINT_DEBUG("Entered: module=%p, ff=%p, ret_val=%u, ret_msg=%u", module, ff, ret_val, ret_msg); secure_metadata_writeToElement(ff->metaData, "ret_msg", &ret_msg, META_TYPE_INT32); ff->destinationID = ff->ctrlFrame.sender_id; ff->ctrlFrame.sender_id = module->index; switch (ff->ctrlFrame.opcode) { case CTRL_ALERT: ff->ctrlFrame.opcode = CTRL_ALERT_REPLY; break; case CTRL_READ_PARAM: ff->ctrlFrame.opcode = CTRL_READ_PARAM_REPLY; break; case CTRL_SET_PARAM: ff->ctrlFrame.opcode = CTRL_SET_PARAM_REPLY; break; case CTRL_EXEC: ff->ctrlFrame.opcode = CTRL_EXEC_REPLY; break; default: PRINT_ERROR("Unhandled msg case: opcode=%u", ff->ctrlFrame.opcode); exit(-1); return; } ff->ctrlFrame.ret_val = ret_val; module_to_switch(module, ff); }
void core_main() { PRINT_IMPORTANT("Entered"); register_to_signal(SIGRTMIN); sem_init(&control_serial_sem, 0, 1); //TODO remove after gen_control_serial_num() converted to RNG signal(SIGINT, core_termination_handler); //register termination handler int status; int i, j, k; metadata_element *list_elem; int list_num; metadata_element *elem; metadata_element *ip_elem; uint32_t ip_num; //###################################################################### overall = (struct fins_overall *) secure_malloc(sizeof(struct fins_overall)); sem_init(&overall->sem, 0, 1); //###################################################################### overall->envi = (struct envi_record *) secure_malloc(sizeof(struct envi_record)); PRINT_IMPORTANT("loading environment"); metadata *meta_envi = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_envi); status = config_read_file(meta_envi, "envi.cfg"); if (status == META_FALSE) { PRINT_ERROR("%s:%d - %s\n", config_error_file(meta_envi), config_error_line(meta_envi), config_error_text(meta_envi)); metadata_destroy(meta_envi); PRINT_ERROR("todo error"); exit(-1); } //############# if_list PRINT_IMPORTANT("interface list"); overall->envi->if_list = list_create(MAX_INTERFACES); list_elem = config_lookup(meta_envi, "environment.interfaces"); if (list_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } list_num = config_setting_length(list_elem); uint32_t if_index; uint8_t *name; uint64_t mac; uint32_t type; uint32_t if_status; uint32_t mtu; uint32_t flags; struct if_record *ifr; for (i = 0; i < list_num; i++) { elem = config_setting_get_elem(list_elem, i); if (elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "index", (int *) &if_index); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_string(elem, "name", (const char **) &name); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int64(elem, "mac", (long long *) &mac); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "type", (int *) &type); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "status", (int *) &if_status); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "mtu", (int *) &mtu); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "flags", (int *) &flags); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } //############# ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index); if (ifr == NULL) { ifr = (struct if_record *) secure_malloc(sizeof(struct if_record)); ifr->index = if_index; strcpy((char *) ifr->name, (char *) name); ifr->mac = mac; ifr->type = (uint16_t) type; ifr->status = (uint8_t) if_status; ifr->mtu = mtu; ifr->flags = flags; ifr->addr_list = list_create(MAX_FAMILIES); if (list_has_space(overall->envi->if_list)) { list_append(overall->envi->if_list, ifr); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } if (flags & IFF_LOOPBACK) { overall->envi->if_loopback = ifr; } } else { PRINT_ERROR("todo error"); exit(-1); } } //############# if_main PRINT_IMPORTANT("main interface"); uint32_t if_main; status = config_lookup_int(meta_envi, "environment.main_interface", (int *) &if_main); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } overall->envi->if_main = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_main); if (overall->envi->if_main == NULL) { PRINT_ERROR("todo"); } //############# addr_list PRINT_IMPORTANT("address list"); //overall->envi->addr_list = list_create(MAX_INTERFACES * MAX_FAMILIES); //TODO use? list_elem = config_lookup(meta_envi, "environment.addresses"); if (list_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } list_num = config_setting_length(list_elem); uint32_t family; //atm only AF_INET, but eventually also AF_INET6 uint32_t ip[4]; //SIOCGIFADDR //ip uint32_t mask[4]; //SIOCGIFNETMASK //mask uint32_t gw[4]; //? //(ip & mask) | 1; uint32_t bdc[4]; //SIOCGIFBRDADDR //(ip & mask) | ~mask uint32_t dst[4]; //SIOCGIFDSTADDR //dst struct addr_record *addr; for (i = 0; i < list_num; i++) { elem = config_setting_get_elem(list_elem, i); if (elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "if_index", (int *) &if_index); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "family", (int *) &family); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } ip_elem = config_setting_get_member(elem, "ip"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { ip[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "mask"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { mask[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "gw"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { gw[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "bdc"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { bdc[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "dst"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { dst[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } //############ ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index); if (ifr != NULL) { if (ifr->flags & IFF_RUNNING) { if (family == AF_INET) { addr = (struct addr_record *) list_find(ifr->addr_list, addr_is_v4); } else { addr = (struct addr_record *) list_find(ifr->addr_list, addr_is_v6); } if (addr == NULL) { addr = (struct addr_record *) secure_malloc(sizeof(struct addr_record)); addr->if_index = if_index; addr->family = AF_INET; if (family == AF_INET) { addr4_set_addr(&addr->ip, IP4_ADR_P2H(ip[0], ip[1], ip[2],ip[3])); addr4_set_addr(&addr->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3])); addr4_set_addr(&addr->gw, IP4_ADR_P2H(gw[0], gw[1], gw[2], gw[3])); addr4_set_addr(&addr->bdc, IP4_ADR_P2H(bdc[0], bdc[1], bdc[2], bdc[3])); addr4_set_addr(&addr->dst, IP4_ADR_P2H(dst[0], dst[1], dst[2], dst[3])); } else if (family == AF_INET6) { //TODO //addr_set_addr6(&addr->ip, ip); PRINT_ERROR("todo"); } else { //TODO error? PRINT_ERROR("todo error"); exit(-1); } if (list_has_space(ifr->addr_list)) { list_append(ifr->addr_list, addr); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } else { //TODO error PRINT_ERROR("todo: replace or add new?"); } } else { //TODO error PRINT_ERROR("todo: decide just drop or add?"); } } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } //############# route_list PRINT_IMPORTANT("route list"); overall->envi->route_list = list_create(MAX_ROUTES); list_elem = config_lookup(meta_envi, "environment.routes"); if (list_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } list_num = config_setting_length(list_elem); uint32_t metric; //SIOCGIFMETRIC uint32_t timeout; //struct timeval route_stamp; struct route_record *route; for (i = 0; i < list_num; i++) { elem = config_setting_get_elem(list_elem, i); if (elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "if_index", (int *) &if_index); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "family", (int *) &family); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } ip_elem = config_setting_get_member(elem, "dst"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { dst[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "mask"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { mask[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ip_elem = config_setting_get_member(elem, "gw"); if (ip_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } ip_num = config_setting_length(ip_elem); for (j = 0; j < ip_num; j++) { gw[j] = (uint32_t) config_setting_get_int_elem(ip_elem, j); } ////ip = IP4_ADR_P2H(192,168,1,5); status = config_setting_lookup_int(elem, "metric", (int *) &metric); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(elem, "timeout", (int *) &timeout); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } //############ ifr = (struct if_record *) list_find1(overall->envi->if_list, ifr_index_test, &if_index); if (ifr != NULL) { if (ifr->flags & IFF_RUNNING) { route = (struct route_record *) secure_malloc(sizeof(struct route_record)); route->if_index = if_index; route->family = family; if (family == AF_INET) { addr4_set_addr(&route->dst, IP4_ADR_P2H(dst[0], dst[1], dst[2], dst[3])); addr4_set_addr(&route->mask, IP4_ADR_P2H(mask[0], mask[1], mask[2],mask[3])); addr4_set_addr(&route->gw, IP4_ADR_P2H(gw[0], gw[1], gw[2], gw[3])); //addr4_set_addr(&route->ip, IP4_ADR_P2H(ip[0], ip[1], ip[2],ip[3])); } else if (family == AF_INET6) { //TODO //addr_set_addr6(&route->ip, ip); } else { //TODO error? } route->metric = metric; route->timeout = timeout; if (list_has_space(overall->envi->route_list)) { list_append(overall->envi->route_list, route); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } else { //TODO error PRINT_ERROR("todo: decide just drop or add?"); } } } metadata_destroy(meta_envi); //###################################################################### PRINT_IMPORTANT("loading stack"); metadata *meta_stack = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_stack); status = config_read_file(meta_stack, "stack.cfg"); if (status == META_FALSE) { PRINT_ERROR("%s:%d - %s\n", config_error_file(meta_stack), config_error_line(meta_stack), config_error_text(meta_stack)); metadata_destroy(meta_stack); PRINT_ERROR("todo error"); exit(-1); } //############# module_list PRINT_IMPORTANT("module list"); overall->lib_list = list_create(MAX_MODULES); memset(overall->modules, 0, MAX_MODULES * sizeof(struct fins_module *)); overall->admin_list = list_create(MAX_MODULES); uint8_t base_path[100]; memset((char *) base_path, 0, 100); strcpy((char *) base_path, "."); metadata_element *mods_elem = config_lookup(meta_stack, "stack.modules"); if (mods_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } int mods_num = config_setting_length(mods_elem); metadata_element *mod_elem; uint32_t mod_id; uint8_t *mod_lib; uint8_t *mod_name; metadata_element *flows_elem; uint32_t mod_flows[MAX_MOD_FLOWS]; uint32_t mod_flows_num; metadata_element *mod_params; metadata_element *mod_admin; struct fins_library *library; struct fins_module *module; for (i = 0; i < mods_num; i++) { mod_elem = config_setting_get_elem(mods_elem, i); if (mod_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(mod_elem, "id", (int *) &mod_id); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_string(mod_elem, "lib", (const char **) &mod_lib); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_string(mod_elem, "name", (const char **) &mod_name); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } flows_elem = config_setting_get_member(mod_elem, "flows"); if (flows_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } mod_flows_num = config_setting_length(flows_elem); for (j = 0; j < mod_flows_num; j++) { mod_flows[j] = (uint32_t) config_setting_get_int_elem(flows_elem, j); } mod_params = config_setting_get_member(mod_elem, "params"); if (mod_params == NULL) { PRINT_ERROR("todo error"); exit(-1); } mod_admin = config_setting_get_member(mod_elem, "admin"); PRINT_DEBUG("admin=%u", mod_admin != NULL) //############ library = (struct fins_library *) list_find1(overall->lib_list, lib_name_test, mod_lib); if (library == NULL) { library = library_load(mod_lib, base_path); if (library == NULL) { PRINT_ERROR("todo error"); exit(-1); } if (list_has_space(overall->lib_list)) { list_append(overall->lib_list, library); } else { PRINT_ERROR("todo error"); exit(-1); } } module = library->create(i, mod_id, mod_name); if (module == NULL) { //TODO error PRINT_ERROR("todo error"); exit(-1); } library->num_mods++; //TODO move flow to update? or links here? status = module->ops->init(module, mod_flows_num, mod_flows, mod_params, overall->envi); //TODO merge init into create? if (status != 0) { overall->modules[i] = module; if (mod_admin != NULL) { list_append(overall->admin_list, module); } } else { PRINT_ERROR("todo error"); exit(-1); } //free(mod_lib); //don't free, string from libconfig points to metadata memory //free(mod_name); } //############# admin_list //TODO change to admin_list? //list_for_each(overall->admin_list, register_all); list_for_each1(overall->admin_list, assign_overall, overall); //############# linking_list PRINT_IMPORTANT("link list"); overall->link_list = list_create(MAX_TABLE_LINKS); metadata_element *links_elem = config_lookup(meta_stack, "stack.links"); if (links_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } int links_num = config_setting_length(links_elem); metadata_element *link_elem; uint32_t link_id; uint32_t link_src; metadata_element *dsts_elem; uint32_t link_dsts[MAX_MODULES]; int link_dsts_num; struct link_record *link; for (i = 0; i < links_num; i++) { link_elem = config_setting_get_elem(links_elem, i); if (link_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(link_elem, "id", (int *) &link_id); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } status = config_setting_lookup_int(link_elem, "src", (int *) &link_src); if (status == META_FALSE) { PRINT_ERROR("todo error"); exit(-1); } dsts_elem = config_setting_get_member(link_elem, "dsts"); if (dsts_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } link_dsts_num = config_setting_length(dsts_elem); for (j = 0; j < link_dsts_num; j++) { link_dsts[j] = (uint32_t) config_setting_get_int_elem(dsts_elem, j); } //############ link = (struct link_record *) secure_malloc(sizeof(struct link_record)); link->id = link_id; //module = (struct fins_module *) list_find1(overall->envi->module_list, mod_id_test, &link_src); link->src_index = -1; for (j = 0; j < MAX_MODULES; j++) { if (overall->modules[j] != NULL && overall->modules[j]->id == link_src) { link->src_index = overall->modules[j]->index; } } if (link->src_index == -1) { PRINT_ERROR("todo error"); exit(-1); } link->dsts_num = link_dsts_num; for (j = 0; j < link_dsts_num; j++) { //module = (struct fins_module *) list_find1(overall->envi->module_list, mod_id_test, &link_dsts[j]); link->dsts_index[j] = -1; for (k = 0; k < MAX_MODULES; k++) { if (overall->modules[k] != NULL && overall->modules[k]->id == link_dsts[j]) { link->dsts_index[j] = overall->modules[k]->index; } } if (link->dsts_index[j] == (uint32_t) -1) { PRINT_ERROR("todo error"); exit(-1); } } if (list_has_space(overall->link_list)) { list_append(overall->link_list, link); } else { //TODO error PRINT_ERROR("todo error"); exit(-1); } } metadata_destroy(meta_stack); //###################################################################### PRINT_IMPORTANT("update modules"); //send out subset of linking table to each module as update //TODO table subset update struct linked_list *link_subset_list; metadata *meta_update; struct finsFrame *ff_update; for (i = 0; i < MAX_MODULES; i++) { if (overall->modules[i] != NULL) { link_subset_list = list_filter1(overall->link_list, link_involved_test, &overall->modules[i]->index, link_clone); //TODO is mem leak PRINT_IMPORTANT("i=%d, link_subset_list=%p, len=%d", i, link_subset_list, link_subset_list->len); meta_update = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_update); //TODO decide on metadata params? //uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); //secure_metadata_writeToElement(meta_update, "send_src_ip", &host_ip, META_TYPE_INT32); ff_update = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff_update->dataOrCtrl = FF_CONTROL; ff_update->destinationID = i; ff_update->metaData = meta_update; ff_update->ctrlFrame.sender_id = 0; ff_update->ctrlFrame.serial_num = gen_control_serial_num(); ff_update->ctrlFrame.opcode = CTRL_SET_PARAM; ff_update->ctrlFrame.param_id = MOD_SET_PARAM_LINKS; ff_update->ctrlFrame.data_len = sizeof(struct linked_list); ff_update->ctrlFrame.data = (uint8_t *) link_subset_list; module_to_switch(overall->modules[0], ff_update); } } //############ say by this point envi var completely init'd //assumed always connect/init to switch first pthread_attr_t attr; pthread_attr_init(&attr); PRINT_IMPORTANT("modules: run"); for (i = 0; i < MAX_MODULES; i++) { if (overall->modules[i] != NULL) { overall->modules[i]->ops->run(overall->modules[i], &attr); } } //############ mini test //sleep(5); char recv_data[4000]; //while (1) { PRINT_IMPORTANT("waiting..."); gets(recv_data); if (0) { metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); uint32_t host_port = 55454; uint32_t dst_ip = IP4_ADR_P2H(192,168,1,3); uint32_t dst_port = 44444; uint32_t ttl = 64; uint32_t tos = 64; secure_metadata_writeToElement(meta, "send_src_ip", &host_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &host_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_ttl", &ttl, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_tos", &tos, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame *) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = 1; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_UP; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(10); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending ARP req"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //uint32_t dst_ip = IP4_ADR_P2H(172, 31, 54, 169); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 20); //uint32_t src_ip = IP4_ADR_P2H(172, 31, 50, 160); secure_metadata_writeToElement(meta_req, "dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "src_ip", &src_ip, META_TYPE_INT32); struct finsFrame *ff_req = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff_req->dataOrCtrl = FF_CONTROL; ff_req->destinationID = 1; //arp ff_req->metaData = meta_req; ff_req->ctrlFrame.sender_id = 4; //ipv4 ff_req->ctrlFrame.serial_num = gen_control_serial_num(); ff_req->ctrlFrame.opcode = CTRL_EXEC; ff_req->ctrlFrame.param_id = 0; //EXEC_ARP_GET_ADDR; ff_req->ctrlFrame.data_len = 0; ff_req->ctrlFrame.data = NULL; PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff_req, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff_req); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t ether_type = 0x0800; //ipv4 if_index = 3; //wlan0 uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw secure_metadata_writeToElement(meta_req, "send_ether_type", ðer_type, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_if_index", &if_index, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = 1; //arp ff->metaData = meta_req; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 100; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 59, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t src_port = 6666; uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t dst_port = 5555; secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &src_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } if (1) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } while (1) ; sleep(5); PRINT_IMPORTANT("waiting..."); char recv_data2[4000]; gets(recv_data2); //############ terminating core_termination_handler(0); }
void core_tests(void) { int i = 0; while (1) { PRINT_IMPORTANT("waiting..."); //sleep(10); //char recv_data[4000]; //gets(recv_data); fgetc(stdin); //wait until user enters PRINT_IMPORTANT("active"); i++; if (i == 1) { metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t host_ip = IP4_ADR_P2H(192,168,1,8); uint32_t host_port = 55454; uint32_t dst_ip = IP4_ADR_P2H(192,168,1,3); uint32_t dst_port = 44444; uint32_t ttl = 64; uint32_t tos = 64; secure_metadata_writeToElement(meta, "send_src_ip", &host_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &host_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_ttl", &ttl, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_tos", &tos, META_TYPE_INT32); uint32_t src_index = 1; uint32_t dst_index = 2; struct finsFrame *ff = (struct finsFrame *) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_UP; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(10); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[src_index]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[src_index], ff); } if (0) { PRINT_DEBUG("Sending ARP req"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //uint32_t dst_ip = IP4_ADR_P2H(172, 31, 54, 169); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 20); //uint32_t src_ip = IP4_ADR_P2H(172, 31, 50, 160); secure_metadata_writeToElement(meta_req, "dst_ip", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "src_ip", &src_ip, META_TYPE_INT32); struct finsFrame *ff_req = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff_req->dataOrCtrl = FF_CONTROL; ff_req->destinationID = 1; //arp ff_req->metaData = meta_req; ff_req->ctrlFrame.sender_id = 4; //ipv4 ff_req->ctrlFrame.serial_num = gen_control_serial_num(); ff_req->ctrlFrame.opcode = CTRL_EXEC; ff_req->ctrlFrame.param_id = 0; //EXEC_ARP_GET_ADDR; ff_req->ctrlFrame.data_len = 0; ff_req->ctrlFrame.data = NULL; PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff_req, meta_req, overall->modules[0]->name, overall->modules[1]->name); module_to_switch(overall->modules[0], ff_req); } if (i == 2) { PRINT_DEBUG("Sending data"); metadata *meta_req = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta_req); uint32_t ether_type = 0x0800; //ipv4 int32_t if_index = 3; //wlan0 uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 5); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t src_index = 2; uint32_t dst_index = 1; secure_metadata_writeToElement(meta_req, "send_ether_type", ðer_type, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_if_index", &if_index, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta_req, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta_req; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 100; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 59, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta_req, overall->modules[src_index]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[src_index], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 4); //wlan0 uint32_t src_port = 6666; uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 1); //gw uint32_t dst_port = 5555; secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_port", &src_port, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_port", &dst_port, META_TYPE_INT32); uint32_t dst_index = 8; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; //arp ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } if (0) { PRINT_DEBUG("Sending data"); metadata *meta = (metadata *) secure_malloc(sizeof(metadata)); metadata_create(meta); uint32_t family = AF_INET; uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 15); //wlan0 uint32_t dst_ip = IP4_ADR_P2H(172, 168, 1, 1); //gw secure_metadata_writeToElement(meta, "send_family", &family, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_src_ipv4", &src_ip, META_TYPE_INT32); secure_metadata_writeToElement(meta, "send_dst_ipv4", &dst_ip, META_TYPE_INT32); uint32_t dst_index = 4; struct finsFrame *ff = (struct finsFrame*) secure_malloc(sizeof(struct finsFrame)); ff->dataOrCtrl = FF_DATA; ff->destinationID = dst_index; ff->metaData = meta; ff->dataFrame.directionFlag = DIR_DOWN; ff->dataFrame.pduLength = 10; ff->dataFrame.pdu = (uint8_t *) secure_malloc(ff->dataFrame.pduLength); memset(ff->dataFrame.pdu, 65, ff->dataFrame.pduLength); PRINT_IMPORTANT("sending: ff=%p, meta=%p, src='%s' to dst='%s'", ff, meta, overall->modules[0]->name, overall->modules[dst_index]->name); module_to_switch(overall->modules[0], ff); } //break; } }
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 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); }