This patch adds net key index to send_seg(). This fixes problem with using invalid network key to encrypt application messages. --- mesh/friend.c | 16 ++++++++-------- mesh/net.c | 50 ++++++++++++++++++++++++++++++-------------------- mesh/net.h | 7 ++++--- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/mesh/friend.c b/mesh/friend.c index 6d26de15b..d27da9592 100644 --- a/mesh/friend.c +++ b/mesh/friend.c @@ -97,7 +97,7 @@ static void response_delay(struct l_timeout *timeout, void *user_data) n += 2; seq = mesh_net_next_seq_num(neg->net); print_packet("Tx-NET_OP_FRND_OFFER", msg, n); - mesh_net_transport_send(neg->net, key_id, + mesh_net_transport_send(neg->net, key_id, 0, mesh_net_get_iv_index(neg->net), 0, seq, 0, neg->lp_addr, msg, n); @@ -316,7 +316,7 @@ void friend_clear(struct mesh_net *net, uint16_t src, uint16_t lpn, l_put_be16(lpn, msg + 1); l_put_be16(lpnCounter, msg + 3); - mesh_net_transport_send(net, 0, + mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net), DEFAULT_TTL, 0, 0, src, msg, sizeof(msg)); @@ -332,7 +332,7 @@ static void clear_retry(struct l_timeout *timeout, void *user_data) l_put_be16(neg->lp_addr, msg + 1); l_put_be16(neg->lp_cnt, msg + 3); - mesh_net_transport_send(neg->net, 0, + mesh_net_transport_send(neg->net, 0, 0, mesh_net_get_iv_index(neg->net), DEFAULT_TTL, 0, 0, neg->old_friend, msg, sizeof(msg)); @@ -398,7 +398,7 @@ static void friend_delay_rsp(struct l_timeout *timeout, void *user_data) pkt->u.one[0].data, pkt->last_len); pkt->u.one[0].sent = true; - mesh_net_transport_send(net, frnd->net_key_cur, + mesh_net_transport_send(net, frnd->net_key_cur, 0, pkt->iv_index, pkt->ttl, pkt->u.one[0].seq, pkt->src, pkt->dst, pkt->u.one[0].data, pkt->last_len); @@ -439,7 +439,7 @@ update: l_put_be32(iv_index, upd + 2); upd[6] = false; /* Queue is Empty */ print_packet("Update", upd, sizeof(upd)); - mesh_net_transport_send(net, frnd->net_key_cur, + mesh_net_transport_send(net, frnd->net_key_cur, 0, mesh_net_get_iv_index(net), 0, net_seq, 0, frnd->lp_addr, upd, sizeof(upd)); @@ -487,7 +487,7 @@ void friend_poll(struct mesh_net *net, uint16_t src, bool seq, neg->u.negotiate.clearing = true; l_put_be16(neg->lp_addr, msg + 1); l_put_be16(neg->lp_cnt, msg + 3); - mesh_net_transport_send(net, 0, + mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net), DEFAULT_TTL, 0, 0, neg->old_friend, msg, sizeof(msg)); @@ -596,7 +596,7 @@ void friend_sub_add(struct mesh_net *net, struct mesh_friend *frnd, print_packet("Tx-NET_OP_PROXY_SUB_CONFIRM", msg, sizeof(msg)); net_seq = mesh_net_get_seq_num(net); - mesh_net_transport_send(net, frnd->net_key_cur, + mesh_net_transport_send(net, frnd->net_key_cur, 0, mesh_net_get_iv_index(net), 0, net_seq, 0, frnd->lp_addr, msg, sizeof(msg)); @@ -634,7 +634,7 @@ void friend_sub_del(struct mesh_net *net, struct mesh_friend *frnd, print_packet("Tx-NET_OP_PROXY_SUB_CONFIRM", msg, sizeof(msg)); net_seq = mesh_net_get_seq_num(net); - mesh_net_transport_send(net, frnd->net_key_cur, + mesh_net_transport_send(net, frnd->net_key_cur, 0, mesh_net_get_iv_index(net), 0, net_seq, 0, frnd->lp_addr, msg, sizeof(msg)); diff --git a/mesh/net.c b/mesh/net.c index 5f49034e4..ba9a8caf1 100644 --- a/mesh/net.c +++ b/mesh/net.c @@ -179,6 +179,7 @@ struct mesh_sar { uint32_t seqAuth; uint16_t seqZero; uint16_t app_idx; + uint16_t net_idx; uint16_t src; uint16_t remote; uint16_t len; @@ -1528,7 +1529,8 @@ static void friend_ack_rxed(struct mesh_net *net, uint32_t iv_index, l_queue_foreach(net->friends, enqueue_friend_pkt, &frnd_ack); } -static bool send_seg(struct mesh_net *net, struct mesh_sar *msg, uint8_t seg); +static bool send_seg(struct mesh_net *net, uint16_t net_idx, + struct mesh_sar *msg, uint8_t seg); static void send_frnd_ack(struct mesh_net *net, uint16_t src, uint16_t dst, uint32_t hdr, uint32_t flags) @@ -1566,7 +1568,7 @@ static void send_frnd_ack(struct mesh_net *net, uint16_t src, uint16_t dst, friend_ack_rxed(net, mesh_net_get_iv_index(net), mesh_net_next_seq_num(net), 0, dst, msg); } else { - mesh_net_transport_send(net, 0, + mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net), DEFAULT_TTL, 0, 0, dst, msg, sizeof(msg)); } @@ -1601,9 +1603,10 @@ static void send_net_ack(struct mesh_net *net, struct mesh_sar *sar, return; } - mesh_net_transport_send(net, 0, + mesh_net_transport_send(net, 0, sar->net_idx, mesh_net_get_iv_index(net), DEFAULT_TTL, - 0, src, dst, msg, sizeof(msg)); + 0, src, dst, msg, + sizeof(msg)); } static void inseg_to(struct l_timeout *seg_timeout, void *user_data) @@ -1734,7 +1737,7 @@ static void ack_received(struct mesh_net *net, bool timeout, l_info("Resend Seg %d net:%p dst:%x app_idx:%3.3x", i, net, outgoing->remote, outgoing->app_idx); - send_seg(net, outgoing, i); + send_seg(net, outgoing->net_idx, outgoing, i); } l_timeout_remove(outgoing->seg_timeout); @@ -2012,6 +2015,7 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, sar_in->key_aid = key_aid; sar_in->len = len; sar_in->last_seg = 0xff; + sar_in->net_idx = net_idx; sar_in->msg_timeout = l_timeout_create(MSG_TO, inmsg_to, net, NULL); @@ -2208,7 +2212,7 @@ static bool ctl_received(struct mesh_net *net, uint16_t key_id, } if (n) { - mesh_net_transport_send(net, 0, + mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net), rsp_ttl, 0, dst & 0x8000 ? 0 : dst, src, msg, n); @@ -3031,8 +3035,8 @@ bool mesh_net_flush(struct mesh_net *net) return true; } -/* TODO: add net key index */ -static bool send_seg(struct mesh_net *net, struct mesh_sar *msg, uint8_t segO) +static bool send_seg(struct mesh_net *net, uint16_t net_idx, + struct mesh_sar *msg, uint8_t segO) { struct mesh_subnet *subnet; uint8_t seg_len; @@ -3041,7 +3045,6 @@ static bool send_seg(struct mesh_net *net, struct mesh_sar *msg, uint8_t segO) uint8_t packet_len; uint8_t segN = SEG_MAX(msg->segmented, msg->len); uint16_t seg_off = SEG_OFF(segO); - uint32_t key_id = 0; uint32_t seq_num; if (msg->segmented) { @@ -3082,10 +3085,13 @@ static bool send_seg(struct mesh_net *net, struct mesh_sar *msg, uint8_t segO) } print_packet("Clr-Net Tx", packet + 1, packet_len); - subnet = get_primary_subnet(net); - key_id = subnet->net_key_tx; + subnet = l_queue_find(net->subnets, match_key_index, + L_UINT_TO_PTR(net_idx)); + if (!subnet) + return false; - if (!net_key_encrypt(key_id, msg->iv_index, packet + 1, packet_len)) { + if (!net_key_encrypt(subnet->net_key_tx, msg->iv_index, packet + 1, + packet_len)) { l_error("Failed to encode packet"); return false; } @@ -3217,6 +3223,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, payload->szmic = szmic; payload->frnd_cred = frnd_cred; payload->key_aid = key_aid; + payload->net_idx = net_idx; payload->iv_index = mesh_net_get_iv_index(net); payload->seqAuth = seq; payload->segmented = segmented; @@ -3245,11 +3252,11 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, for (i = 0; i < 4; i++) { for (seg = 0; seg <= seg_max && result; seg++) - result = send_seg(net, payload, seg); + result = send_seg(net, net_idx, payload, seg); } } else { for (seg = 0; seg <= seg_max && result; seg++) - result = send_seg(net, payload, seg); + result = send_seg(net, net_idx, payload, seg); } /* Reliable: Cache; Unreliable: Flush*/ @@ -3321,11 +3328,11 @@ void mesh_net_ack_send(struct mesh_net *net, uint32_t key_id, l_free(str); } -/* TODO: add net key index */ void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id, - uint32_t iv_index, uint8_t ttl, - uint32_t seq, uint16_t src, uint16_t dst, - const uint8_t *msg, uint16_t msg_len) + uint16_t net_idx, uint32_t iv_index, + uint8_t ttl, uint32_t seq, uint16_t src, + uint16_t dst, const uint8_t *msg, + uint16_t msg_len) { uint32_t use_seq = seq; uint8_t pkt_len; @@ -3373,7 +3380,10 @@ void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id, } if (!key_id) { - struct mesh_subnet *subnet = get_primary_subnet(net); + struct mesh_subnet *subnet = l_queue_find(net->subnets, + match_key_index, L_UINT_TO_PTR(net_idx)); + if (!subnet) + return; key_id = subnet->net_key_tx; use_seq = mesh_net_next_seq_num(net); @@ -3556,7 +3566,7 @@ void mesh_net_heartbeat_send(struct mesh_net *net) l_put_be16(hb->features, msg + n); n += 2; - mesh_net_transport_send(net, 0, mesh_net_get_iv_index(net), + mesh_net_transport_send(net, 0, 0, mesh_net_get_iv_index(net), hb->pub_ttl, 0, 0, hb->pub_dst, msg, n); } diff --git a/mesh/net.h b/mesh/net.h index df4366b59..60396dbe7 100644 --- a/mesh/net.h +++ b/mesh/net.h @@ -305,9 +305,10 @@ struct l_queue *mesh_net_get_app_keys(struct mesh_net *net); bool mesh_net_flush(struct mesh_net *net); void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id, - uint32_t iv_index, uint8_t ttl, - uint32_t seq, uint16_t src, uint16_t dst, - const uint8_t *msg, uint16_t msg_len); + uint16_t net_idx, uint32_t iv_index, + uint8_t ttl, uint32_t seq, uint16_t src, + uint16_t dst, const uint8_t *msg, + uint16_t msg_len); bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, uint16_t dst, uint8_t key_id, uint16_t net_idx, -- 2.17.1