From patchwork Mon Oct 14 05:22:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junho Lee X-Patchwork-Id: 13834159 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3697F231C9A for ; Mon, 14 Oct 2024 05:22:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883343; cv=none; b=Z9T9O915nAJS86+WULjfmz1bYQteIFXMORjoT1D5/ufr/phKfiVORnUVelBHlO0CZ3YjsU9ua6nZJqNJIS1On8wLkWmNb/oRNhLanmJ+7ChO6hYEHG3x6EeNzIw3bdIInsIkCBJDSY1HmXe8z+/MZREvRk1BG2qHH9Y1B7RArjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883343; c=relaxed/simple; bh=24i+6Z2HTD0KuqF8tSfPaKyEZPp1oS7DUuQzoQgL7DA=; h=Message-ID:Date:From:To:Subject:MIME-Version:Content-Type: Content-Disposition; b=mBGQfMzGvO9+RZiWFH4D2xWfj5fqddQvBVUEN8bdHuPB3CYWkt6l46m3oXjgGRYNYwQ9SSio0FgbZJrYWeB3R1KHe88GMNaULgdZ+ZYy0pLXvpLeIv8Z+94XUr4k7ZEyRjnIKiv4pfeFgkDRFE+UFNq14ybuJ71zFaE35PaqCI0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KnqfpZra; arc=none smtp.client-ip=209.85.215.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KnqfpZra" Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-7ea12e0dc7aso2382546a12.3 for ; Sun, 13 Oct 2024 22:22:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728883338; x=1729488138; darn=vger.kernel.org; h=content-disposition:mime-version:subject:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=5QNZNODlXDO9jMJgBRxoAX6pr9nv05MbyxXWPUi+zyA=; b=KnqfpZraWP4pltSEKCipy5AVNMyxj9ATpzFixSxz4OZeH3AoKgpI0m9X8z6XH2oqa0 cWjAHkcQolVzqUnupzlu6tLmlqr0pSjligBCIW30TJk26vWaZTJrXWW/Lwse3tQZqgm8 KBAMgHbF1hLKguoaQpt7vyuLVh08RX5J8OosKMutbRwZxKywPL0bGOAkIWLwai4LeWoO s1RlR+LyS9VplZZrBmFpQBjYJv5vYxUs5J6dLIVLT/qio1spSQuIRAwyWQyasQyO8k3a 8Nbd1F8rSy4m+zdDZqlw7kivQGOy+HgqiDxEjTsnL4RAP4s+ilIEOcxUSrnWFUtGVSm3 f2XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728883338; x=1729488138; h=content-disposition:mime-version:subject:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5QNZNODlXDO9jMJgBRxoAX6pr9nv05MbyxXWPUi+zyA=; b=JgpFkRSDuULoVDr9MMDEHkkO/l9Qa1cRylww1Ir7MJcrkgXLuh7H4LGe9d+eVOJm5b L/mzrxUdWtnWccYoBPI6/bfzq9Q6VEjX7YTSRcEvT3m0aQGztc+EUM/RBmCPDzIbaT4i gdAhQIgaYVQ8QDUzRNrgjDiJH2rPrW50MTMNkOKrEZtrsvIYZ2D9QDkXikwfrMsCLjm4 GJExijh3JamjX/uwPqByVvi/xPM8J1PNIo6AGEX2FEXpEbcEoME802AWi62MTIKwaebW bRSTc3qA4ydwG8XtdrUM72AUBBclG9zIyhTuIKjIRfMoYxSAT6cUzcypod8lR5i2BdTR YjuA== X-Gm-Message-State: AOJu0Yw9kTqQe1cJ7X1hWScdzJTfbX2Y1SWjLddn5CRMMxnd088wGtVL J49CM8/wGUwfbfevjjOhH5cKvZEztU5+el4k29maPjCHl7rOzL7e9IaPew== X-Google-Smtp-Source: AGHT+IHORMndwFPB6JrdyjveAogp8W3raR82BKNqI0BJyVQ6UPHRgzMTT4MfObyRurt4A9UoHCpn5g== X-Received: by 2002:a05:6a20:ba11:b0:1d7:fca:7b45 with SMTP id adf61e73a8af0-1d8c955c90emr9305782637.8.1728883337628; Sun, 13 Oct 2024 22:22:17 -0700 (PDT) Received: from JUNHO-NEOSTACK. ([221.154.134.3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e62def395sm1535775b3a.69.2024.10.13.22.22.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 22:22:17 -0700 (PDT) Message-ID: <670caa89.a70a0220.5edfe.2cd5@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 14 Oct 2024 14:22:14 +0900 From: Junho Lee To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 1/4] mesh: Implement lower transport layer for segmentation and reassembly Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline These is implementations of segmentation and reassembly, except for the friend feature for LPN nodes, implemented according to Mesh Protocol 1.1 section 3.5.3. Sections 4.2.48 and 4.2.49 of Mesh Protocol 1.1 define the transmission interval, number of retransmissions, retransmission interval, ack transmission, number of ack retransmissions, ack retransmission interval, ack retransmission interval, and discard time for SAR, which is a mutable state, and required a SAR implementation that conforms to the specification. The existing segmentation and reassembly were implemented before the specification was officially released, so they were fixed. - Changed the design of SAR retransmissions. - Changed the design of SAR ack retransmissions. - In the previous implementation, the segments of the SAR were sent to the network layer immediately for every segO. This has been changed so that they are sent one after the other when the timer expires for each segment interval state. - In the previous implementation, the SAR retransmission feature for multicast sent all segments to the network layer a fixed four times. This has been changed to send as many SAR retransmissions for multicast as the number of states. - In the original implementation, the SAR retransmission feaure for unicast was persistent as long as the receiver was receiving additional segments. This has been changed to send as many SAR retransmissions for unicast as the number of states. - In the previous implementation, the SAR transfer feature for the same destination worked as a single thread, so if SAR transfers were duplicated, queuing was used to delay the transfer, but if the upper transport layer required successive SAR transfers, the queue would stack up infinitely. This was solved by discarding existing SAR transmissions when SAR transmissions for the same destination are required. - In the previous implementation, an ack was sent each time a segment was received for a SAR that had already been successfully received. This has been changed to sending an ACK only once within a certain period. --- mesh/mesh.c | 45 ++++ mesh/mesh.h | 2 + mesh/net.c | 672 +++++++++++++++++++++++++++++++++++----------------- 3 files changed, 498 insertions(+), 221 deletions(-) diff --git a/mesh/mesh.c b/mesh/mesh.c index 91cf25175..f89230b6c 100644 --- a/mesh/mesh.c +++ b/mesh/mesh.c @@ -95,6 +95,41 @@ static struct l_queue *pending_queue; static const char *storage_dir; +struct mesh_sar_transmitter { + uint8_t seg_int_step; + uint8_t unicast_rtx_cnt; + uint8_t unicast_rtx_without_prog_cnt; + uint8_t unicast_rtx_int_step; + uint8_t unicast_rtx_int_inc; + uint8_t multicast_rtx_cnt; + uint8_t multicast_rtx_int_step; +}; + +struct mesh_sar_receiver { + uint8_t seg_threshold; + uint8_t ack_delay_inc; + uint8_t ack_rtx_cnt; + uint8_t discard_timeout; + uint8_t receiver_seg_int_step; +}; + +static struct mesh_sar_transmitter mesh_sar_txr = { + .seg_int_step = 5, + .unicast_rtx_cnt = 2, + .unicast_rtx_without_prog_cnt = 2, + .unicast_rtx_int_step = 7, + .unicast_rtx_int_inc = 1, + .multicast_rtx_cnt = 2, + .multicast_rtx_int_step = 8 +}; +static struct mesh_sar_receiver mesh_sar_rxr = { + .seg_threshold = 3, + .ack_delay_inc = 1, + .ack_rtx_cnt = 0, + .discard_timeout = 1, + .receiver_seg_int_step = 5 +}; + /* Forward static decalrations */ static void def_attach(struct l_timeout *timeout, void *user_data); static void def_leave(struct l_timeout *timeout, void *user_data); @@ -203,6 +238,16 @@ uint8_t mesh_get_friend_queue_size(void) return mesh.friend_queue_sz; } +void mesh_get_sar_transmitter(void *sar_txr) +{ + memcpy(sar_txr, &mesh_sar_txr, sizeof(struct mesh_sar_transmitter)); +} + +void mesh_get_sar_receiver(void *sar_rxr) +{ + memcpy(sar_rxr, &mesh_sar_rxr, sizeof(struct mesh_sar_receiver)); +} + static void parse_settings(const char *mesh_conf_fname) { struct l_settings *settings; diff --git a/mesh/mesh.h b/mesh/mesh.h index c30a8d1f0..5d62f35c8 100644 --- a/mesh/mesh.h +++ b/mesh/mesh.h @@ -43,3 +43,5 @@ bool mesh_relay_supported(void); bool mesh_friendship_supported(void); uint16_t mesh_get_crpl(void); uint8_t mesh_get_friend_queue_size(void); +void mesh_get_sar_transmitter(void *sar_txr); +void mesh_get_sar_receiver(void *sar_rxr); diff --git a/mesh/net.c b/mesh/net.c index 05ca48326..48032c64d 100644 --- a/mesh/net.c +++ b/mesh/net.c @@ -30,6 +30,11 @@ #include "mesh/model.h" #include "mesh/appkey.h" #include "mesh/rpl.h" +#include "mesh/mesh.h" + +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#endif #define abs_diff(a, b) ((a) > (b) ? (a) - (b) : (b) - (a)) @@ -44,10 +49,6 @@ #define IV_UPDATE_SEQ_TRIGGER 0x800000 /* Half of Seq-Nums expended */ -#define SEG_TO 2 -#define MSG_TO 60 -#define SAR_DEL 10 - #define DEFAULT_TRANSMIT_COUNT 1 #define DEFAULT_TRANSMIT_INTERVAL 100 @@ -91,15 +92,34 @@ struct mesh_subnet { uint8_t kr_phase; }; +struct mesh_sar_transmitter { + uint8_t seg_int_step; + uint8_t unicast_rtx_cnt; + uint8_t unicast_rtx_without_prog_cnt; + uint8_t unicast_rtx_int_step; + uint8_t unicast_rtx_int_inc; + uint8_t multicast_rtx_cnt; + uint8_t multicast_rtx_int_step; +}; + +struct mesh_sar_receiver { + uint8_t seg_threshold; + uint8_t ack_delay_inc; + uint8_t ack_rtx_cnt; + uint8_t discard_timeout; + uint8_t receiver_seg_int_step; +}; + struct mesh_net { struct mesh_io *io; struct mesh_node *node; struct mesh_prov *prov; struct l_queue *app_keys; + struct mesh_sar_transmitter *sar_txr; + struct mesh_sar_receiver *sar_rxr; unsigned int pkt_id; unsigned int bea_id; unsigned int beacon_id; - unsigned int sar_id_next; bool friend_enable; bool snb_enable; @@ -138,7 +158,6 @@ struct mesh_net { struct l_queue *replay_cache; struct l_queue *sar_in; struct l_queue *sar_out; - struct l_queue *sar_queue; struct l_queue *frnd_msgs; struct l_queue *friends; struct l_queue *negotiations; @@ -152,15 +171,10 @@ struct mesh_msg { }; struct mesh_sar { - unsigned int id; - struct l_timeout *seg_timeout; - struct l_timeout *msg_timeout; uint32_t flags; - uint32_t last_nak; uint32_t iv_index; uint32_t seqAuth; uint16_t seqZero; - uint16_t app_idx; uint16_t net_idx; uint16_t src; uint16_t remote; @@ -169,13 +183,32 @@ struct mesh_sar { bool segmented; bool frnd; bool frnd_cred; - bool delete; uint8_t ttl; - uint8_t last_seg; uint8_t key_aid; uint8_t buf[4]; /* Large enough for ACK-Flags and MIC */ }; +struct mesh_sar_tx { + struct mesh_sar *sar; + struct l_timeout *seg_timeout; + uint16_t retrans_ms; + uint8_t int_ms; + uint8_t attempt_left; + uint8_t attempt_left_no_progress; + uint8_t segO; + uint8_t segN; + bool ack_received; +}; + +struct mesh_sar_rx { + struct mesh_sar *sar; + struct l_timeout *ack_timeout; + struct l_timeout *msg_timeout; + uint16_t ack_int_ms; + uint8_t ack_attempt_left; + uint8_t discard_to; +}; + struct mesh_destination { uint16_t dst; uint16_t ref_cnt; @@ -225,6 +258,65 @@ struct net_beacon_data { static struct l_queue *fast_cache; static struct l_queue *nets; +static uint8_t sar_tx_seg_int_ms(struct mesh_sar_transmitter *txr) +{ + return (txr->seg_int_step + 1) * 10; +} + +static uint8_t sar_tx_retrans_cnt(struct mesh_sar_transmitter *txr, + bool is_unicast, bool no_progress) +{ + if (is_unicast) { + return (no_progress ? + txr->unicast_rtx_without_prog_cnt : + txr->unicast_rtx_cnt); + } else { + return txr->multicast_rtx_cnt; + } +} + +/* Maximum retransmission interval is 50800 milliseconds. */ +static uint16_t sar_tx_retrans_timeout_ms(struct mesh_sar_transmitter *txr, + bool is_unicast, int ttl) +{ + if (is_unicast) { + return (txr->unicast_rtx_int_step + 1 + ((ttl > 0) ? + ((txr->unicast_rtx_int_inc + 1) * (ttl - 1)) : + 0)) * 25; + } else { + return (txr->multicast_rtx_int_step + 1) * 25; + } +} + +static uint8_t sar_rx_seg_threshold(struct mesh_sar_receiver *rxr) +{ + return rxr->seg_threshold; +} + +/* Maximum ack retransmission interval is 1360 milliseconds. */ +static uint16_t sar_rx_ack_timeout_ms(struct mesh_sar_receiver *rxr, int segN) +{ + return MIN(segN, rxr->ack_delay_inc * 2 + 3) * + (rxr->receiver_seg_int_step + 1) * 10 / 2; +} + +/* Maximum retransmission delay is 1360 milliseconds. */ +static uint16_t sar_rx_delay_ack_timeout_ms(struct mesh_sar_receiver *rxr) +{ + return (rxr->ack_delay_inc * 2 + 3) * + (rxr->receiver_seg_int_step + 1) * 10 / 2; +} + +static uint8_t sar_rx_discard_timeout(struct mesh_sar_receiver *rxr) +{ + return (rxr->discard_timeout + 1) * 5; +} + +static uint8_t sar_rx_ack_retrans_count(struct mesh_sar_receiver *rxr) +{ + return rxr->ack_rtx_cnt; +} + static void net_rx(void *net_ptr, void *user_data); static inline struct mesh_subnet *get_primary_subnet(struct mesh_net *net) @@ -521,11 +613,54 @@ static void mesh_sar_free(void *data) if (!sar) return; - l_timeout_remove(sar->seg_timeout); - l_timeout_remove(sar->msg_timeout); l_free(sar); } +static struct mesh_sar_tx *mesh_sar_tx_new(void) +{ + size_t size = sizeof(struct mesh_sar_tx); + struct mesh_sar_tx *sar_tx; + + sar_tx = l_malloc(size); + memset(sar_tx, 0, size); + return sar_tx; +} + +static void mesh_sar_tx_free(void *data) +{ + struct mesh_sar_tx *sar_tx = data; + + if (!sar_tx) + return; + + mesh_sar_free(sar_tx->sar); + l_timeout_remove(sar_tx->seg_timeout); + l_free(sar_tx); +} + +static struct mesh_sar_rx *mesh_sar_rx_new(void) +{ + size_t size = sizeof(struct mesh_sar_rx); + struct mesh_sar_rx *sar_rx; + + sar_rx = l_malloc(size); + memset(sar_rx, 0, size); + return sar_rx; +} + +static void mesh_sar_rx_free(void *data) +{ + struct mesh_sar_rx *sar_rx = data; + + if (!sar_rx) + return; + + mesh_sar_free(sar_rx->sar); + l_timeout_remove(sar_rx->ack_timeout); + l_timeout_remove(sar_rx->msg_timeout); + l_free(sar_rx); +} + static void subnet_free(void *data) { struct mesh_subnet *subnet = data; @@ -634,6 +769,11 @@ struct mesh_net *mesh_net_new(struct mesh_node *node) net = l_new(struct mesh_net, 1); + net->sar_txr = l_new(struct mesh_sar_transmitter, 1); + mesh_get_sar_transmitter(net->sar_txr); + net->sar_rxr = l_new(struct mesh_sar_receiver, 1); + mesh_get_sar_receiver(net->sar_rxr); + net->node = node; net->seq_num = DEFAULT_SEQUENCE_NUMBER; net->default_ttl = TTL_MASK; @@ -645,7 +785,6 @@ struct mesh_net *mesh_net_new(struct mesh_node *node) net->msg_cache = l_queue_new(); net->sar_in = l_queue_new(); net->sar_out = l_queue_new(); - net->sar_queue = l_queue_new(); net->frnd_msgs = l_queue_new(); net->destinations = l_queue_new(); net->app_keys = l_queue_new(); @@ -667,12 +806,14 @@ void mesh_net_free(void *user_data) if (!net) return; + l_free(net->sar_txr); + l_free(net->sar_rxr); + l_queue_destroy(net->subnets, subnet_free); l_queue_destroy(net->msg_cache, l_free); l_queue_destroy(net->replay_cache, l_free); - l_queue_destroy(net->sar_in, mesh_sar_free); - l_queue_destroy(net->sar_out, mesh_sar_free); - l_queue_destroy(net->sar_queue, mesh_sar_free); + l_queue_destroy(net->sar_in, mesh_sar_rx_free); + l_queue_destroy(net->sar_out, mesh_sar_tx_free); l_queue_destroy(net->frnd_msgs, l_free); l_queue_destroy(net->friends, mesh_friend_free); l_queue_destroy(net->negotiations, mesh_friend_free); @@ -1080,36 +1221,52 @@ static bool msg_in_cache(struct mesh_net *net, uint16_t src, uint32_t seq, return false; } -static bool match_sar_seq0(const void *a, const void *b) +static bool match_sar_tx_seq0(const void *a, const void *b) { - const struct mesh_sar *sar = a; + const struct mesh_sar_tx *sar_tx = a; uint16_t seqZero = L_PTR_TO_UINT(b); - return sar->seqZero == seqZero; + return sar_tx->sar->seqZero == seqZero; +} + +static bool match_sar_tx_remote(const void *a, const void *b) +{ + const struct mesh_sar_tx *sar_tx = a; + uint16_t remote = L_PTR_TO_UINT(b); + + return sar_tx->sar->remote == remote; } -static bool match_sar_remote(const void *a, const void *b) +static bool match_sar_rx_remote(const void *a, const void *b) { - const struct mesh_sar *sar = a; + const struct mesh_sar_rx *sar_rx = a; uint16_t remote = L_PTR_TO_UINT(b); - return sar->remote == remote; + return sar_rx->sar->remote == remote; } static bool match_msg_timeout(const void *a, const void *b) { - const struct mesh_sar *sar = a; + const struct mesh_sar_rx *sar_rx = a; const struct l_timeout *msg_timeout = b; - return sar->msg_timeout == msg_timeout; + return sar_rx->msg_timeout == msg_timeout; +} + +static bool match_ack_timeout(const void *a, const void *b) +{ + const struct mesh_sar_rx *sar_rx = a; + const struct l_timeout *ack_timeout = b; + + return sar_rx->ack_timeout == ack_timeout; } static bool match_seg_timeout(const void *a, const void *b) { - const struct mesh_sar *sar = a; + const struct mesh_sar_tx *sar_tx = a; const struct l_timeout *seg_timeout = b; - return sar->seg_timeout == seg_timeout; + return sar_tx->seg_timeout == seg_timeout; } static bool match_dest_dst(const void *a, const void *b) @@ -1501,156 +1658,193 @@ static void send_net_ack(struct mesh_net *net, struct mesh_sar *sar, sizeof(msg)); } -static void inseg_to(struct l_timeout *seg_timeout, void *user_data) +static void delay_seg_ack_to(struct l_timeout *ack_timeout, void *user_data) { struct mesh_net *net = user_data; - struct mesh_sar *sar = l_queue_find(net->sar_in, - match_seg_timeout, seg_timeout); + struct mesh_sar_rx *sar_rx = l_queue_find(net->sar_in, + match_ack_timeout, ack_timeout); - l_timeout_remove(seg_timeout); - if (!sar) + l_timeout_remove(ack_timeout); + if (!sar_rx) return; - /* Send NAK */ - l_debug("Timeout %p %3.3x", sar, sar->app_idx); - send_net_ack(net, sar, sar->flags); - - sar->seg_timeout = l_timeout_create(SEG_TO, inseg_to, net, NULL); + sar_rx->ack_timeout = NULL; } -static void inmsg_to(struct l_timeout *msg_timeout, void *user_data) +static void seg_ack_to(struct l_timeout *ack_timeout, void *user_data) { struct mesh_net *net = user_data; - struct mesh_sar *sar = l_queue_find(net->sar_in, - match_msg_timeout, msg_timeout); + struct mesh_sar_rx *sar_rx = l_queue_find(net->sar_in, + match_ack_timeout, ack_timeout); - if (!sar) { - l_timeout_remove(msg_timeout); + l_timeout_remove(ack_timeout); + if (!sar_rx) return; - } - if (!sar->delete) { - /* - * Incomplete timer expired, cancel SAR and start - * delete timer - */ - l_timeout_remove(sar->seg_timeout); - sar->seg_timeout = NULL; - sar->delete = true; - l_timeout_modify(sar->msg_timeout, SAR_DEL); - return; - } + sar_rx->ack_timeout = NULL; + + send_net_ack(net, sar_rx->sar, sar_rx->sar->flags); - l_queue_remove(net->sar_in, sar); - mesh_sar_free(sar); + if (sar_rx->ack_attempt_left) { + sar_rx->ack_attempt_left--; + sar_rx->ack_timeout = l_timeout_create(sar_rx->ack_int_ms, + seg_ack_to, net, NULL); + } } -static void outmsg_to(struct l_timeout *msg_timeout, void *user_data) +static void seg_rx_completed_to(struct l_timeout *msg_timeout, void *user_data) { struct mesh_net *net = user_data; - struct mesh_sar *sar = l_queue_remove_if(net->sar_out, + struct mesh_sar_rx *sar_rx = l_queue_find(net->sar_in, match_msg_timeout, msg_timeout); l_timeout_remove(msg_timeout); - if (!sar) + if (!sar_rx) return; - sar->msg_timeout = NULL; - mesh_sar_free(sar); -} + sar_rx->msg_timeout = NULL; -static void outseg_to(struct l_timeout *seg_timeout, void *user_data); + l_queue_remove(net->sar_in, sar_rx); + mesh_sar_rx_free(sar_rx); +} -static void send_queued_sar(struct mesh_net *net, uint16_t dst) +static void seg_rx_discard_to(struct l_timeout *msg_timeout, void *user_data) { - struct mesh_sar *sar = l_queue_remove_if(net->sar_queue, - match_sar_remote, L_UINT_TO_PTR(dst)); + struct mesh_net *net = user_data; + struct mesh_sar_rx *sar_rx = l_queue_find(net->sar_in, + match_msg_timeout, msg_timeout); - if (!sar) + l_timeout_remove(msg_timeout); + if (!sar_rx) return; - /* Out to current outgoing, and immediate expire Seg TO */ - l_queue_push_head(net->sar_out, sar); - sar->seg_timeout = NULL; - sar->msg_timeout = l_timeout_create(MSG_TO, outmsg_to, net, NULL); - outseg_to(NULL, net); + sar_rx->msg_timeout = NULL; + + l_debug("Discard pending SAR for %04x", sar_rx->sar->seqZero); + l_queue_remove(net->sar_in, sar_rx); + mesh_sar_rx_free(sar_rx); } -static void ack_received(struct mesh_net *net, bool timeout, - uint16_t src, uint16_t dst, - uint16_t seq0, uint32_t ack_flag) +static void ack_rxed(struct mesh_net *net, uint16_t src, uint16_t dst, + uint16_t seq0, uint32_t ack_flag) { - struct mesh_sar *outgoing; - uint32_t seg_flag = 0x00000001; - uint32_t ack_copy = ack_flag; - uint16_t i; + struct mesh_sar_tx *sar_tx; + struct mesh_sar *sar; + uint32_t flags_copy; - l_debug("ACK Rxed (%x) (to:%d): %8.8x", seq0, timeout, ack_flag); + l_debug("ACK Rxed (%x): %8.8x", seq0, ack_flag); - outgoing = l_queue_find(net->sar_out, match_sar_seq0, + sar_tx = l_queue_find(net->sar_out, match_sar_tx_seq0, L_UINT_TO_PTR(seq0)); - if (!outgoing) { + if (!sar_tx) { l_debug("Not Found: %4.4x", seq0); return; } + sar = sar_tx->sar; + /* * TODO -- If we receive from different * SRC than we are sending to, make sure the OBO flag is set */ - if ((!timeout && !ack_flag) || - (outgoing->flags & ack_flag) == outgoing->flags) { - l_debug("ob_sar_removal (%x)", outgoing->flags); + if (!ack_flag) { + l_debug("Cancel sending segments. (%x)", sar->flags); + l_queue_remove(net->sar_out, sar_tx); + mesh_sar_tx_free(sar_tx); + return; + } - /* Note: ack_flags == 0x00000000 is a remote Cancel request */ + flags_copy = sar->flags & ~ack_flag; - l_queue_remove(net->sar_out, outgoing); - send_queued_sar(net, outgoing->remote); - mesh_sar_free(outgoing); + if (flags_copy == sar->flags) { + /* Already received ack. */ + return; + } + sar_tx->ack_received = true; + sar->flags = flags_copy; + + if (!flags_copy) { + /* Complete SAR transmission. Receiver has all segs. */ + l_queue_remove(net->sar_out, sar_tx); + mesh_sar_tx_free(sar_tx); return; } +} + +static void send_next_seg_to(struct l_timeout *seg_timeout, void *user_data) +{ + struct mesh_net *net = user_data; + struct mesh_sar_tx *sar_tx = l_queue_find(net->sar_out, + match_seg_timeout, seg_timeout); + uint8_t interval_ms = 0; - outgoing->last_nak |= ack_flag; + l_timeout_remove(seg_timeout); + if (!sar_tx) + return; - ack_copy &= outgoing->flags; + sar_tx->seg_timeout = NULL; - for (i = 0; i <= SEG_MAX(true, outgoing->len); i++, seg_flag <<= 1) { - if (seg_flag & ack_flag) { - l_debug("Skipping Seg %d of %d", - i, SEG_MAX(true, outgoing->len)); + /* Complete transmission to unicast address. Receiver has all segs. */ + if (!sar_tx->sar->flags) + goto free; + + while (sar_tx->segO <= sar_tx->segN) { + if (!((1 << sar_tx->segO) & sar_tx->sar->flags)) { + /* Skip received segments. */ + sar_tx->segO++; continue; } - ack_copy |= seg_flag; - - l_debug("Resend Seg %d net:%p dst:%x app_idx:%3.3x", - i, net, outgoing->remote, outgoing->app_idx); + if (!send_seg(net, net->tx_cnt, net->tx_interval, sar_tx->sar, + sar_tx->segO)) { + l_error("Failed to send SAR"); + goto free; + } - send_seg(net, net->tx_cnt, net->tx_interval, outgoing, i); + sar_tx->segO++; + interval_ms = sar_tx->int_ms; + break; } - l_timeout_remove(outgoing->seg_timeout); - outgoing->seg_timeout = l_timeout_create(SEG_TO, outseg_to, net, NULL); -} + if (sar_tx->segO > sar_tx->segN) { + if (!sar_tx->attempt_left) { + if (IS_UNICAST(sar_tx->sar->remote)) { + l_warn("Ran out of SAR retransmit attempts"); + goto free; + } else { + /* Complete transmission to multicast address.*/ + goto free; + } + } else if (!sar_tx->attempt_left_no_progress) { + l_warn("Ran out of SAR retransmit without progress attempts"); + goto free; + } -static void outseg_to(struct l_timeout *seg_timeout, void *user_data) -{ - struct mesh_net *net = user_data; - struct mesh_sar *sar = l_queue_find(net->sar_out, - match_seg_timeout, seg_timeout); + sar_tx->segO = 0; + sar_tx->attempt_left--; + if (!sar_tx->ack_received) + sar_tx->attempt_left_no_progress--; + else + sar_tx->attempt_left_no_progress = sar_tx_retrans_cnt( + net->sar_txr, + true, true); + sar_tx->ack_received = false; + interval_ms = sar_tx->retrans_ms; + } - l_timeout_remove(seg_timeout); - if (!sar) - return; + sar_tx->seg_timeout = l_timeout_create_ms(interval_ms, send_next_seg_to, + net, NULL); - sar->seg_timeout = NULL; +out: + return; - /* Re-Send missing segments by faking NACK */ - ack_received(net, true, sar->remote, sar->src, - sar->seqZero, sar->last_nak); +free: + l_queue_remove(net->sar_out, sar_tx); + mesh_sar_tx_free(sar_tx); + goto out; } static bool match_replay_cache(const void *a, const void *b) @@ -1956,130 +2150,165 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, uint8_t segO, uint8_t segN, const uint8_t *data, uint8_t size) { - struct mesh_sar *sar_in = NULL; + struct mesh_sar_rx *sar_in = NULL; + struct mesh_sar *sar = NULL; uint16_t seg_off = 0; - uint32_t expected, this_seg_flag, largest, seqAuth; - bool reset_seg_to = true; + uint32_t expected, this_seg_flag, seqAuth; /* * DST could receive additional Segments after * completing due to a lost ACK, so re-ACK and discard */ - sar_in = l_queue_find(net->sar_in, match_sar_remote, + sar_in = l_queue_find(net->sar_in, match_sar_rx_remote, L_UINT_TO_PTR(src)); /* Discard *old* incoming-SAR-in-progress if this segment newer */ seqAuth = seq_auth(seq, seqZero); - if (sar_in && (sar_in->seqAuth != seqAuth || - sar_in->iv_index != iv_index)) { + if (sar_in && (sar_in->sar->seqAuth != seqAuth || + sar_in->sar->iv_index != iv_index)) { bool newer; - if (iv_index > sar_in->iv_index) + if (iv_index > sar_in->sar->iv_index) newer = true; - else if (iv_index == sar_in->iv_index) - newer = seqAuth > sar_in->seqAuth; + else if (iv_index == sar_in->sar->iv_index) + newer = seqAuth > sar_in->sar->seqAuth; else newer = false; if (newer) { /* Cancel Old, start New */ + l_debug("Discard SAR duplicated from src: 0x%04x", src); l_queue_remove(net->sar_in, sar_in); - mesh_sar_free(sar_in); + mesh_sar_rx_free(sar_in); sar_in = NULL; } else /* Ignore Old */ + l_debug("Ignore old seqAuth"); return false; } expected = 0xffffffff >> (31 - segN); if (sar_in) { + sar = sar_in->sar; l_debug("RXed (old: %04x %06x size:%d) %d of %d", seqZero, seq, size, segO, segN); /* Sanity Check--> certain things must match */ - if (SEG_MAX(true, sar_in->len) != segN || - sar_in->key_aid != key_aid) + if (SEG_MAX(true, sar->len) != segN || + sar->key_aid != key_aid) return false; - if (sar_in->flags == expected) { - /* Re-Send ACK for full msg */ - send_net_ack(net, sar_in, expected); + if (sar->flags == expected) { + /* + * According to MshPRTv1.1: 3.5.3.4, if the destination + * is a unicast address and a segment is received for a + * completed message, it sends one acknowledgment within + * a period of: + * [ack delay increment * seg reception interval] + */ + l_debug("Got segment for already complete msg"); + if (IS_UNICAST(dst) && sar_in->ack_timeout == NULL) { + send_net_ack(net, sar, expected); + sar_in->ack_timeout = l_timeout_create_ms( + sar_rx_delay_ack_timeout_ms( + net->sar_rxr), + delay_seg_ack_to, net, NULL); + } return true; - } else if (sar_in->delete) - /* Ignore cancelled */ - return false; + } } else { uint16_t len = MAX_SEG_TO_LEN(segN); l_debug("RXed (new: %04x %06x size: %d len: %d) %d of %d", seqZero, seq, size, len, segO, segN); l_debug("Queue Size: %d", l_queue_length(net->sar_in)); - sar_in = mesh_sar_new(len); - sar_in->seqAuth = seqAuth; - sar_in->iv_index = iv_index; - sar_in->src = dst; - sar_in->remote = src; - sar_in->seqZero = seqZero; - 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); - - l_debug("First Seg %4.4x", sar_in->flags); + sar = mesh_sar_new(len); + sar->seqAuth = seqAuth; + sar->iv_index = iv_index; + sar->src = dst; + sar->remote = src; + sar->seqZero = seqZero; + sar->key_aid = key_aid; + sar->len = len; + sar->net_idx = net_idx; + + sar_in = mesh_sar_rx_new(); + sar_in->sar = sar; + + if (IS_UNICAST(dst)) { + /* + * According to MshPRTv1.1: 3.5.3.4, if the destination + * is a unicast address, it sends the acks for received + * segments, and additionally retransmits the acks when + * segN exceeds the threshold. + */ + sar_in->ack_int_ms = sar_rx_ack_timeout_ms(net->sar_rxr, + segN); + + if (segN > sar_rx_seg_threshold(net->sar_rxr)) { + sar_in->ack_attempt_left = + sar_rx_ack_retrans_count(net->sar_rxr); + } else { + sar_in->ack_attempt_left = 0; + } + } + + sar_in->discard_to = sar_rx_discard_timeout(net->sar_rxr); + + l_debug("First Seg %4.4x", sar->flags); l_queue_push_head(net->sar_in, sar_in); } - seg_off = segO * MAX_SEG_LEN; - memcpy(sar_in->buf + seg_off, data, size); this_seg_flag = 0x00000001 << segO; - /* Don't reset Seg TO or NAK if we already have this seg */ - if (this_seg_flag & sar_in->flags) - reset_seg_to = false; + /* Ignore this seg if it already exists.*/ + if (this_seg_flag & sar->flags) + return false; + + l_timeout_remove(sar_in->ack_timeout); + sar_in->ack_timeout = NULL; + l_timeout_remove(sar_in->msg_timeout); + sar_in->msg_timeout = NULL; - sar_in->flags |= this_seg_flag; - sar_in->ttl = ttl; + seg_off = segO * MAX_SEG_LEN; + memcpy(sar->buf + seg_off, data, size); + sar->flags |= this_seg_flag; + sar->ttl = ttl; /* Msg length only definitive on last segment */ if (segO == segN) - sar_in->len = segN * MAX_SEG_LEN + size; + sar->len = segN * MAX_SEG_LEN + size; + + /* Send ACK only if DST is unicast address. */ + if (IS_UNICAST(dst)) { + sar_in->ack_timeout = l_timeout_create_ms(sar_in->ack_int_ms, + seg_ack_to, + net, NULL); + } - if (sar_in->flags == expected) { + if (sar->flags == expected) { /* Got it all */ - send_net_ack(net, sar_in, expected); + send_net_ack(net, sar, expected); msg_rxed(net, frnd, iv_index, ttl, seq, net_idx, - sar_in->remote, dst, key_aid, true, szmic, - sar_in->seqZero, sar_in->buf, sar_in->len); + sar->remote, dst, key_aid, true, szmic, + sar->seqZero, sar->buf, sar->len); - /* Kill Inter-Seg timeout */ - l_timeout_remove(sar_in->seg_timeout); - sar_in->seg_timeout = NULL; - - /* Start delete timer */ - sar_in->delete = true; - l_timeout_modify(sar_in->msg_timeout, SAR_DEL); + /* + * Delay SAR removal to be able to acknowledge a transaction + * when repeated segments are received. + */ + sar_in->msg_timeout = l_timeout_create(sar_in->discard_to, + seg_rx_completed_to, + net, NULL); return true; } - if (reset_seg_to) { - /* Restart Inter-Seg Timeout */ - l_timeout_remove(sar_in->seg_timeout); - - /* if this is the largest outstanding segment, send NAK now */ - largest = (0xffffffff << segO) & expected; - if ((largest & sar_in->flags) == largest) - send_net_ack(net, sar_in, sar_in->flags); - - sar_in->seg_timeout = l_timeout_create(SEG_TO, - inseg_to, net, NULL); - } else - largest = 0; + sar_in->msg_timeout = l_timeout_create(sar_in->discard_to, + seg_rx_discard_to, + net, NULL); - l_debug("NAK: %d expected:%08x largest:%08x flags:%08x", - reset_seg_to, expected, largest, sar_in->flags); + l_debug("expected:%08x flags:%08x", expected, sar->flags); return false; } @@ -2393,8 +2622,7 @@ static enum _relay_advice packet_received(void *user_data, friend_ack_rxed(net, iv_index, net_seq, net_src, net_dst, msg); else - ack_received(net, false, - net_src, net_dst, + ack_rxed(net, net_src, net_dst, net_seqZero, l_get_be32(msg + 3)); } else { @@ -2565,8 +2793,7 @@ static void iv_upd_to(struct l_timeout *upd_timeout, void *user_data) switch (net->iv_upd_state) { case IV_UPD_UPDATING: - if (l_queue_length(net->sar_out) || - l_queue_length(net->sar_queue)) { + if (l_queue_length(net->sar_out)) { l_debug("don't leave IV Update until sar_out empty"); l_timeout_modify(net->iv_update_timeout, 10); break; @@ -3194,8 +3421,6 @@ static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval, send_msg_pkt(net, cnt, interval, packet, packet_len + 1); - msg->last_seg = segO; - return true; } @@ -3255,7 +3480,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, bool szmic, const void *msg, uint16_t msg_len) { struct mesh_sar *payload = NULL; - uint8_t seg, seg_max; + uint8_t seg_max; bool result; if (!net || msg_len > 384) @@ -3311,47 +3536,52 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, payload->segmented = segmented; if (segmented) { + struct mesh_sar_tx *drop_sar_tx; payload->flags = 0xffffffff >> (31 - seg_max); payload->seqZero = seq & SEQ_ZERO_MASK; - payload->id = ++net->sar_id_next; /* Single thread SAR messages to same Unicast DST */ - if (l_queue_find(net->sar_out, match_sar_remote, - L_UINT_TO_PTR(dst))) { - /* Delay sending Outbound SAR unless prior - * SAR to same DST has completed */ - - l_debug("OB-Queued SeqZero: %4.4x", payload->seqZero); - l_queue_push_tail(net->sar_queue, payload); - return true; + drop_sar_tx = l_queue_find(net->sar_out, match_sar_tx_remote, + L_UINT_TO_PTR(dst)); + if (drop_sar_tx) { + /* Cancel incomplete prior SAR on the same dst */ + l_debug("Cancel incompleted SAR: SeqZero %4.4x", + drop_sar_tx->sar->seqZero); + l_queue_remove(net->sar_out, drop_sar_tx); + mesh_sar_tx_free(drop_sar_tx); } } - result = true; - - if (!IS_UNICAST(dst) && segmented) { - int i; + result = send_seg(net, cnt, interval, payload, 0); - for (i = 0; i < 4; i++) { - for (seg = 0; seg <= seg_max && result; seg++) - result = send_seg(net, cnt, interval, payload, - seg); - } + /* + * Set the timeout to send the next seg or retransmit if the payload is + * segmented. Flush if it is not segmented or if the transmission + * failed. + */ + if (result && segmented) { + struct mesh_sar_tx *sar_tx = mesh_sar_tx_new(); + bool is_unicast = IS_UNICAST(dst); + + sar_tx->ack_received = false; + sar_tx->int_ms = sar_tx_seg_int_ms(net->sar_txr); + sar_tx->attempt_left = sar_tx_retrans_cnt(net->sar_txr, + is_unicast, false); + sar_tx->attempt_left_no_progress = sar_tx_retrans_cnt( + net->sar_txr, + is_unicast, true); + sar_tx->retrans_ms = sar_tx_retrans_timeout_ms(net->sar_txr, + is_unicast, + ttl); + sar_tx->sar = payload; + l_queue_push_head(net->sar_out, sar_tx); + sar_tx->seg_timeout = l_timeout_create_ms(sar_tx->int_ms, + send_next_seg_to, net, NULL); + sar_tx->segO = 1; /* The 0th seg is already sent. */ + sar_tx->segN = seg_max; } else { - for (seg = 0; seg <= seg_max && result; seg++) - result = send_seg(net, cnt, interval, payload, seg); - } - - /* Reliable: Cache; Unreliable: Flush*/ - if (result && segmented && IS_UNICAST(dst)) { - l_queue_push_head(net->sar_out, payload); - payload->seg_timeout = - l_timeout_create(SEG_TO, outseg_to, net, NULL); - payload->msg_timeout = - l_timeout_create(MSG_TO, outmsg_to, net, NULL); - payload->id = ++net->sar_id_next; - } else mesh_sar_free(payload); + } return result; } From patchwork Mon Oct 14 05:22:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junho Lee X-Patchwork-Id: 13834160 Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39C00231C9A for ; Mon, 14 Oct 2024 05:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883361; cv=none; b=uicmfQcfHOM/2epIDGlq7gY93fXA4L/iEkwdNEpEIw+ocCGncKjf/oIx0VR/2dyiiM2FnzhU+w4+5wO+SWlhs9BvwlJ1JfWYsxwZ34J0KE8lsVc3GJUndEIjW5QQxGkJ6PBeUYPCF6S+sAp/1S5fWIGUtAU4YxjILAtRuDwsyKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883361; c=relaxed/simple; bh=faK6XWq8mtyX5B6ULPT7a+PZyqj05h2pVIOwz15J7RE=; h=Message-ID:Date:From:To:Subject:MIME-Version:Content-Type: Content-Disposition; b=FOep5DVb9iJQ+N91oMABQ1mSE25dKRUa8//jZg7i4w/4lVMnYYtHu872IRwnoe4gVyxJcT8fKe8ViVnJNay3An5szXR6Fd3Q47a50j6WNATgkogRLr7d/9keQSWlq1kfWaG1KHwaBKr6MLnfweoljoRZEBy7gcTxWiwRYHAG1uE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EJwvo/xa; arc=none smtp.client-ip=209.85.210.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EJwvo/xa" Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-7170d0edab6so1878825a34.0 for ; Sun, 13 Oct 2024 22:22:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728883359; x=1729488159; darn=vger.kernel.org; h=content-disposition:mime-version:subject:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=tRx3IC4NYZcXkgJI8J3F4oa+SBfBLkFGe9LUx01FzGc=; b=EJwvo/xa2BEm89hvltLY1ohqk+GTbTTFCag5AHCyQx/EjHLWdnt1c3xAb+h5wDs/TQ BwIhFI6vMlBl5qdp77FNBk27xcFrDfhRZ5wKNDqSLLQP3brcrlsOsHr3nmc/D1wB08sa 0UjRCXHNDMmaRq2jwQAXMM/+7ioFsEMrhLPr49CPR3GKZgikqhiYyxSaaUYqVmn2tFPN 9I1t3KhAFChnlDuKA6g0O6/xO2HQAKiMXD6kJv4ImRziR9bnkWPE81XkQg3tlbmXxgkI JICLkcby7m1JT52oj1vNtrCP/I3DvkQf8BNS1TJzF7io8NFQWDTIYOxAhJRAzZxCtYND Dsgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728883359; x=1729488159; h=content-disposition:mime-version:subject:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tRx3IC4NYZcXkgJI8J3F4oa+SBfBLkFGe9LUx01FzGc=; b=JapyOAyuS9aFL3dzLz4oTWF4rp8K6JF2NgRGcjKo08oYfm4sjxUgRPw1hS1IR4Lt6F /pkzFGB+7+aD55gmz5pVran7gc81ZJHLJQjtvHzIDNHK0xBOEfDHC/jqaMk0M6npi4We jnpT5z6xb9xXBCS7z7TffaluICvgkKtyBG/pVFLpW0+HJrIcCVoUaJTBJj+PDUstmQMd VUVn+RGVa71/9MT/9c1cTIe7Blk3qQ62dta5OlxPnzjkUssNduBXurIpDZ9rzq2sAFrd 92bSmvL8UEElr9JXpxNyuagemFRUdtERlIq2odYPcESnGSRZ9UuwQaFYOBFo9lAg8119 t77w== X-Gm-Message-State: AOJu0YwDDC4n1371Fd+HQv0lg+2nEaOn6VRE4vu3SvaSlOdOzxM3aBu2 qpi6EXQ6VOAGw12vNQRe25A+OhnOByQKTLDB+1vBwRJ5ZfcgxHsq+sAJjw== X-Google-Smtp-Source: AGHT+IGV8ukpe4nxWiL7WIMGbWs7d4asRCQBB+XSFCUVWtyTPjZdqedzGt+g8+AoWWcOKlaHpOJ7sQ== X-Received: by 2002:a05:6359:4c9b:b0:1ba:5118:ebee with SMTP id e5c5f4694b2df-1c32ba41305mr271471155d.8.1728883358808; Sun, 13 Oct 2024 22:22:38 -0700 (PDT) Received: from JUNHO-NEOSTACK. ([221.154.134.3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e56e0d9f9sm2921660b3a.79.2024.10.13.22.22.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 22:22:38 -0700 (PDT) Message-ID: <670caa9e.050a0220.1d90eb.58b8@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 14 Oct 2024 14:22:36 +0900 From: Junho Lee To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 2/4] mesh: Add mesh conf for SAR Transmitter and Receiver Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline This allows you to set the SAR Transitter state and SAR Receiver state in the mesh conf file. --- mesh/mesh-main.conf | 107 ++++++++++++++++++++++++++++++++++++++++++++ mesh/mesh.c | 80 ++++++++++++++++++++++++++++++--- 2 files changed, 181 insertions(+), 6 deletions(-) diff --git a/mesh/mesh-main.conf b/mesh/mesh-main.conf index aca9e6fa5..01f4c3d23 100644 --- a/mesh/mesh-main.conf +++ b/mesh/mesh-main.conf @@ -41,3 +41,110 @@ # Setting this value to zero means there's no timeout. # Defaults to 60. #ProvTimeout = 60 + + +[SARTransmitter] + +# Transmission interval step between segments of a message. +# Interval is measured in milliseconds and calculated using the following +# formula: +# +# (SegIntervalStep + 1) * 10 ms. +# +# Valid range 0-15. +# Defaults to 5. +#SegIntervalStep = 5 + +# Maximum number of retransmissions of segments to a unicast destination. +# Valid range 0-15. +# Defaults to 2. +#UnicastRetransCount = 2 + +# Maximum number of retransmissions of segments to a unicast destination when no +# acknowledgment is newly received during the retransmission interval. +# This value sould be set to greater than AckRetransCount on a peer node. +# Valid range 0-15. +# Defaults to 2. +#UnicastRetransWithoutProgressCount = 2 + +# Retransmission interval step between segments of a meesage to a unicast +# destination. +# Interval is measured in milliseconds and calculated using the following +# formula: +# +# (UnicastRetransIntervalStep + 1) * 25 ms +# +# Valid range 0-15. +# Defaults to 7. +#UnicastRetransIntervalStep = 7 + +# Interval step between segments of a message to a unicast destination that +# increases proportionally to (ttl - 1) when ttl is over 0. +# Increment is measured in milliseconds and calculated using the following +# formula: +# +# (UnicastRetransIntervalIncrement + 1) * 25 ms +# +# Valid range 0-15. +# Defaults to 1. +#UnicastRetransIntervalIncrement = 1 + +# Maximum number of retransmissions of segments to a multicast destination. +# Valid range 0-15. +# Defaults to 2. +#MulticastRetransCount = 2 + +# Retransmission interval step between segments of a meesage to a multicast +# destination. +# Interval is measured in milliseconds and calculated using the following +# formula: +# +# (MulticastRetransIntervalStep + 1) * 25 ms +# +# Valid range 0-15. +# Defaults to 8. +#MulticastRetransIntervalStep = 8 + +[SARReceiver] + +# Threshold number of segments in a message to retransmit acknowledgment +# messages. If the number of segments in a message exceeds SegmentsThreshold, +# retransmit the Acknowledgment message by AckRetransCount. +# Valid range 0-31. +# Defaults to 3 +#SegmentsThreshold = 3 + +# Interval increment between acknowledgment messages. +# Increment is measured in segments and calculated using the following formula: +# +# AckDelayIncrement + 1.5 +# +# Valid range 0-7 +# Defaults to 1 +#AckDelayIncrement = 1 + +# Maximum number of retransmissions of acknowledgment messages. +# Valid range 0-3 +# Defaults to 0 +#AckRetransCount = 0 + +# Timeout to discard a segmented message when no more new segments of the +# message are coming in. +# Timeout is measured in seconds and calculated using the following formula: +# +# (DiscardTimeout + 1) * 5 sec +# +# Valid range 0-15 +# Defaults to 1 +#DiscardTimeout = 1 + +# Interval between received segments of a message. This is used to control rate +# of transmission of acknowledgment messages. +# Increment is measured in milliseconds and calculated using the following +# formula: +# +# (ReceiverSegIntervalStep + 1) * 10 ms +# +# Valid range 0-15 +# Defaults to 5 +#ReceiverSegIntervalStep = 5 diff --git a/mesh/mesh.c b/mesh/mesh.c index f89230b6c..17236c110 100644 --- a/mesh/mesh.c +++ b/mesh/mesh.c @@ -248,16 +248,11 @@ void mesh_get_sar_receiver(void *sar_rxr) memcpy(sar_rxr, &mesh_sar_rxr, sizeof(struct mesh_sar_receiver)); } -static void parse_settings(const char *mesh_conf_fname) +static void parse_mesh_general(const struct l_settings *settings) { - struct l_settings *settings; char *str; uint32_t value; - settings = l_settings_new(); - if (!l_settings_load_from_file(settings, mesh_conf_fname)) - goto done; - str = l_settings_get_string(settings, "General", "Beacon"); if (str) { if (!strcasecmp(str, "true")) @@ -290,6 +285,79 @@ static void parse_settings(const char *mesh_conf_fname) if (l_settings_get_uint(settings, "General", "ProvTimeout", &value)) mesh.prov_timeout = value; +} + +static void parse_mesh_sar(const struct l_settings *settings) +{ + uint32_t value; + + if (l_settings_get_uint(settings, "SARTransmitter", "SegIntervalStep", + &value) && value <= 15) + mesh_sar_txr.seg_int_step = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "UnicastRetransCount", + &value) && value <= 15) + mesh_sar_txr.unicast_rtx_cnt = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "UnicastRetransWithoutProgressCount", + &value) && value <= 15) + mesh_sar_txr.unicast_rtx_without_prog_cnt = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "UnicastRetransIntervalStep", + &value) && value <= 15) + mesh_sar_txr.unicast_rtx_int_step = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "UnicastRetransIntervalIncrement", + &value) && value <= 15) + mesh_sar_txr.unicast_rtx_int_inc = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "MulticastRetransCount", + &value) && value <= 15) + mesh_sar_txr.multicast_rtx_cnt = value; + + if (l_settings_get_uint(settings, "SARTransmitter", + "MulticastRetransIntervalStep", + &value) && value <= 15) + mesh_sar_txr.multicast_rtx_int_step = value; + + if (l_settings_get_uint(settings, "SARReceiver", "SegmentsThreshold", + &value) && value <= 31) + mesh_sar_rxr.seg_threshold = value; + + if (l_settings_get_uint(settings, "SARReceiver", "AckDelayIncrement", + &value) && value <= 7) + mesh_sar_rxr.ack_delay_inc = value; + + if (l_settings_get_uint(settings, "SARReceiver", "AckRetransCount", + &value) && value <= 3) + mesh_sar_rxr.ack_rtx_cnt = value; + + if (l_settings_get_uint(settings, "SARReceiver", "DiscardTimeout", + &value) && value <= 15) + mesh_sar_rxr.discard_timeout = value; + + if (l_settings_get_uint(settings, "SARReceiver", + "ReceiverSegIntervalStep", + &value) && value <= 15) + mesh_sar_rxr.receiver_seg_int_step = value; +} + +static void parse_settings(const char *mesh_conf_fname) +{ + struct l_settings *settings; + + settings = l_settings_new(); + if (!l_settings_load_from_file(settings, mesh_conf_fname)) + goto done; + + parse_mesh_general(settings); + parse_mesh_sar(settings); + done: l_settings_free(settings); } From patchwork Mon Oct 14 05:23:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junho Lee X-Patchwork-Id: 13834161 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 163C3231C9A for ; Mon, 14 Oct 2024 05:23:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883393; cv=none; b=QgIk74DMHRyMDyHnS/eUyeIx8yYpEB2cYg6Uxe1nMOCIeqdAeOmu9p85KeurxPXupl7su2Zt9W2uZNPHZhHKSHeVkhXPB5T1RZqbvKWTh2LRulPsW8M5KXl8LOw0fC3K2A4f2lI1O3A5zmB6Qs7psIFGnyL//AIR2UbejNPxSL4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883393; c=relaxed/simple; bh=22rmkplHJOZuJfQerTHUr1KtHf0Zubet17D6hFOCe7o=; h=Message-ID:Date:From:To:Subject:MIME-Version:Content-Type: Content-Disposition; b=HVeFE+Ba6NVeHIh/Aqu19W4G8kmP/1fhBdzzvCdPn+hIyCT3V70IR68YDcggiSAvs8w8W1fBH8urcpvV12J+0Tlgu9GL9UNGERIEgSMnU9nb6LnlrMAXlw5wVFQUFRXBDarVd0xL9x/rvhQ2mOpa/YnMdX10xPp+oqHAsK+BUFw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BKEiuzA1; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BKEiuzA1" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-2e2fb304e7dso2161536a91.1 for ; Sun, 13 Oct 2024 22:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728883391; x=1729488191; darn=vger.kernel.org; h=content-disposition:mime-version:subject:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=5cahgCFq8ehz8N/gd6lTB5/KBKLYT/QE1Od2NZO1lEw=; b=BKEiuzA1fqG3ng7bjEnPaOnBcOwB4LgTaXF8PNTugeC1ycQue40fprm5P3g0YtFEGE ikApWpZRtqlN9RM0Hwj5IYNdITPodJev8kAOfIJbGXoJPf30oGSOvaYmVjMwCdRnzP05 LjCrvs/xlnoMpWXuiuj0TslFfTBF5o9VJcF/Mvz29g3CXNn5pWLzApj5vXIv7QatAy3I olN4vW41Pa4TJGA6CZTc44SwzsgsCcVPfYjFsyNb9nBEZs9fGEk6rc7QO6isEeswfoRy iPm7OtxQLABQR0LI29fOhKBsD4ykXO+CnR759y3Pc512xREAdiiYfokiDWwjOeL/kDIw MF3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728883391; x=1729488191; h=content-disposition:mime-version:subject:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5cahgCFq8ehz8N/gd6lTB5/KBKLYT/QE1Od2NZO1lEw=; b=czKfh3BiDl0dVLEBjutDfDn7pE2KuHmavwyK8u2GC36qP84jmEm4G1FM3dGl7dYNtM 5KUnsN/iX5mPm+sVCmyDAjTgPacPv+IGmyZX/oOf3lcXAmhf4yzUUDdPaCNXJfoqbB9T 5oNPZOFP0jO4ieYUsiZdV0Y1cmg0VQx6BkQsqPiS6VbeW4AL4vOBcqr4C1NehosS6xgo zOT52vKobpoHdTW13I9X6MLd74S8UHuKhO7fTssg2YZMNfrMqxHNAGbiSIL+kOS5Eg/N clKZHSRZM618CGw3QS7bmYH4jFbwD4RbpBYWwQTC4seKnWKi8Oe9ko+DZlm6jtGa4Cob XiSQ== X-Gm-Message-State: AOJu0Yy2Jj5tf/+utaBTPWLaAU3MEZRkPab18rEst/kz/mDjr/0j4/AM fFQiLyEMlnuE5IC6MtWb7UO2egVYl40zf7nfQsHaa1XY9UVwp/zLK2xhIA== X-Google-Smtp-Source: AGHT+IEs1hfcMsyrYxjUx9pXR3zHwqXatIuoccyhQ03e8WvGBVW4ZUTquNW1odItbCm8mnP8Idn0+Q== X-Received: by 2002:a17:90b:3b81:b0:2e2:b17a:b118 with SMTP id 98e67ed59e1d1-2e2f0c550e1mr13112910a91.32.1728883391084; Sun, 13 Oct 2024 22:23:11 -0700 (PDT) Received: from JUNHO-NEOSTACK. ([221.154.134.3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e2d1c0fd2bsm7837630a91.17.2024.10.13.22.23.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 22:23:10 -0700 (PDT) Message-ID: <670caabe.170a0220.2dd053.5e00@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 14 Oct 2024 14:23:08 +0900 From: Junho Lee To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 3/4] mesh: Fix inserting NULL into bool type arguments to false Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline Inserting NULL into a variable of type bool is undefined behavior and inconsistent. --- mesh/net.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mesh/net.c b/mesh/net.c index 48032c64d..82c812b73 100644 --- a/mesh/net.c +++ b/mesh/net.c @@ -2649,7 +2649,7 @@ static enum _relay_advice packet_received(void *user_data, msg, app_msg_len); } } else { - seg_rxed(net, NULL, iv_index, net_ttl, + seg_rxed(net, false, iv_index, net_ttl, net_seq, net_idx, net_src, net_dst, key_aid, net_szmic, net_seqZero, net_segO, net_segN, @@ -2657,10 +2657,10 @@ static enum _relay_advice packet_received(void *user_data, } } else { - msg_rxed(net, NULL, iv_index, net_ttl, net_seq, net_idx, - net_src, net_dst, key_aid, false, - false, net_seq & SEQ_ZERO_MASK, msg, - app_msg_len); + msg_rxed(net, false, iv_index, net_ttl, net_seq, + net_idx, net_src, net_dst, key_aid, + false, false, net_seq & SEQ_ZERO_MASK, + msg, app_msg_len); } /* If this is one of our Unicast addresses, disallow relay */ From patchwork Mon Oct 14 05:23:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Junho Lee X-Patchwork-Id: 13834162 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF8A9231C9A for ; Mon, 14 Oct 2024 05:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883415; cv=none; b=NhxaJvS5TuTZJCNaHQzcXl0Jd2Jz9XStUMdZeXAlz1APBsFg8+62ujtFw+XTg98zVTofr5OLvjI9WFHRJQ4ViIRJhPNk91wt4Yu6w+bzZZ6xbQKi8OUYlE0uqSUTzb+rRub/HoYBzxS0XySwedq1BcI4kB4p9nbhsDgExhieQwk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728883415; c=relaxed/simple; bh=mOP+3hYa6+AK08a6UpOqnUyx/0ifCRmGMLKyrExB0ik=; h=Message-ID:Date:From:To:Subject:MIME-Version:Content-Type: Content-Disposition; b=HCU48rezOHUBlxtqFbmfWZeWqnDbd50OzFMnw8Cl9gSXhbKufWgRxXvdl8c/tktP6gypIJ0tHgsaytVWX9vjdvecsS+jdKap+S/N/ihYh6vsWtfoMIlCcOh4iSUyBqNqvio+9BB/oT6XbpFE5zBGhWyKpg/9A9rzztVSMqFPERE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cH4eLDY0; arc=none smtp.client-ip=209.85.215.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cH4eLDY0" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-7ea7ad1e01fso593121a12.0 for ; Sun, 13 Oct 2024 22:23:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728883412; x=1729488212; darn=vger.kernel.org; h=content-disposition:mime-version:subject:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=tTEhZXeXvYCtqYbbp1VU57C6tf++vWvSo01WkePNrdw=; b=cH4eLDY0+nStqssqxPgulqKUqPovM5KiEsaAmZQbFG/ofWqbOJE3mwB4vEcCIw+jbs fK3Qg5bLLvSWNUc/OViStheLV0qzFeRVOQVTyqtCLUWHklgVIboApVZVqclNiYKuKkqC 0dMzFwz9y7xW8HgBhWUqGYfLyNjYOiQLr2Tsy3zwpVqsTAFyk02rgxnSDtx0rorw6BuN 9tYa7xZIeHDl7Wu6Kg5fz+S4efK4M4FTcPstVER4baDV+tsgi1kBVchKNpjFVqzUayZA frCatW5qf9PjKOCVBl3TaBc+UQiH7MOGt1FWKhAS7p6PhhpSFDAyiFmhYER0nu+k20Kt VY/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728883412; x=1729488212; h=content-disposition:mime-version:subject:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tTEhZXeXvYCtqYbbp1VU57C6tf++vWvSo01WkePNrdw=; b=KJ6eSt5hUAgjZxKjlXIYmfJcBEJHbFsB2em/DeYxkdLZBPIRW25dxPAHA/cojSdYNL Z0EQHtNPYuqmJU+zC+73XxzU1L/fkEgnO9A51p4TWkNwHDj2k/oZt6PriAlXIpGZEY/d 3Rp0wvzPrKGhiWH8LqMy1b9a4sZ6iabhNBV0N10VNBBgH921TPObacgB6w/+rGa3d7Yp ZKJysSuSY+97/ON2GYqg1DKdazUjvbVy7SGECHudNBDQ40oTnB8Dr5/W03H7XeVE91p8 2ielcldp6+FAsfXt3z0wau4PlAY0beOEAlKfQTt4yTWR4rwVitbmB3ZSQpJACIuPdzm+ GwFQ== X-Gm-Message-State: AOJu0YybHuV8yYncfa7prfNMjE/Zrjyymo6gki31LXvvsX0VCAgfbowR FBvY1z2WBEvY4TJ50qiJDYtvCmw3Mp/rSb1BZTa4PWC8PYsqiVl1X9FcbA== X-Google-Smtp-Source: AGHT+IGeRC3/1+ZLzVtd4ywnhiV5nC8FvI8sla+2JgqHOT6H3LZAC4fJgUiv45uC6cQwpdfmy5F9jA== X-Received: by 2002:a05:6a20:c888:b0:1c8:de01:e7e5 with SMTP id adf61e73a8af0-1d8bc8c8c47mr12586068637.15.1728883411450; Sun, 13 Oct 2024 22:23:31 -0700 (PDT) Received: from JUNHO-NEOSTACK. ([221.154.134.3]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71e51081a33sm3393409b3a.133.2024.10.13.22.23.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Oct 2024 22:23:31 -0700 (PDT) Message-ID: <670caad3.a70a0220.273f1.6612@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 14 Oct 2024 14:23:28 +0900 From: Junho Lee To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2 4/4] mesh: Add features of Segmented Control message Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline According to Mesh Protocol v1.1, with the addition of Transport Control messages with variable length PDU such as 'Friend Subscription List Add' and 'Friend Subscription List Remove', it is necessary to support segmentation in the Lower Transport layer because the Upper Transport Control PDU can be larger than the size of a single Network PDU. This is achieved by performing the segmentation function for Transport Control messages, where the segmentation of Transport Control messages is the same as for Access messages, but with different message fields and lengths, so the segmentation is controlled by branching. It was written to perform the function as little as possible without changing the existing design. We avoided modifying arguments outside of static functions, and kept the existing working design unchanged. However, some macro definitions had to be modified because they were written based on Access messages, so we changed them to work by comparing the Transport Control message flag. This commit does not support segmentation for control messages as a friend of Low Power Nodes. This should be added later. --- mesh/crypto.c | 69 +++++----- mesh/net.c | 279 +++++++++++++++++++++++++--------------- mesh/net.h | 33 ++++- unit/test-mesh-crypto.c | 4 +- 4 files changed, 246 insertions(+), 139 deletions(-) diff --git a/mesh/crypto.c b/mesh/crypto.c index b712a2654..08548031b 100644 --- a/mesh/crypto.c +++ b/mesh/crypto.c @@ -532,7 +532,8 @@ bool mesh_crypto_packet_build(bool ctl, uint8_t ttl, uint8_t *packet, uint8_t *packet_len) { uint32_t hdr; - size_t n; + size_t hdr_offset = 9; + size_t payload_offset = 10; if (seq > SEQ_MASK) return false; @@ -543,50 +544,44 @@ bool mesh_crypto_packet_build(bool ctl, uint8_t ttl, l_put_be16(src, packet + 5); l_put_be16(dst, packet + 7); - n = 9; - if (!ctl) { - uint32_t tmp = segmented ? 0x1 : 0; + hdr = (segmented ? 0x1 : 0) << SEG_HDR_SHIFT; + + if (segmented) { + hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT; + hdr |= (segO & SEG_MASK) << SEGO_HDR_SHIFT; + hdr |= (segN & SEG_MASK) << SEGN_HDR_SHIFT; + payload_offset += 3; + } - hdr = tmp << SEG_HDR_SHIFT; + if (!ctl) { hdr |= (key_aid & KEY_ID_MASK) << KEY_HDR_SHIFT; if (segmented) { hdr |= szmic << SZMIC_HDR_SHIFT; - hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT; - hdr |= (segO & SEG_MASK) << SEGO_HDR_SHIFT; - hdr |= (segN & SEG_MASK) << SEGN_HDR_SHIFT; } - l_put_be32(hdr, packet + n); - - /* Only first octet is valid for unsegmented messages */ - if (segmented) - n += 4; - else - n += 1; - - memcpy(packet + n, payload, payload_len); - - l_put_be32(0x00000000, packet + payload_len + n); - if (packet_len) - *packet_len = payload_len + n + 4; } else { if ((opcode & OPCODE_MASK) != opcode) return false; - hdr = opcode << KEY_HDR_SHIFT; - l_put_be32(hdr, packet + n); - n += 1; + hdr |= opcode << OPCODE_HDR_SHIFT; + } + + l_put_be32(hdr, packet + hdr_offset); - memcpy(packet + n, payload, payload_len); - n += payload_len; + memcpy(packet + payload_offset, payload, payload_len); - l_put_be64(0x0000000000000000, packet + n); + if (!ctl) { + l_put_be32(0x00000000, packet + payload_offset + payload_len); if (packet_len) - *packet_len = n + 8; + *packet_len = payload_offset + payload_len + 4; + } else { + l_put_be64(0x0000000000000000, packet + payload_offset + + payload_len); + if (packet_len) + *packet_len = payload_offset + payload_len + 8; } - return true; } @@ -665,6 +660,22 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len, if (payload_len) *payload_len = packet_len - 9; + } else if (is_segmented) { + if (seqZero) + *seqZero = (hdr >> SEQ_ZERO_HDR_SHIFT) & + SEQ_ZERO_MASK; + + if (segO) + *segO = (hdr >> SEGO_HDR_SHIFT) & SEG_MASK; + + if (segN) + *segN = (hdr >> SEGN_HDR_SHIFT) & SEG_MASK; + + if (payload) + *payload = packet + 13; + + if (payload_len) + *payload_len = packet_len - 13; } else { if (payload) *payload = packet + 10; diff --git a/mesh/net.c b/mesh/net.c index 82c812b73..215db995c 100644 --- a/mesh/net.c +++ b/mesh/net.c @@ -179,12 +179,20 @@ struct mesh_sar { uint16_t src; uint16_t remote; uint16_t len; - bool szmic; bool segmented; + bool ctl; bool frnd; bool frnd_cred; uint8_t ttl; - uint8_t key_aid; + union { + struct { + uint8_t key_aid; + bool szmic; + }; /* Access message */ + struct { + uint8_t opcode; + }; /* Transport Control message */ + }; uint8_t buf[4]; /* Large enough for ACK-Flags and MIC */ }; @@ -1964,7 +1972,7 @@ static bool msg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, hdr |= (uint32_t) 0x01 << SEG_HDR_SHIFT; hdr |= szmic << SZMIC_HDR_SHIFT; hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT; - hdr |= SEG_MAX(true, size) << SEGN_HDR_SHIFT; + hdr |= SEG_MAX(false, true, size) << SEGN_HDR_SHIFT; } if (friend_packet_queue(net, iv_index, false, frnd_ttl, @@ -2141,11 +2149,18 @@ static void friend_seg_rxed(struct mesh_net *net, frnd_msg->cnt_in++; } +static bool ctl_rxed(struct mesh_net *net, uint32_t net_key_id, + uint32_t iv_index, uint8_t ttl, + uint32_t seq, uint16_t src, + uint16_t dst, uint8_t opcode, + int8_t rssi, const uint8_t *pkt, + uint8_t len); + static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, uint8_t ttl, uint32_t seq, uint16_t net_idx, - uint16_t src, uint16_t dst, - uint8_t key_aid, + uint16_t src, uint16_t dst, bool ctl, + uint8_t opcode, uint8_t key_aid, bool szmic, uint16_t seqZero, uint8_t segO, uint8_t segN, const uint8_t *data, uint8_t size) @@ -2194,10 +2209,15 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, l_debug("RXed (old: %04x %06x size:%d) %d of %d", seqZero, seq, size, segO, segN); /* Sanity Check--> certain things must match */ - if (SEG_MAX(true, sar->len) != segN || - sar->key_aid != key_aid) + if (SEG_MAX(ctl, true, sar->len) != segN) return false; + if (!ctl && sar->key_aid != key_aid) { + return false; + } else if (ctl && sar->opcode != opcode) { + return false; + } + if (sar->flags == expected) { /* * According to MshPRTv1.1: 3.5.3.4, if the destination @@ -2217,7 +2237,7 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, return true; } } else { - uint16_t len = MAX_SEG_TO_LEN(segN); + uint16_t len = MAX_SEG_TO_LEN(ctl, segN); l_debug("RXed (new: %04x %06x size: %d len: %d) %d of %d", seqZero, seq, size, len, segO, segN); @@ -2228,9 +2248,12 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, sar->src = dst; sar->remote = src; sar->seqZero = seqZero; - sar->key_aid = key_aid; sar->len = len; sar->net_idx = net_idx; + if (ctl) + sar->opcode = opcode; + else + sar->key_aid = key_aid; sar_in = mesh_sar_rx_new(); sar_in->sar = sar; @@ -2270,14 +2293,14 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, l_timeout_remove(sar_in->msg_timeout); sar_in->msg_timeout = NULL; - seg_off = segO * MAX_SEG_LEN; + seg_off = segO * MAX_SEG_LEN(ctl); memcpy(sar->buf + seg_off, data, size); sar->flags |= this_seg_flag; sar->ttl = ttl; /* Msg length only definitive on last segment */ if (segO == segN) - sar->len = segN * MAX_SEG_LEN + size; + sar->len = segN * MAX_SEG_LEN(ctl) + size; /* Send ACK only if DST is unicast address. */ if (IS_UNICAST(dst)) { @@ -2290,9 +2313,21 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, /* Got it all */ send_net_ack(net, sar, expected); - msg_rxed(net, frnd, iv_index, ttl, seq, net_idx, - sar->remote, dst, key_aid, true, szmic, - sar->seqZero, sar->buf, sar->len); + if (!ctl) { + msg_rxed(net, frnd, iv_index, ttl, seq, net_idx, + sar->remote, dst, key_aid, true, szmic, + sar->seqZero, sar->buf, sar->len); + } else { + struct mesh_subnet *subnet = l_queue_find(net->subnets, + match_key_index, + L_UINT_TO_PTR(net_idx)); + if (!subnet) + return false; + + ctl_rxed(net, subnet->net_key_tx, iv_index, ttl, + seq, sar->remote, dst, + opcode, 0, sar->buf, sar->len); + } /* * Delay SAR removal to be able to acknowledge a transaction @@ -2312,7 +2347,7 @@ static bool seg_rxed(struct mesh_net *net, bool frnd, uint32_t iv_index, return false; } -static bool ctl_received(struct mesh_net *net, uint32_t net_key_id, +static bool ctl_rxed(struct mesh_net *net, uint32_t net_key_id, uint32_t iv_index, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst, @@ -2324,7 +2359,11 @@ static bool ctl_received(struct mesh_net *net, uint32_t net_key_id, uint8_t n = 0; uint16_t net_idx; - if (ttl > 1) { + /* TODO: If length is greater than 11, it must be segmented, so it must + * be able to handle Segmented Control messages when acting as a friend + * node. + */ + if (ttl > 1 && len <= 11) { uint32_t hdr = opcode << OPCODE_HDR_SHIFT; uint8_t frnd_ttl = ttl - 1; @@ -2625,8 +2664,15 @@ static enum _relay_advice packet_received(void *user_data, ack_rxed(net, net_src, net_dst, net_seqZero, l_get_be32(msg + 3)); + } else if (net_segmented) { + seg_rxed(net, false, iv_index, net_ttl, + net_seq, net_idx, net_src, + net_dst, net_ctl, net_opcode, + key_aid, net_szmic, net_seqZero, + net_segO, net_segN, + msg, app_msg_len); } else { - ctl_received(net, net_key_id, iv_index, net_ttl, + ctl_rxed(net, net_key_id, iv_index, net_ttl, net_seq, net_src, net_dst, net_opcode, rssi, msg, app_msg_len); @@ -2651,8 +2697,9 @@ static enum _relay_advice packet_received(void *user_data, } else { seg_rxed(net, false, iv_index, net_ttl, net_seq, net_idx, net_src, - net_dst, key_aid, net_szmic, - net_seqZero, net_segO, net_segN, + net_dst, net_ctl, net_opcode, + key_aid, net_szmic, net_seqZero, + net_segO, net_segN, msg, app_msg_len); } @@ -3371,16 +3418,16 @@ static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval, uint8_t gatt_data[30]; uint8_t *packet = gatt_data; uint8_t packet_len; - uint8_t segN = SEG_MAX(msg->segmented, msg->len); - uint16_t seg_off = SEG_OFF(segO); + uint8_t segN = SEG_MAX(msg->ctl, msg->segmented, msg->len); + uint16_t seg_off = SEG_OFF(msg->ctl, segO); uint32_t seq_num; if (msg->segmented) { /* Send each segment on unique seq_num */ seq_num = mesh_net_next_seq_num(net); - if (msg->len - seg_off > SEG_OFF(1)) - seg_len = SEG_OFF(1); + if (msg->len - seg_off > SEG_OFF(msg->ctl, 1)) + seg_len = SEG_OFF(msg->ctl, 1); else seg_len = msg->len - seg_off; } else { @@ -3396,12 +3443,12 @@ static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval, l_debug("segN %d segment %d seg_off %d", segN, segO, seg_off); /* TODO: Are we RXing on an LPN's behalf? Then set RLY bit */ - if (!mesh_crypto_packet_build(false, msg->ttl, seq_num, msg->src, - msg->remote, 0, msg->segmented, - msg->key_aid, msg->szmic, false, - msg->seqZero, segO, segN, - msg->buf + seg_off, seg_len, - packet + 1, &packet_len)) { + if (!mesh_crypto_packet_build(msg->ctl, msg->ttl, seq_num, msg->src, + msg->remote, msg->opcode, + msg->segmented, msg->key_aid, + msg->szmic, false, msg->seqZero, + segO, segN, msg->buf + seg_off, + seg_len, packet + 1, &packet_len)) { l_error("Failed to build packet"); return false; } @@ -3424,6 +3471,63 @@ static bool send_seg(struct mesh_net *net, uint8_t cnt, uint16_t interval, return true; } +static bool mesh_net_send(struct mesh_net *net, struct mesh_sar *payload, + uint8_t segN) +{ + bool result = false; + + if (payload->segmented) { + struct mesh_sar_tx *drop_sar_tx; + + payload->flags = 0xffffffff >> (31 - segN); + payload->seqZero = payload->seqAuth & SEQ_ZERO_MASK; + + /* Single thread SAR messages to same Unicast DST */ + drop_sar_tx = l_queue_find(net->sar_out, match_sar_tx_remote, + L_UINT_TO_PTR(payload->remote)); + if (drop_sar_tx) { + /* Cancel incomplete prior SAR on the same dst */ + l_debug("Cancel incompleted SAR: SeqZero %4.4x", + drop_sar_tx->sar->seqZero); + l_queue_remove(net->sar_out, drop_sar_tx); + mesh_sar_tx_free(drop_sar_tx); + } + } + + result = send_seg(net, net->tx_cnt, net->tx_interval, payload, 0); + + /* + * Set the timeout to send the next seg or retransmit if the payload is + * segmented. Flush if it is not segmented or if the transmission + * failed. + */ + if (result && payload->segmented) { + struct mesh_sar_tx *sar_tx = mesh_sar_tx_new(); + bool is_unicast = IS_UNICAST(payload->remote); + + sar_tx->ack_received = false; + sar_tx->int_ms = sar_tx_seg_int_ms(net->sar_txr); + sar_tx->attempt_left = sar_tx_retrans_cnt(net->sar_txr, + is_unicast, false); + sar_tx->attempt_left_no_progress = sar_tx_retrans_cnt( + net->sar_txr, + is_unicast, true); + sar_tx->retrans_ms = sar_tx_retrans_timeout_ms(net->sar_txr, + is_unicast, + payload->ttl); + sar_tx->sar = payload; + l_queue_push_head(net->sar_out, sar_tx); + sar_tx->seg_timeout = l_timeout_create_ms(sar_tx->int_ms, + send_next_seg_to, net, NULL); + sar_tx->segO = 1; /* The 0th seg is already sent. */ + sar_tx->segN = segN; + } else { + mesh_sar_free(payload); + } + + return result; +} + void mesh_net_send_seg(struct mesh_net *net, uint32_t net_key_id, uint32_t iv_index, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst, uint32_t hdr, @@ -3497,7 +3601,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, /* Long and sizmic messages *require* segmenting */ segmented |= szmic; - seg_max = SEG_MAX(segmented, msg_len); + seg_max = SEG_MAX(false, segmented, msg_len); segmented |= !!(seg_max); /* First enqueue to any Friends and internal models */ @@ -3524,6 +3628,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, payload = mesh_sar_new(msg_len); memcpy(payload->buf, msg, msg_len); payload->len = msg_len; + payload->ctl = false; payload->src = src; payload->remote = dst; payload->ttl = ttl; @@ -3535,53 +3640,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, payload->seqAuth = seq; payload->segmented = segmented; - if (segmented) { - struct mesh_sar_tx *drop_sar_tx; - payload->flags = 0xffffffff >> (31 - seg_max); - payload->seqZero = seq & SEQ_ZERO_MASK; - - /* Single thread SAR messages to same Unicast DST */ - drop_sar_tx = l_queue_find(net->sar_out, match_sar_tx_remote, - L_UINT_TO_PTR(dst)); - if (drop_sar_tx) { - /* Cancel incomplete prior SAR on the same dst */ - l_debug("Cancel incompleted SAR: SeqZero %4.4x", - drop_sar_tx->sar->seqZero); - l_queue_remove(net->sar_out, drop_sar_tx); - mesh_sar_tx_free(drop_sar_tx); - } - } - - result = send_seg(net, cnt, interval, payload, 0); - - /* - * Set the timeout to send the next seg or retransmit if the payload is - * segmented. Flush if it is not segmented or if the transmission - * failed. - */ - if (result && segmented) { - struct mesh_sar_tx *sar_tx = mesh_sar_tx_new(); - bool is_unicast = IS_UNICAST(dst); - - sar_tx->ack_received = false; - sar_tx->int_ms = sar_tx_seg_int_ms(net->sar_txr); - sar_tx->attempt_left = sar_tx_retrans_cnt(net->sar_txr, - is_unicast, false); - sar_tx->attempt_left_no_progress = sar_tx_retrans_cnt( - net->sar_txr, - is_unicast, true); - sar_tx->retrans_ms = sar_tx_retrans_timeout_ms(net->sar_txr, - is_unicast, - ttl); - sar_tx->sar = payload; - l_queue_push_head(net->sar_out, sar_tx); - sar_tx->seg_timeout = l_timeout_create_ms(sar_tx->int_ms, - send_next_seg_to, net, NULL); - sar_tx->segO = 1; /* The 0th seg is already sent. */ - sar_tx->segN = seg_max; - } else { - mesh_sar_free(payload); - } + result = mesh_net_send(net, payload, seg_max); return result; } @@ -3640,11 +3699,18 @@ void mesh_net_transport_send(struct mesh_net *net, uint32_t net_key_id, uint16_t dst, const uint8_t *msg, uint16_t msg_len) { - uint32_t use_seq = seq; - uint8_t pkt_len; - uint8_t pkt[30]; + struct mesh_sar *payload; + uint8_t opcode, seg_max; + bool segmented = false; bool result = false; + /* + * Check maximum message length: + * Parameter length(8) * Maximum # of segments(32) + Header size(1) + */ + if (!net || msg_len > 257) + return; + if (!net->src_addr) return; @@ -3657,10 +3723,19 @@ void mesh_net_transport_send(struct mesh_net *net, uint32_t net_key_id, if (ttl == DEFAULT_TTL) ttl = net->default_ttl; - /* Range check the Opcode and msg length*/ - if (*msg & 0xc0 || (9 + msg_len + 8 > 29)) + /* + * Mesh Protocol v1.1: An unassigned address shall not be used in the + * SRC field or the DST field of a Network PDU. + */ + if (IS_UNASSIGNED(src) || IS_UNASSIGNED(dst)) return; + segmented |= msg[0] & SEGMENTED; + seg_max = SEG_MAX(true, segmented, msg_len - 1); + segmented |= !!(seg_max); + + opcode = msg[0] & OPCODE_MASK; + /* * MshPRFv1.0.1 section 3.4.5.2, Interface output filter: * If TTL is set to 1, message shall be dropped. @@ -3681,36 +3756,38 @@ void mesh_net_transport_send(struct mesh_net *net, uint32_t net_key_id, /* Deliver to Local entities if applicable */ if (!(dst & 0x8000) && src >= net->src_addr && src <= net->last_addr) - result = ctl_received(net, net_key_id, iv_index, ttl, + result = ctl_rxed(net, net_key_id, iv_index, ttl, mesh_net_next_seq_num(net), src, dst, msg[0], 0, msg + 1, msg_len - 1); if (!net_key_id) { + seq = mesh_net_next_seq_num(net); + + if (result || (dst >= net->src_addr && dst <= net->last_addr)) + return; + } else { struct mesh_subnet *subnet = l_queue_find(net->subnets, - match_key_index, L_UINT_TO_PTR(net_idx)); + match_key_id, L_UINT_TO_PTR(net_key_id)); if (!subnet) return; - net_key_id = subnet->net_key_tx; - use_seq = mesh_net_next_seq_num(net); - - if (result || (dst >= net->src_addr && dst <= net->last_addr)) - return; + net_idx = subnet->idx; } - if (!mesh_crypto_packet_build(true, ttl, use_seq, src, dst, msg[0], - false, 0, false, false, 0, 0, 0, msg + 1, - msg_len - 1, pkt + 1, &pkt_len)) - return; - - if (!net_key_encrypt(net_key_id, iv_index, pkt + 1, pkt_len)) { - l_error("Failed to encode packet"); - return; - } + payload = mesh_sar_new(msg_len - 1); + memcpy(payload->buf, msg + 1, msg_len - 1); + payload->len = msg_len - 1; + payload->ctl = true; + payload->ttl = ttl; + payload->src = src; + payload->remote = dst; + payload->opcode = opcode; + payload->net_idx = net_idx; + payload->iv_index = iv_index; + payload->seqAuth = seq; + payload->segmented = segmented; - if (!(IS_UNASSIGNED(dst))) - send_msg_pkt(net, net->tx_cnt, net->tx_interval, pkt, - pkt_len + 1); + mesh_net_send(net, payload, seg_max); } int mesh_net_key_refresh_phase_set(struct mesh_net *net, uint16_t idx, diff --git a/mesh/net.h b/mesh/net.h index d385ba16e..285c8e355 100644 --- a/mesh/net.h +++ b/mesh/net.h @@ -26,13 +26,32 @@ struct mesh_node; #define KEY_CACHE_SIZE 64 #define FRND_CACHE_MAX 32 -#define MAX_UNSEG_LEN 15 /* msg_len == 11 + sizeof(MIC) */ -#define MAX_SEG_LEN 12 /* UnSeg length - 3 octets overhead */ -#define SEG_MAX(seg, len) ((!seg && len <= MAX_UNSEG_LEN) ? 0 : \ - (((len) - 1) / MAX_SEG_LEN)) - -#define SEG_OFF(seg) ((seg) * MAX_SEG_LEN) -#define MAX_SEG_TO_LEN(seg) ((seg) ? SEG_OFF((seg) + 1) : MAX_UNSEG_LEN) +#define MAX_CTL_UNSEG_LEN 11 /* msg_len == 7 + sizeof(MIC) */ +#define MAX_CTL_SEG_LEN 8 /* UnSeg length - 3 octets overhead */ +#define CTL_SEG_MAX(seg, len) ((!(seg) && (len) <= MAX_CTL_UNSEG_LEN) ? 0 : \ + (((len) - 1) / MAX_CTL_SEG_LEN)) + +#define MAX_ACC_UNSEG_LEN 15 /* msg_len == 11 + sizeof(MIC) */ +#define MAX_ACC_SEG_LEN 12 /* UnSeg length - 3 octets overhead */ +#define ACC_SEG_MAX(seg, len) ((!(seg) && (len) <= MAX_ACC_UNSEG_LEN) ? 0 : \ + (((len) - 1) / MAX_ACC_SEG_LEN)) + +#define MAX_UNSEG_LEN(ctl) ((ctl) ? MAX_CTL_UNSEG_LEN : MAX_ACC_UNSEG_LEN) +#define MAX_SEG_LEN(ctl) ((ctl) ? MAX_CTL_SEG_LEN : MAX_ACC_SEG_LEN) +#define SEG_MAX(ctl, seg, len) ((ctl) ? CTL_SEG_MAX((seg), (len)) : \ + ACC_SEG_MAX((seg), (len))) + +#define CTL_SEG_OFF(seg) ((seg) * MAX_CTL_SEG_LEN) +#define MAX_CTL_SEG_TO_LEN(seg) ((seg) ? CTL_SEG_OFF((seg) + 1) : \ + MAX_CTL_UNSEG_LEN) + +#define ACC_SEG_OFF(seg) ((seg) * MAX_ACC_SEG_LEN) +#define MAX_ACC_SEG_TO_LEN(seg) ((seg) ? ACC_SEG_OFF((seg) + 1) : \ + MAX_ACC_UNSEG_LEN) + +#define SEG_OFF(ctl, seg) ((ctl) ? CTL_SEG_OFF(seg) : ACC_SEG_OFF(seg)) +#define MAX_SEG_TO_LEN(ctl, seg) ((ctl) ? MAX_CTL_SEG_TO_LEN(seg) : \ + MAX_ACC_SEG_TO_LEN(seg)) #define SEGMENTED 0x80 #define UNSEGMENTED 0x00 diff --git a/unit/test-mesh-crypto.c b/unit/test-mesh-crypto.c index 39632d973..56cf8c8e7 100644 --- a/unit/test-mesh-crypto.c +++ b/unit/test-mesh-crypto.c @@ -1087,7 +1087,7 @@ static void check_encrypt(const struct mesh_crypto_test *keys) app_msg = l_util_from_hexstring(keys->app_msg, &app_msg_len); if (keys->szmic) { - seg_max = SEG_MAX(keys->segmented, app_msg_len + 8); + seg_max = SEG_MAX(false, keys->segmented, app_msg_len + 8); enc_msg = l_malloc(app_msg_len + 8); status = mesh_crypto_payload_encrypt(aad, app_msg, @@ -1097,7 +1097,7 @@ static void check_encrypt(const struct mesh_crypto_test *keys) keys->szmic, keys->akf ? app_key : dev_key); } else { - seg_max = SEG_MAX(keys->segmented, app_msg_len + 4); + seg_max = SEG_MAX(false, keys->segmented, app_msg_len + 4); enc_msg = l_malloc(app_msg_len + 4); status = mesh_crypto_payload_encrypt(aad, app_msg,