@@ -3091,6 +3091,48 @@ where
3091
3091
}
3092
3092
}
3093
3093
3094
+ fn can_forward_htlc_to_outgoing_channel(
3095
+ &self, chan: &mut Channel<SP>, msg: &msgs::UpdateAddHTLC, next_packet: &NextPacketDetails
3096
+ ) -> Result<Option<msgs::ChannelUpdate>, (&'static str, u16, Option<msgs::ChannelUpdate>)> {
3097
+ if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
3098
+ // Note that the behavior here should be identical to the above block - we
3099
+ // should NOT reveal the existence or non-existence of a private channel if
3100
+ // we don't allow forwards outbound over them.
3101
+ return Err(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None));
3102
+ }
3103
+ if chan.context.get_channel_type().supports_scid_privacy() && next_packet.outgoing_scid != chan.context.outbound_scid_alias() {
3104
+ // `option_scid_alias` (referred to in LDK as `scid_privacy`) means
3105
+ // "refuse to forward unless the SCID alias was used", so we pretend
3106
+ // we don't have the channel here.
3107
+ return Err(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
3108
+ }
3109
+ let chan_update_opt = self.get_channel_update_for_onion(next_packet.outgoing_scid, chan).ok();
3110
+
3111
+ // Note that we could technically not return an error yet here and just hope
3112
+ // that the connection is reestablished or monitor updated by the time we get
3113
+ // around to doing the actual forward, but better to fail early if we can and
3114
+ // hopefully an attacker trying to path-trace payments cannot make this occur
3115
+ // on a small/per-node/per-channel scale.
3116
+ if !chan.context.is_live() { // channel_disabled
3117
+ // If the channel_update we're going to return is disabled (i.e. the
3118
+ // peer has been disabled for some time), return `channel_disabled`,
3119
+ // otherwise return `temporary_channel_failure`.
3120
+ if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
3121
+ return Err(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
3122
+ } else {
3123
+ return Err(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
3124
+ }
3125
+ }
3126
+ if next_packet.outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
3127
+ return Err(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
3128
+ }
3129
+ if let Err((err, code)) = chan.htlc_satisfies_config(msg, next_packet.outgoing_amt_msat, next_packet.outgoing_cltv_value) {
3130
+ return Err((err, code, chan_update_opt));
3131
+ }
3132
+
3133
+ Ok(chan_update_opt)
3134
+ }
3135
+
3094
3136
fn htlc_failure_from_update_add_err(
3095
3137
&self, msg: &msgs::UpdateAddHTLC, counterparty_node_id: &PublicKey, err_msg: &'static str,
3096
3138
mut err_code: u16, chan_update: Option<msgs::ChannelUpdate>, is_intro_node_blinded_forward: bool,
@@ -3155,9 +3197,7 @@ where
3155
3197
msg, &self.node_signer, &self.logger, &self.secp_ctx
3156
3198
)?;
3157
3199
3158
- let NextPacketDetails {
3159
- next_packet_pubkey, outgoing_amt_msat, outgoing_scid, outgoing_cltv_value
3160
- } = match next_packet_details_opt {
3200
+ let next_packet_details = match next_packet_details_opt {
3161
3201
Some(next_packet_details) => next_packet_details,
3162
3202
// it is a receive, so no need for outbound checks
3163
3203
None => return Ok((next_hop, shared_secret, None)),
@@ -3166,14 +3206,14 @@ where
3166
3206
// Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
3167
3207
// can't hold the outbound peer state lock at the same time as the inbound peer state lock.
3168
3208
if let Some((err, code, chan_update)) = loop {
3169
- let id_option = self.short_to_chan_info.read().unwrap().get(&outgoing_scid).cloned();
3209
+ let id_option = self.short_to_chan_info.read().unwrap().get(&next_packet_details. outgoing_scid).cloned();
3170
3210
let forwarding_chan_info_opt = match id_option {
3171
3211
None => { // unknown_next_peer
3172
3212
// Note that this is likely a timing oracle for detecting whether an scid is a
3173
3213
// phantom or an intercept.
3174
3214
if (self.default_configuration.accept_intercept_htlcs &&
3175
- fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)) ||
3176
- fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, outgoing_scid, &self.chain_hash)
3215
+ fake_scid::is_valid_intercept(&self.fake_scid_rand_bytes, next_packet_details. outgoing_scid, &self.chain_hash)) ||
3216
+ fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, next_packet_details. outgoing_scid, &self.chain_hash)
3177
3217
{
3178
3218
None
3179
3219
} else {
@@ -3200,50 +3240,18 @@ where
3200
3240
},
3201
3241
Some(chan) => chan
3202
3242
};
3203
- if !chan.context.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
3204
- // Note that the behavior here should be identical to the above block - we
3205
- // should NOT reveal the existence or non-existence of a private channel if
3206
- // we don't allow forwards outbound over them.
3207
- break Some(("Refusing to forward to a private channel based on our config.", 0x4000 | 10, None));
3208
- }
3209
- if chan.context.get_channel_type().supports_scid_privacy() && outgoing_scid != chan.context.outbound_scid_alias() {
3210
- // `option_scid_alias` (referred to in LDK as `scid_privacy`) means
3211
- // "refuse to forward unless the SCID alias was used", so we pretend
3212
- // we don't have the channel here.
3213
- break Some(("Refusing to forward over real channel SCID as our counterparty requested.", 0x4000 | 10, None));
3214
- }
3215
- let chan_update_opt = self.get_channel_update_for_onion(outgoing_scid, chan).ok();
3216
-
3217
- // Note that we could technically not return an error yet here and just hope
3218
- // that the connection is reestablished or monitor updated by the time we get
3219
- // around to doing the actual forward, but better to fail early if we can and
3220
- // hopefully an attacker trying to path-trace payments cannot make this occur
3221
- // on a small/per-node/per-channel scale.
3222
- if !chan.context.is_live() { // channel_disabled
3223
- // If the channel_update we're going to return is disabled (i.e. the
3224
- // peer has been disabled for some time), return `channel_disabled`,
3225
- // otherwise return `temporary_channel_failure`.
3226
- if chan_update_opt.as_ref().map(|u| u.contents.flags & 2 == 2).unwrap_or(false) {
3227
- break Some(("Forwarding channel has been disconnected for some time.", 0x1000 | 20, chan_update_opt));
3228
- } else {
3229
- break Some(("Forwarding channel is not in a ready state.", 0x1000 | 7, chan_update_opt));
3230
- }
3231
- }
3232
- if outgoing_amt_msat < chan.context.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
3233
- break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt));
3234
- }
3235
- if let Err((err, code)) = chan.htlc_satisfies_config(&msg, outgoing_amt_msat, outgoing_cltv_value) {
3236
- break Some((err, code, chan_update_opt));
3243
+ match self.can_forward_htlc_to_outgoing_channel(chan, msg, &next_packet_details) {
3244
+ Ok(chan_update_opt) => chan_update_opt,
3245
+ Err(e) => break Some(e),
3237
3246
}
3238
- chan_update_opt
3239
3247
} else {
3240
3248
None
3241
3249
};
3242
3250
3243
3251
let cur_height = self.best_block.read().unwrap().height + 1;
3244
3252
3245
3253
if let Err((err_msg, code)) = check_incoming_htlc_cltv(
3246
- cur_height, outgoing_cltv_value, msg.cltv_expiry
3254
+ cur_height, next_packet_details. outgoing_cltv_value, msg.cltv_expiry
3247
3255
) {
3248
3256
if code & 0x1000 != 0 && chan_update_opt.is_none() {
3249
3257
// We really should set `incorrect_cltv_expiry` here but as we're not
@@ -3262,7 +3270,7 @@ where
3262
3270
msg, counterparty_node_id, err, code, chan_update, next_hop.is_intro_node_blinded_forward(), &shared_secret
3263
3271
));
3264
3272
}
3265
- Ok((next_hop, shared_secret, Some(next_packet_pubkey)))
3273
+ Ok((next_hop, shared_secret, Some(next_packet_details. next_packet_pubkey)))
3266
3274
}
3267
3275
3268
3276
fn construct_pending_htlc_status<'a>(
0 commit comments