Skip to content

Commit 4808790

Browse files
committed
Never generate a BroadcastChannelUpdate for priv channels
Currently we always generate a `MessageSendEvent::BroadcastChannelUpdate` when a channel is closed even if the channel is private. Our immediate peers should ignore such messages as they haven't seen a corresponding `channel_announcement`, but we are still giving up some privacy by informing our immediate peers of which channels were ours. Here we split `ChannelManager::get_channel_update` into a `get_channel_update_for_broadcast` and `get_channel_update_for_unicast`. The first is used when we are broadcasting a `channel_update`, allowing us to refuse to do so for private channels. The second is used when failing a payment (in which case the recipient has already shown that they are aware of the channel so no such privacy concerns exist).
1 parent bfd9646 commit 4808790

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -781,15 +781,15 @@ macro_rules! convert_chan_err {
781781
$short_to_id.remove(&short_id);
782782
}
783783
let shutdown_res = $channel.force_shutdown(true);
784-
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
784+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
785785
},
786786
ChannelError::CloseDelayBroadcast(msg) => {
787787
log_error!($self.logger, "Channel {} need to be shutdown but closing transactions not broadcast due to {}", log_bytes!($channel_id[..]), msg);
788788
if let Some(short_id) = $channel.get_short_channel_id() {
789789
$short_to_id.remove(&short_id);
790790
}
791791
let shutdown_res = $channel.force_shutdown(false);
792-
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update(&$channel).ok()))
792+
(true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
793793
}
794794
}
795795
}
@@ -845,7 +845,8 @@ macro_rules! handle_monitor_err {
845845
// splitting hairs we'd prefer to claim payments that were to us, but we haven't
846846
// given up the preimage yet, so might as well just wait until the payment is
847847
// retried, avoiding the on-chain fees.
848-
let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id, $chan.force_shutdown(true), $self.get_channel_update(&$chan).ok()));
848+
let res: Result<(), _> = Err(MsgHandleErrInternal::from_finish_shutdown("ChannelMonitor storage failure".to_owned(), *$chan_id,
849+
$chan.force_shutdown(true), $self.get_channel_update_for_broadcast(&$chan).ok() ));
849850
(res, true)
850851
},
851852
ChannelMonitorUpdateErr::TemporaryFailure => {
@@ -1225,9 +1226,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
12251226
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
12261227
}
12271228
let chan_update = if let Some(chan) = chan_option {
1228-
if let Ok(update) = self.get_channel_update(&chan) {
1229-
Some(update)
1230-
} else { None }
1229+
self.get_channel_update_for_broadcast(&chan).ok()
12311230
} else { None };
12321231

