diff options
Diffstat (limited to 'src/ipcpd')
-rw-r--r-- | src/ipcpd/ipcp.c | 22 | ||||
-rw-r--r-- | src/ipcpd/local/main.c | 40 | ||||
-rw-r--r-- | src/ipcpd/shim-eth-llc/main.c | 55 | ||||
-rw-r--r-- | src/ipcpd/shim-udp/main.c | 85 |
4 files changed, 120 insertions, 82 deletions
diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index 89033c26..00dd69cb 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -114,7 +114,6 @@ void ipcp_fini() ipcp_data_destroy(ipcpi.data); pthread_cond_destroy(&ipcpi.state_cond); - pthread_mutex_destroy(&ipcpi.state_mtx); pthread_rwlock_destroy(&ipcpi.state_lock); } @@ -234,7 +233,8 @@ void * ipcp_main_loop(void * o) int fd = -1; pthread_rwlock_rdlock(&ipcpi.state_lock); - if (ipcp_get_state() == IPCP_SHUTDOWN) { + if (ipcp_get_state() == IPCP_SHUTDOWN + || ipcp_get_state() == IPCP_NULL) { pthread_rwlock_unlock(&ipcpi.state_lock); break; } @@ -281,7 +281,8 @@ void * ipcp_main_loop(void * o) if (conf_msg->ipcp_type == IPCP_NORMAL) { conf.addr_size = conf_msg->addr_size; conf.cep_id_size = conf_msg->cep_id_size; - conf.pdu_length_size = conf_msg->pdu_length_size; + conf.pdu_length_size = + conf_msg->pdu_length_size; conf.qos_id_size = conf_msg->qos_id_size; conf.seqno_size = conf_msg->seqno_size; conf.has_ttl = conf_msg->has_ttl; @@ -348,7 +349,7 @@ void * ipcp_main_loop(void * o) } fd = np1_flow_alloc(msg->api, msg->port_id); if (fd < 0) { - LOG_ERR("Could not get fd for port_id. %d", + LOG_ERR("Failed allocating fd on port_id %d.", msg->port_id); ret_msg.has_result = true; ret_msg.result = -1; @@ -361,11 +362,6 @@ void * ipcp_main_loop(void * o) msg->dst_name, msg->src_ae_name, msg->qos_cube); - if (ret_msg.result < 0) { - LOG_DBG("Deallocate failed on port_id %d.", - msg->port_id); - flow_dealloc(fd); - } break; case IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP: if (ipcpi.ops->ipcp_flow_alloc_resp == NULL) { @@ -374,10 +370,10 @@ void * ipcp_main_loop(void * o) } if (!msg->response) { - fd = np1_flow_resp(msg->api, msg->port_id); + fd = np1_flow_resp(msg->port_id); if (fd < 0) { - LOG_ERR("Could not get fd for port_id %d.", - msg->port_id); + LOG_WARN("Port_id %d is not known.", + msg->port_id); ret_msg.has_result = true; ret_msg.result = -1; break; @@ -396,7 +392,7 @@ void * ipcp_main_loop(void * o) fd = np1_flow_dealloc(msg->port_id); if (fd < 0) { - LOG_ERR("Could not deallocate port_id %d.", + LOG_WARN("Could not deallocate port_id %d.", msg->port_id); ret_msg.has_result = true; ret_msg.result = -1; diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c index 412795ec..f0c85084 100644 --- a/src/ipcpd/local/main.c +++ b/src/ipcpd/local/main.c @@ -26,7 +26,6 @@ #include <ouroboros/logs.h> #include <ouroboros/errno.h> #include <ouroboros/dev.h> -#include <ouroboros/fcntl.h> #include <ouroboros/fqueue.h> #include <ouroboros/ipcp-dev.h> #include <ouroboros/local-dev.h> @@ -38,7 +37,6 @@ #include <stdlib.h> #include <pthread.h> #include <sys/wait.h> -#include <fcntl.h> #include <assert.h> #define EVENT_WAIT_TIMEOUT 100 /* us */ @@ -90,14 +88,10 @@ static void * ipcp_local_sdu_loop(void * o) (void) o; - while (true) { + while (flow_event_wait(local_data.flows, local_data.fq, &timeout)) { int fd; ssize_t idx; - if (flow_event_wait(local_data.flows, local_data.fq, &timeout) - == -ETIMEDOUT) - continue; - pthread_rwlock_rdlock(&ipcpi.state_lock); if (ipcp_get_state() != IPCP_ENROLLED) { @@ -140,7 +134,11 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) pthread_rwlock_wrlock(&ipcpi.state_lock); - ipcp_set_state(IPCP_SHUTDOWN); + if (ipcp_get_state() == IPCP_INIT) + ipcp_set_state(IPCP_NULL); + + if (ipcp_get_state() == IPCP_ENROLLED) + ipcp_set_state(IPCP_SHUTDOWN); pthread_rwlock_unlock(&ipcpi.state_lock); } @@ -154,9 +152,6 @@ static int ipcp_local_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); - /* this IPCP doesn't need to maintain its dif_name */ - free(conf->dif_name); - pthread_rwlock_wrlock(&ipcpi.state_lock); if (ipcp_get_state() != IPCP_INIT) { @@ -165,6 +160,9 @@ static int ipcp_local_bootstrap(struct dif_config * conf) return -1; } + /* this IPCP doesn't need to maintain its dif_name */ + free(conf->dif_name); + ipcp_set_state(IPCP_ENROLLED); pthread_create(&local_data.sduloop, NULL, ipcp_local_sdu_loop, NULL); @@ -231,7 +229,8 @@ static int ipcp_local_flow_alloc(int fd, LOG_DBG("Allocating flow to %s on fd %d.", dst_name, fd); - assert(dst_name || src_ae_name); + assert(dst_name); + assert(src_ae_name); pthread_rwlock_rdlock(&ipcpi.state_lock); @@ -296,17 +295,24 @@ static int ipcp_local_flow_dealloc(int fd) ipcp_flow_fini(fd); pthread_rwlock_rdlock(&ipcpi.state_lock); + + if (ipcp_get_state() != IPCP_ENROLLED) { + pthread_rwlock_unlock(&ipcpi.state_lock); + LOG_DBG("Won't register with non-enrolled IPCP."); + return -1; /* -ENOTENROLLED */ + } + pthread_rwlock_wrlock(&local_data.lock); flow_set_del(local_data.flows, fd); local_data.in_out[fd] = -1; + flow_dealloc(fd); + pthread_rwlock_unlock(&local_data.lock); pthread_rwlock_unlock(&ipcpi.state_lock); - flow_dealloc(fd); - LOG_INFO("Flow with fd %d deallocated.", fd); return 0; @@ -382,8 +388,10 @@ int main(int argc, char * argv[]) ipcp_fini(); - pthread_cancel(local_data.sduloop); - pthread_join(local_data.sduloop, NULL); + if (ipcp_get_state() == IPCP_SHUTDOWN) { + pthread_cancel(local_data.sduloop); + pthread_join(local_data.sduloop, NULL); + } local_data_fini(); diff --git a/src/ipcpd/shim-eth-llc/main.c b/src/ipcpd/shim-eth-llc/main.c index b7b9f783..3f3c0e1e 100644 --- a/src/ipcpd/shim-eth-llc/main.c +++ b/src/ipcpd/shim-eth-llc/main.c @@ -31,7 +31,6 @@ #include <ouroboros/bitmap.h> #include <ouroboros/dev.h> #include <ouroboros/ipcp-dev.h> -#include <ouroboros/fcntl.h> #include <ouroboros/fqueue.h> #include <ouroboros/logs.h> @@ -625,18 +624,22 @@ static void * eth_llc_ipcp_sdu_writer(void * o) (void) o; - while (true) { - if (flow_event_wait(eth_llc_data.np1_flows, - eth_llc_data.fq, - &timeout) == -ETIMEDOUT) - continue; + while (flow_event_wait(eth_llc_data.np1_flows, + eth_llc_data.fq, + &timeout)) { + pthread_rwlock_rdlock(&ipcpi.state_lock); + + if (ipcp_get_state() != IPCP_ENROLLED) { + pthread_rwlock_unlock(&ipcpi.state_lock); + return (void *) -1; /* -ENOTENROLLED */ + } while ((fd = fqueue_next(eth_llc_data.fq)) >= 0) { if (ipcp_flow_read(fd, &sdb)) { LOG_ERR("Bad read from fd %d.", fd); continue; } - pthread_rwlock_rdlock(&ipcpi.state_lock); + pthread_rwlock_rdlock(ð_llc_data.flows_lock); ssap = reverse_bits(eth_llc_data.fd_to_ef[fd].sap); @@ -646,7 +649,6 @@ static void * eth_llc_ipcp_sdu_writer(void * o) MAC_SIZE); pthread_rwlock_unlock(ð_llc_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); eth_llc_ipcp_send_frame(r_addr, dsap, ssap, shm_du_buff_head(sdb), @@ -654,6 +656,8 @@ static void * eth_llc_ipcp_sdu_writer(void * o) - shm_du_buff_head(sdb)); ipcp_flow_del(sdb); } + + pthread_rwlock_unlock(&ipcpi.state_lock); } return (void *) 1; @@ -673,7 +677,11 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) pthread_rwlock_wrlock(&ipcpi.state_lock); - ipcp_set_state(IPCP_SHUTDOWN); + if (ipcp_get_state() == IPCP_INIT) + ipcp_set_state(IPCP_NULL); + + if (ipcp_get_state() == IPCP_ENROLLED) + ipcp_set_state(IPCP_SHUTDOWN); pthread_rwlock_unlock(&ipcpi.state_lock); } @@ -702,9 +710,6 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); - /* this IPCP doesn't need to maintain its dif_name */ - free(conf->dif_name); - if (conf->if_name == NULL) { LOG_ERR("Interface name is NULL."); return -1; @@ -831,7 +836,6 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) if (ipcp_get_state() != IPCP_INIT) { pthread_rwlock_unlock(&ipcpi.state_lock); LOG_ERR("IPCP in wrong state."); - close(skfd); return -1; } @@ -855,6 +859,9 @@ static int eth_llc_ipcp_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&ipcpi.state_lock); + /* this IPCP doesn't need to maintain its dif_name */ + free(conf->dif_name); + LOG_DBG("Bootstrapped shim IPCP over Ethernet with LLC with api %d.", getpid()); @@ -1043,6 +1050,13 @@ static int eth_llc_ipcp_flow_dealloc(int fd) ipcp_flow_fini(fd); pthread_rwlock_rdlock(&ipcpi.state_lock); + + if (ipcp_get_state() != IPCP_ENROLLED) { + pthread_rwlock_unlock(&ipcpi.state_lock); + LOG_DBG("Won't register with non-enrolled IPCP."); + return -1; /* -ENOTENROLLED */ + } + pthread_rwlock_wrlock(ð_llc_data.flows_lock); flow_set_del(eth_llc_data.np1_flows, fd); @@ -1056,11 +1070,11 @@ static int eth_llc_ipcp_flow_dealloc(int fd) eth_llc_data.ef_to_fd[sap] = -1; + flow_dealloc(fd); + pthread_rwlock_unlock(ð_llc_data.flows_lock); pthread_rwlock_unlock(&ipcpi.state_lock); - flow_dealloc(fd); - LOG_DBG("Flow with fd %d deallocated.", fd); return 0; @@ -1135,11 +1149,12 @@ int main(int argc, char * argv[]) ipcp_fini(); - pthread_cancel(eth_llc_data.sdu_reader); - pthread_cancel(eth_llc_data.sdu_writer); - - pthread_join(eth_llc_data.sdu_writer, NULL); - pthread_join(eth_llc_data.sdu_reader, NULL); + if (ipcp_get_state() == IPCP_SHUTDOWN) { + pthread_cancel(eth_llc_data.sdu_reader); + pthread_cancel(eth_llc_data.sdu_writer); + pthread_join(eth_llc_data.sdu_writer, NULL); + pthread_join(eth_llc_data.sdu_reader, NULL); + } eth_llc_data_fini(); diff --git a/src/ipcpd/shim-udp/main.c b/src/ipcpd/shim-udp/main.c index c90b47a2..eff0bd94 100644 --- a/src/ipcpd/shim-udp/main.c +++ b/src/ipcpd/shim-udp/main.c @@ -28,7 +28,6 @@ #include <ouroboros/dev.h> #include <ouroboros/ipcp-dev.h> #include <ouroboros/fqueue.h> -#include <ouroboros/fcntl.h> #include <ouroboros/errno.h> #include <ouroboros/logs.h> @@ -453,7 +452,6 @@ static void * ipcp_udp_sdu_reader(void * o) continue; flags = fcntl(skfd, F_GETFL, 0); fcntl(skfd, F_SETFL, flags | O_NONBLOCK); - fd = udp_data.uf_to_fd[skfd]; n = sizeof(r_saddr); if ((n = recvfrom(skfd, &buf, @@ -463,7 +461,14 @@ static void * ipcp_udp_sdu_reader(void * o) (unsigned *) &n)) <= 0) continue; + pthread_rwlock_rdlock(&ipcpi.state_lock); + pthread_rwlock_rdlock(&udp_data.flows_lock); + + fd = udp_data.uf_to_fd[skfd]; flow_write(fd, buf, n); + + pthread_rwlock_unlock(&udp_data.flows_lock); + pthread_rwlock_unlock(&ipcpi.state_lock); } } @@ -478,11 +483,16 @@ static void * ipcp_udp_sdu_loop(void * o) (void) o; - while (true) { - if (flow_event_wait(udp_data.np1_flows, - udp_data.fq, - &timeout) == -ETIMEDOUT) - continue; + while (flow_event_wait(udp_data.np1_flows, udp_data.fq, &timeout)) { + pthread_rwlock_rdlock(&ipcpi.state_lock); + + if (ipcp_get_state() != IPCP_ENROLLED) { + pthread_rwlock_unlock(&ipcpi.state_lock); + return (void *) -1; /* -ENOTENROLLED */ + } + + + pthread_rwlock_rdlock(&udp_data.flows_lock); while ((fd = fqueue_next(udp_data.fq)) >= 0) { if (ipcp_flow_read(fd, &sdb)) { @@ -490,19 +500,18 @@ static void * ipcp_udp_sdu_loop(void * o) continue; } - pthread_rwlock_rdlock(&ipcpi.state_lock); - pthread_rwlock_rdlock(&udp_data.flows_lock); - if (send(udp_data.fd_to_uf[fd].skfd, shm_du_buff_head(sdb), shm_du_buff_tail(sdb) - shm_du_buff_head(sdb), 0) < 0) LOG_ERR("Failed to send SDU."); - pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_unlock(&ipcpi.state_lock); + ipcp_flow_del(sdb); + } + - ipcp_flow_del(sdb); } + pthread_rwlock_unlock(&udp_data.flows_lock); + pthread_rwlock_unlock(&ipcpi.state_lock); } return (void *) 1; @@ -519,7 +528,11 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c) if (info->si_pid == irmd_api) { pthread_rwlock_wrlock(&ipcpi.state_lock); - ipcp_set_state(IPCP_SHUTDOWN); + if (ipcp_get_state() == IPCP_INIT) + ipcp_set_state(IPCP_NULL); + + if (ipcp_get_state() == IPCP_ENROLLED) + ipcp_set_state(IPCP_SHUTDOWN); pthread_rwlock_unlock(&ipcpi.state_lock); } @@ -539,9 +552,6 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) assert(conf); assert(conf->type == THIS_TYPE); - /* this IPCP doesn't need to maintain its dif_name */ - free(conf->dif_name); - if (inet_ntop(AF_INET, &conf->ip_addr, ipstr, @@ -624,6 +634,9 @@ static int ipcp_udp_bootstrap(struct dif_config * conf) pthread_rwlock_unlock(&ipcpi.state_lock); + /* this IPCP doesn't need to maintain its dif_name */ + free(conf->dif_name); + LOG_DBG("Bootstrapped shim IPCP over UDP with api %d.", getpid()); LOG_DBG("Bound to IP address %s.", ipstr); LOG_DBG("DNS server address is %s.", dnsstr); @@ -945,8 +958,8 @@ static int ipcp_udp_flow_alloc(int fd, LOG_DBG("Allocating flow to %s.", dst_name); - if (dst_name == NULL || src_ae_name == NULL) - return -1; + assert(dst_name); + assert(src_ae_name); if (strlen(dst_name) > 255 || strlen(src_ae_name) > 255) { @@ -1098,32 +1111,36 @@ static int ipcp_udp_flow_dealloc(int fd) ipcp_flow_fini(fd); - flow_set_del(udp_data.np1_flows, fd); - pthread_rwlock_rdlock(&ipcpi.state_lock); + + if (ipcp_get_state() != IPCP_ENROLLED) { + pthread_rwlock_unlock(&ipcpi.state_lock); + LOG_DBG("Won't register with non-enrolled IPCP."); + return -1; /* -ENOTENROLLED */ + } + pthread_rwlock_wrlock(&udp_data.flows_lock); + flow_set_del(udp_data.np1_flows, fd); + skfd = udp_data.fd_to_uf[fd].skfd; udp_data.uf_to_fd[skfd] = -1; udp_data.fd_to_uf[fd].udp = -1; udp_data.fd_to_uf[fd].skfd = -1; + close(skfd); + pthread_rwlock_unlock(&udp_data.flows_lock); pthread_rwlock_rdlock(&udp_data.flows_lock); clr_fd(skfd); - pthread_rwlock_unlock(&udp_data.flows_lock); - pthread_rwlock_wrlock(&udp_data.flows_lock); - - close(skfd); + flow_dealloc(fd); pthread_rwlock_unlock(&udp_data.flows_lock); pthread_rwlock_unlock(&ipcpi.state_lock); - flow_dealloc(fd); - LOG_DBG("Flow with fd %d deallocated.", fd); return 0; @@ -1197,13 +1214,15 @@ int main(int argc, char * argv[]) ipcp_fini(); - pthread_cancel(udp_data.handler); - pthread_cancel(udp_data.sdu_reader); - pthread_cancel(udp_data.sduloop); - pthread_join(udp_data.sduloop, NULL); - pthread_join(udp_data.handler, NULL); - pthread_join(udp_data.sdu_reader, NULL); + if (ipcp_get_state() == IPCP_SHUTDOWN) { + pthread_cancel(udp_data.handler); + pthread_cancel(udp_data.sdu_reader); + pthread_cancel(udp_data.sduloop); + pthread_join(udp_data.sduloop, NULL); + pthread_join(udp_data.handler, NULL); + pthread_join(udp_data.sdu_reader, NULL); + } udp_data_fini(); |