diff options
Diffstat (limited to 'src/lib/dev.c')
| -rw-r--r-- | src/lib/dev.c | 143 |
1 files changed, 99 insertions, 44 deletions
diff --git a/src/lib/dev.c b/src/lib/dev.c index 92310b9e..cb483aca 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -99,7 +99,7 @@ struct flow { uint16_t oflags; ssize_t part_idx; - struct crypt_info crypt; + struct crypt_ctx * crypt; struct timespec snd_act; struct timespec rcv_act; @@ -227,7 +227,7 @@ static enum flow_state flow_wait_assign(int flow_id) static int proc_announce(const char * prog) { uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; int err; if (proc_announce__irm_req_ser(&msg, prog) < 0) @@ -244,7 +244,7 @@ static int proc_announce(const char * prog) static void proc_exit(void) { uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; if (proc_exit__irm_req_ser(&msg) < 0) return; @@ -252,6 +252,69 @@ static void proc_exit(void) send_recv_msg(&msg); } +static int sdb_encrypt(struct flow * flow, + struct shm_du_buff * sdb) +{ + buffer_t in; + buffer_t out; + uint8_t * head; + uint8_t * tail; + + if (flow->crypt == NULL) + return 0; /* No encryption */ + + in.data = shm_du_buff_head(sdb); + in.len = shm_du_buff_len(sdb); + + if (crypt_encrypt(flow->crypt, in, &out) < 0) + goto fail_encrypt; + + head = shm_du_buff_head_alloc(sdb, IVSZ); + if (head == NULL) + goto fail_alloc; + + tail = shm_du_buff_tail_alloc(sdb, (out.len - in.len) - IVSZ); + if (tail == NULL) + goto fail_alloc; + + memcpy(head, out.data, out.len); + + freebuf(out); + + return 0; + fail_alloc: + freebuf(out); + fail_encrypt: + return -ECRYPT; +} + +static int sdb_decrypt(struct flow * flow, + struct shm_du_buff * sdb) +{ + buffer_t in; + buffer_t out; + uint8_t * head; + + if (flow->crypt == NULL) + return 0; /* No decryption */ + + in.data = shm_du_buff_head(sdb); + in.len = shm_du_buff_len(sdb); + + if (crypt_decrypt(flow->crypt, in, &out) < 0) + return -ENOMEM; + + + head = shm_du_buff_head_release(sdb, IVSZ) + IVSZ; + shm_du_buff_tail_release(sdb, (in.len - out.len) - IVSZ); + + memcpy(head, out.data, out.len); + + freebuf(out); + + return 0; +} + #include "frct.c" void * flow_tx(void * o) @@ -309,18 +372,18 @@ static void _flow_keepalive(struct flow * flow) timeo = flow->info.qs.timeout; acl = shm_rbuff_get_acl(flow->rx_rb); - if (timeo == 0 || acl & (ACL_FLOWPEER | ACL_FLOWDOWN)) + if (timeo == 0 || acl & (ACL_FLOWPEER | ACL_FLOWDOWN)) return; clock_gettime(PTHREAD_COND_CLOCK, &now); - if (ts_diff_ns(&r_act, &now) > (int64_t) timeo * MILLION) { + if (ts_diff_ns(&now, &r_act) > (int64_t) timeo * MILLION) { shm_rbuff_set_acl(flow->rx_rb, ACL_FLOWPEER); shm_flow_set_notify(ai.fqset, flow_id, FLOW_PEER); return; } - if (ts_diff_ns(&s_act, &now) > (int64_t) timeo * (MILLION >> 2)) { + if (ts_diff_ns(&now, &s_act) > (int64_t) timeo * (MILLION >> 2)) { pthread_rwlock_unlock(&ai.lock); flow_send_keepalive(flow, now); @@ -423,7 +486,7 @@ static void __flow_fini(int fd) shm_flow_set_close(ai.flows[fd].set); } - crypt_fini(&ai.flows[fd].crypt); + crypt_destroy_ctx(ai.flows[fd].crypt); list_del(&ai.flows[fd].next); @@ -477,16 +540,14 @@ static int flow_init(struct flow_info * info, flow->part_idx = NO_PART; flow->snd_act = now; flow->rcv_act = now; + flow->crypt = NULL; - flow->crypt.flags = info->qs.cypher_s; /* TODO: move cypher_s */ - - memset(flow->crypt.key, 0, SYMMKEYSZ); - - if (flow->crypt.flags > 0 && sk!= NULL && sk->data != NULL) - memcpy(flow->crypt.key, sk->data , sk->len); - - if (crypt_init(&flow->crypt) < 0) - goto fail_crypt; + if (sk!= NULL && sk->data != NULL) { + assert(sk->len == SYMMKEYSZ); + flow->crypt = crypt_create_ctx(sk->data); + if (flow->crypt == NULL) + goto fail_crypt; + } assert(flow->frcti == NULL); @@ -519,7 +580,7 @@ static int flow_init(struct flow_info * info, fail_flow_set_add: frcti_destroy(flow->frcti); fail_frcti: - crypt_fini(&flow->crypt); + crypt_destroy_ctx(flow->crypt); fail_crypt: shm_flow_set_close(flow->set); fail_set: @@ -764,7 +825,7 @@ int flow_accept(qosspec_t * qs, { struct flow_info flow; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; buffer_t sk; int fd; int err; @@ -805,7 +866,7 @@ int flow_alloc(const char * dst, { struct flow_info flow; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; buffer_t sk; /* symmetric key */ int fd; int err; @@ -824,8 +885,10 @@ int flow_alloc(const char * dst, return -ENOMEM; err = send_recv_msg(&msg); - if (err < 0) + if (err < 0) { + printf("send_recv_msg error %d\n", err); return err; + } err = flow__irm_result_des(&msg, &flow, &sk); if (err < 0) @@ -842,28 +905,20 @@ int flow_alloc(const char * dst, } int flow_join(const char * dst, - qosspec_t * qs, const struct timespec * timeo) { struct flow_info flow; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; int fd; int err; -#ifdef QOS_DISABLE_CRC - if (qs != NULL) - qs->ber = 1; -#endif - if (qs != NULL && qs->cypher_s > 0) - return -ENOTSUP; /* TODO: Encrypted broadcast */ - memset(&flow, 0, sizeof(flow)); flow.n_pid = getpid(); - flow.qs = qs == NULL ? qos_raw : *qs; + flow.qs = qos_np1; - if (flow_alloc__irm_req_ser(&msg, &flow, dst, timeo)) + if (flow_join__irm_req_ser(&msg, &flow, dst, timeo)) return -ENOMEM; err = send_recv_msg(&msg); @@ -876,9 +931,6 @@ int flow_join(const char * dst, fd = flow_init(&flow, NULL); - if (qs != NULL) - *qs = flow.qs; - return fd; } @@ -888,7 +940,7 @@ int flow_dealloc(int fd) struct flow_info info; uint8_t pkt[PKT_BUF_LEN]; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; struct timespec tic = TIMESPEC_INIT_NS(TICTIME); struct timespec timeo = TIMESPEC_INIT_S(0); struct flow * flow; @@ -962,7 +1014,7 @@ int ipcp_flow_dealloc(int fd) { struct flow_info info; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; struct flow * flow; int err; @@ -1200,7 +1252,7 @@ static int flow_tx_sdb(struct flow * flow, if (frcti_snd(flow->frcti, sdb) < 0) goto enomem; - if (crypt_encrypt(&flow->crypt, sdb) < 0) + if (sdb_encrypt(flow, sdb) < 0) goto enomem; if (flow->info.qs.ber == 0 && add_crc(sdb) != 0) @@ -1302,7 +1354,7 @@ static bool invalid_pkt(struct flow * flow, if (flow->info.qs.ber == 0 && chk_crc(sdb) != 0) return true; - if (crypt_decrypt(&flow->crypt, sdb) < 0) + if (sdb_decrypt(flow, sdb) < 0) return true; return false; @@ -1330,6 +1382,7 @@ static ssize_t flow_rx_sdb(struct flow * flow, pthread_rwlock_unlock(&ai.lock); *sdb = shm_rdrbuff_get(ai.rdrb, idx); + if (invalid_pkt(flow, *sdb)) { shm_rdrbuff_remove(ai.rdrb, idx); return -EAGAIN; @@ -1767,11 +1820,12 @@ int np1_flow_dealloc(int flow_id, return fd; } -int np1_flow_resp(int flow_id) +int np1_flow_resp(int flow_id, + int resp) { int fd; - if (flow_wait_assign(flow_id) != FLOW_ALLOCATED) + if (resp == 0 && flow_wait_assign(flow_id) != FLOW_ALLOCATED) return -1; pthread_rwlock_rdlock(&ai.lock); @@ -1786,7 +1840,7 @@ int np1_flow_resp(int flow_id) int ipcp_create_r(const struct ipcp_info * info) { uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; int err; if (ipcp_create_r__irm_req_ser(&msg,info) < 0) @@ -1806,7 +1860,7 @@ int ipcp_flow_req_arr(const buffer_t * dst, { struct flow_info flow; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; int err; memset(&flow, 0, sizeof(flow)); @@ -1832,6 +1886,7 @@ int ipcp_flow_req_arr(const buffer_t * dst, flow.n_1_pid = flow.n_pid; flow.n_pid = getpid(); flow.mpl = 0; + flow.qs = qos_np1; return flow_init(&flow, NULL); } @@ -1843,7 +1898,7 @@ int ipcp_flow_alloc_reply(int fd, { struct flow_info flow; uint8_t buf[SOCK_BUF_SIZE]; - buffer_t msg = {buf, SOCK_BUF_SIZE}; + buffer_t msg = {SOCK_BUF_SIZE, buf}; int err; assert(fd >= 0 && fd < SYS_MAX_FLOWS); @@ -1943,7 +1998,7 @@ int np1_flow_read(int fd, pthread_rwlock_rdlock(&ai.lock); - idx = shm_rbuff_read(flow->rx_rb);; + idx = shm_rbuff_read(flow->rx_rb); if (idx < 0) { pthread_rwlock_unlock(&ai.lock); return idx; |