12331232
if let Some(update) = chan_update {
@@ -1276,7 +1275,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
12761275
};
12771276
log_trace!(self.logger, "Force-closing channel {}", log_bytes!(channel_id[..]));
12781277
self.finish_force_close_channel(chan.force_shutdown(true));
1279-
if let Ok(update) = self.get_channel_update(&chan) {
1278+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
12801279
let mut channel_state = self.channel_state.lock().unwrap();
12811280
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
12821281
msg: update
@@ -1536,31 +1535,31 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15361535
// hopefully an attacker trying to path-trace payments cannot make this occur
15371536
// on a small/per-node/per-channel scale.
15381537
if !chan.is_live() { // channel_disabled
1539-
break Some(("Forwarding channel is not in a ready state.", 0x1000 | 20, Some(self.get_channel_update(chan).unwrap())));
1538+
break Some(("Forwarding channel is not in a ready state.", 0x1000 | 20, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15401539
}
15411540
if *amt_to_forward < chan.get_counterparty_htlc_minimum_msat() { // amount_below_minimum
1542-
break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, Some(self.get_channel_update(chan).unwrap())));
1541+
break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15431542
}
15441543
let fee = amt_to_forward.checked_mul(chan.get_fee_proportional_millionths() as u64).and_then(|prop_fee| { (prop_fee / 1000000).checked_add(chan.get_holder_fee_base_msat(&self.fee_estimator) as u64) });
15451544
if fee.is_none() || msg.amount_msat < fee.unwrap() || (msg.amount_msat - fee.unwrap()) < *amt_to_forward { // fee_insufficient
1546-
break Some(("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, Some(self.get_channel_update(chan).unwrap())));
1545+
break Some(("Prior hop has deviated from specified fees parameters or origin node has obsolete ones", 0x1000 | 12, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15471546
}
15481547
if (msg.cltv_expiry as u64) < (*outgoing_cltv_value) as u64 + chan.get_cltv_expiry_delta() as u64 { // incorrect_cltv_expiry
1549-
break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update(chan).unwrap())));
1548+
break Some(("Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta", 0x1000 | 13, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15501549
}
15511550
let cur_height = self.best_block.read().unwrap().height() + 1;
15521551
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now, but we want to be robust wrt to counterparty
15531552
// packet sanitization (see HTLC_FAIL_BACK_BUFFER rational)
15541553
if msg.cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
1555-
break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
1554+
break Some(("CLTV expiry is too close", 0x1000 | 14, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15561555
}
15571556
if msg.cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
15581557
break Some(("CLTV expiry is too far in the future", 21, None));
15591558
}
15601559
// In theory, we would be safe against unitentional channel-closure, if we only required a margin of LATENCY_GRACE_PERIOD_BLOCKS.
15611560
// But, to be safe against policy reception, we use a longuer delay.
15621561
if (*outgoing_cltv_value) as u64 <= (cur_height + HTLC_FAIL_BACK_BUFFER) as u64 {
1563-
break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, Some(self.get_channel_update(chan).unwrap())));
1562+
break Some(("Outgoing CLTV value is too soon", 0x1000 | 14, Some(self.get_channel_update_for_unicast(chan).unwrap())));
15641563
}
15651564

15661565
break None;
@@ -1588,9 +1587,27 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15881587
(pending_forward_info, channel_state.unwrap())
15891588
}
15901589

1591-
/// only fails if the channel does not yet have an assigned short_id
1590+
/// Gets the current channel_update for the given channel. This first checks if the channel is
1591+
/// public, and thus should be called whenever the result is going to be passed out in a
1592+
/// [`MessageSendEvent::BroadcastChannelUpdate`] event.
1593+
///
1594+
/// May be called with channel_state already locked!
1595+
fn get_channel_update_for_broadcast(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
1596+
if !chan.should_announce() {
1597+
return Err(LightningError {
1598+
err: "Cannot broadcast a channel_update for a private channel".to_owned(),
1599+
action: msgs::ErrorAction::IgnoreError
1600+
});
1601+
}
1602+
self.get_channel_update_for_unicast(chan)
1603+
}
1604+
1605+
/// Gets the current channel_update for the given channel. This does not check if the channel
1606+
/// is public (only returning an Err if the channel does not yet have an assigned short_id),
1607+
/// and thus MUST NOT be called unless the recipient of the resulting message has already
1608+
/// provided evidence that they know about the existence of the channel.
15921609
/// May be called with channel_state already locked!
1593-
fn get_channel_update(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
1610+
fn get_channel_update_for_unicast(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
15941611
let short_channel_id = match chan.get_short_channel_id() {
15951612
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
15961613
Some(id) => id,
@@ -1982,7 +1999,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
19821999
if let Some(msg) = chan.get_signed_channel_announcement(&self.our_network_key, self.get_our_node_id(), self.genesis_hash.clone()) {
19832000
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
19842001
msg,
1985-
update_msg: match self.get_channel_update(chan) {
2002+
update_msg: match self.get_channel_update_for_broadcast(chan) {
19862003
Ok(msg) => msg,
19872004
Err(_) => continue,
19882005
},
@@ -2074,7 +2091,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20742091
} else {
20752092
panic!("Stated return value requirements in send_htlc() were not met");
20762093
}
2077-
let chan_update = self.get_channel_update(chan.get()).unwrap();
2094+
let chan_update = self.get_channel_update_for_unicast(chan.get()).unwrap();
20782095
failed_forwards.push((htlc_source, payment_hash,
20792096
HTLCFailReason::Reason { failure_code: 0x1000 | 7, data: chan_update.encode_with_len() }
20802097
));
@@ -2146,7 +2163,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21462163
if let Some(short_id) = channel.get_short_channel_id() {
21472164
channel_state.short_to_id.remove(&short_id);
21482165
}
2149-
Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update(&channel).ok()))
2166+
Err(MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, channel.force_shutdown(true), self.get_channel_update_for_broadcast(&channel).ok()))
21502167
},
21512168
ChannelError::CloseDelayBroadcast(_) => { panic!("Wait is only generated on receipt of channel_reestablish, which is handled by try_chan_entry, we don't bother to support it here"); }
21522169
};
@@ -2347,7 +2364,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23472364
ChannelUpdateStatus::DisabledStaged if chan.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::Enabled),
23482365
ChannelUpdateStatus::EnabledStaged if !chan.is_live() => chan.set_channel_update_status(ChannelUpdateStatus::Disabled),
23492366
ChannelUpdateStatus::DisabledStaged if !chan.is_live() => {
2350-
if let Ok(update) = self.get_channel_update(&chan) {
2367+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
23512368
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
23522369
msg: update
23532370
});
@@ -2356,7 +2373,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23562373
chan.set_channel_update_status(ChannelUpdateStatus::Disabled);
23572374
},
23582375
ChannelUpdateStatus::EnabledStaged if chan.is_live() => {
2359-
if let Ok(update) = self.get_channel_update(&chan) {
2376+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
23602377
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
23612378
msg: update
23622379
});
@@ -2406,7 +2423,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24062423
let (failure_code, onion_failure_data) =
24072424
match self.channel_state.lock().unwrap().by_id.entry(channel_id) {
24082425
hash_map::Entry::Occupied(chan_entry) => {
2409-
if let Ok(upd) = self.get_channel_update(&chan_entry.get()) {
2426+
if let Ok(upd) = self.get_channel_update_for_unicast(&chan_entry.get()) {
24102427
(0x1000|7, upd.encode_with_len())
24112428
} else {
24122429
(0x4000|10, Vec::new())
@@ -2987,7 +3004,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29873004
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_source.0, &htlc_source.1, HTLCFailReason::Reason { failure_code: 0x4000 | 8, data: Vec::new() });
29883005
}
29893006
if let Some(chan) = chan_option {
2990-
if let Ok(update) = self.get_channel_update(&chan) {
3007+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
29913008
let mut channel_state = self.channel_state.lock().unwrap();
29923009
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
29933010
msg: update
@@ -3033,7 +3050,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30333050
self.tx_broadcaster.broadcast_transaction(&broadcast_tx);
30343051
}
30353052
if let Some(chan) = chan_option {
3036-
if let Ok(update) = self.get_channel_update(&chan) {
3053+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
30373054
let mut channel_state = self.channel_state.lock().unwrap();
30383055
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
30393056
msg: update
@@ -3071,7 +3088,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30713088
// want to reject the new HTLC and fail it backwards instead of forwarding.
30723089
match pending_forward_info {
30733090
PendingHTLCStatus::Forward(PendingHTLCInfo { ref incoming_shared_secret, .. }) => {
3074-
let reason = if let Ok(upd) = self.get_channel_update(chan) {
3091+
let reason = if let Ok(upd) = self.get_channel_update_for_unicast(chan) {
30753092
onion_utils::build_first_hop_failure_packet(incoming_shared_secret, error_code, &{
30763093
let mut res = Vec::with_capacity(8 + 128);
30773094
// TODO: underspecified, follow https://github.com/lightningnetwork/lightning-rfc/issues/791
@@ -3333,7 +3350,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33333350

33343351
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
33353352
msg: try_chan_entry!(self, chan.get_mut().announcement_signatures(&self.our_network_key, self.get_our_node_id(), self.genesis_hash.clone(), msg), channel_state, chan),
3336-
update_msg: self.get_channel_update(chan.get()).unwrap(), // can only fail if we're not in a ready state
3353+
// Note that announcement_signatures fails if the channel cannot be announced,
3354+
// so get_channel_update_for_broadcast will never fail by the time we get here.
3355+
update_msg: self.get_channel_update_for_broadcast(chan.get()).unwrap(),
33373356
});
33383357
},
33393358
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel".to_owned(), msg.channel_id))
@@ -3478,7 +3497,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
34783497
short_to_id.remove(&short_id);
34793498
}
34803499
failed_channels.push(chan.force_shutdown(false));
3481-
if let Ok(update) = self.get_channel_update(&chan) {
3500+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
34823501
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
34833502
msg: update
34843503
});
@@ -3917,7 +3936,7 @@ where
39173936
let res = f(channel);
39183937
if let Ok((chan_res, mut timed_out_pending_htlcs)) = res {
39193938
for (source, payment_hash) in timed_out_pending_htlcs.drain(..) {
3920-
let chan_update = self.get_channel_update(&channel).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
3939+
let chan_update = self.get_channel_update_for_unicast(&channel).map(|u| u.encode_with_len()).unwrap(); // Cannot add/recv HTLCs before we have a short_id so unwrap is safe
39213940
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::Reason {
39223941
failure_code: 0x1000 | 14, // expiry_too_soon, or at least it is now
39233942
data: chan_update,
@@ -3946,7 +3965,7 @@ where
39463965
// It looks like our counterparty went on-chain or funding transaction was
39473966
// reorged out of the main chain. Close the channel.
39483967
failed_channels.push(channel.force_shutdown(true));
3949-
if let Ok(update) = self.get_channel_update(&channel) {
3968+
if let Ok(update) = self.get_channel_update_for_broadcast(&channel) {
39503969
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
39513970
msg: update
39523971
});
@@ -4125,7 +4144,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
41254144
short_to_id.remove(&short_id);
41264145
}
41274146
failed_channels.push(chan.force_shutdown(true));
4128-
if let Ok(update) = self.get_channel_update(&chan) {
4147+
if let Ok(update) = self.get_channel_update_for_broadcast(&chan) {
41294148
pending_msg_events.push(events::MessageSendEvent::BroadcastChannelUpdate {
41304149
msg: update
41314150
});

0 commit comments

Comments
 (0)