Skip to content

Commit bc28655

Browse files
committed
f: de-macro malformed errors and infer zero-data from error code
1 parent d410ae0 commit bc28655

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

lightning/src/ln/onion_payment.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -473,27 +473,23 @@ where
473473
NS::Target: NodeSigner,
474474
L::Target: Logger,
475475
{
476-
macro_rules! return_malformed_err {
477-
($msg: expr, $err_code: expr, $force_blinding_error: expr) => {
478-
{
479-
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", $msg);
480-
let (sha256_of_onion, failure_code) = if $force_blinding_error || msg.blinding_point.is_some() {
481-
([0; 32], INVALID_ONION_BLINDING)
482-
} else {
483-
(Sha256::hash(&msg.onion_routing_packet.hop_data).to_byte_array(), $err_code)
484-
};
485-
return Err(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
486-
channel_id: msg.channel_id,
487-
htlc_id: msg.htlc_id,
488-
sha256_of_onion,
489-
failure_code,
490-
}));
491-
}
492-
}
493-
}
476+
let encode_malformed_error = |message: &str, err_code: u16| {
477+
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", message);
478+
let (sha256_of_onion, failure_code) = if msg.blinding_point.is_some() || err_code == INVALID_ONION_BLINDING {
479+
([0; 32], INVALID_ONION_BLINDING)
480+
} else {
481+
(Sha256::hash(&msg.onion_routing_packet.hop_data).to_byte_array(), err_code)
482+
};
483+
return Err(HTLCFailureMsg::Malformed(msgs::UpdateFailMalformedHTLC {
484+
channel_id: msg.channel_id,
485+
htlc_id: msg.htlc_id,
486+
sha256_of_onion,
487+
failure_code,
488+
}));
489+
};
494490

495491
if let Err(_) = msg.onion_routing_packet.public_key {
496-
return_malformed_err!("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6, false);
492+
return encode_malformed_error("invalid ephemeral pubkey", 0x8000 | 0x4000 | 6);
497493
}
498494

499495
if msg.onion_routing_packet.version != 0 {
@@ -503,12 +499,12 @@ where
503499
//receiving node would have to brute force to figure out which version was put in the
504500
//packet by the node that send us the message, in the case of hashing the hop_data, the
505501
//node knows the HMAC matched, so they already know what is there...
506-
return_malformed_err!("Unknown onion packet version", 0x8000 | 0x4000 | 4, false);
502+
return encode_malformed_error("Unknown onion packet version", 0x8000 | 0x4000 | 4);
507503
}
508504

509505
let encode_relay_error = |message: &str, err_code: u16, shared_secret: [u8; 32], trampoline_shared_secret: Option<[u8; 32]>, data: &[u8]| {
510506
if msg.blinding_point.is_some() {
511-
return_malformed_err!(message, INVALID_ONION_BLINDING, false)
507+
return encode_malformed_error(message, INVALID_ONION_BLINDING)
512508
}
513509

514510
log_info!(logger, "Failed to accept/forward incoming HTLC: {}", message);
@@ -526,8 +522,8 @@ where
526522
msg.payment_hash, msg.blinding_point, node_signer
527523
) {
528524
Ok(res) => res,
529-
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code, trampoline_onion_blinding }) => {
530-
return_malformed_err!(err_msg, err_code, trampoline_onion_blinding);
525+
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
526+
return encode_malformed_error(err_msg, err_code);
531527
},
532528
Err(onion_utils::OnionDecodeErr::Relay { err_msg, err_code, shared_secret, trampoline_shared_secret }) => {
533529
return encode_relay_error(err_msg, err_code, shared_secret.secret_bytes(), trampoline_shared_secret.map(|tss| tss.secret_bytes()), &[0; 0]);

lightning/src/ln/onion_utils.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,7 @@ pub(crate) enum OnionDecodeErr {
16761676
///
16771677
/// If the malformed onion packet was a blinded Trampoline hop, we communicate it up the call
16781678
/// stack for appropriate error handling.
1679-
Malformed { err_msg: &'static str, err_code: u16, trampoline_onion_blinding: bool },
1679+
Malformed { err_msg: &'static str, err_code: u16 },
16801680
/// We failed to decode the onion payload.
16811681
///
16821682
/// If the payload we failed to decode belonged to a Trampoline onion, following the successful
@@ -1735,7 +1735,6 @@ where
17351735
err_msg:
17361736
"Final Node OnionHopData provided for us as an intermediary node",
17371737
err_code: INVALID_ONION_BLINDING,
1738-
trampoline_onion_blinding: false,
17391738
});
17401739
}
17411740
Err(OnionDecodeErr::Relay {
@@ -1836,15 +1835,13 @@ where
18361835
Err(OnionDecodeErr::Malformed {
18371836
err_msg: "Non-final Trampoline onion data provided to us as last hop",
18381837
err_code: INVALID_ONION_BLINDING,
1839-
trampoline_onion_blinding: true,
18401838
})
18411839
},
18421840
Ok((msgs::InboundTrampolinePayload::BlindedReceive(_), Some(_))) => {
18431841
Err(OnionDecodeErr::Malformed {
18441842
err_msg:
18451843
"Final Trampoline onion data provided to us as intermediate hop",
18461844
err_code: INVALID_ONION_BLINDING,
1847-
trampoline_onion_blinding: true,
18481845
})
18491846
},
18501847
Ok((_, None)) => Err(OnionDecodeErr::Relay {
@@ -1871,7 +1868,6 @@ where
18711868
return Err(OnionDecodeErr::Malformed {
18721869
err_msg: "Intermediate Node OnionHopData provided for us as a final node",
18731870
err_code: INVALID_ONION_BLINDING,
1874-
trampoline_onion_blinding: false,
18751871
});
18761872
}
18771873
Err(OnionDecodeErr::Relay {
@@ -2006,7 +2002,6 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(
20062002
return Err(OnionDecodeErr::Malformed {
20072003
err_msg: "HMAC Check failed",
20082004
err_code: 0x8000 | 0x4000 | 5,
2009-
trampoline_onion_blinding: false,
20102005
});
20112006
}
20122007

0 commit comments

Comments
 (0)