Skip to content

Commit f923e3b

Browse files
committed
lightningd: make sure we don't send channel_updates for unasked for channels.
We can decide to send an HTLC down a preferred channel which leads to the same peer as the one they asked for, but the spec is clear that you shouldn't send the "wrong" channel_update in that case. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 56ed48e commit f923e3b

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

lightningd/channel.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,10 +1302,32 @@ channel_scid_or_local_alias(const struct channel *chan)
13021302
return *chan->alias[LOCAL];
13031303
}
13041304

1305+
1306+
/* BOLT #4:
1307+
* An _intermediate hop_ MUST NOT, but the _final node_:
1308+
*...
1309+
* - if it returns a `channel_update`:
1310+
* - MUST set `short_channel_id` to the `short_channel_id` used
1311+
* by the incoming onion.
1312+
*/
1313+
/* So, if the scid doesn't match (redirect or we chose an equivalent
1314+
* channel), we simply don't return an update. */
13051315
const u8 *channel_update_for_error(const tal_t *ctx,
1316+
const struct htlc_in *hin,
13061317
struct channel *channel)
13071318
{
1308-
/* FIXME: Call directly from callers */
1319+
if (!hin || !hin->payload || !hin->payload->forward_channel)
1320+
return NULL;
1321+
1322+
/* We shouldn't have forwarded to the channel if it wasn't
1323+
* allowed to use that scid, so we can keep it simple here. */
1324+
if ((!channel->alias[LOCAL]
1325+
|| !short_channel_id_eq(*hin->payload->forward_channel, *channel->alias[LOCAL]))
1326+
&& (!channel->scid
1327+
|| !short_channel_id_eq(*hin->payload->forward_channel, *channel->scid))) {
1328+
return NULL;
1329+
}
1330+
13091331
return channel_gossip_update_for_error(ctx, channel);
13101332
}
13111333

lightningd/channel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,9 @@ void channel_set_billboard(struct channel *channel, bool perm,
965965
struct htlc_in *channel_has_htlc_in(struct channel *channel);
966966
struct htlc_out *channel_has_htlc_out(struct channel *channel);
967967

968+
/* hin can be NULL */
968969
const u8 *channel_update_for_error(const tal_t *ctx,
970+
const struct htlc_in *hin,
969971
struct channel *channel);
970972

971973
struct amount_msat htlc_max_possible_send(const struct channel *channel);

lightningd/peer_htlcs.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ static void destroy_hout_subd_died(struct htlc_out *hout)
570570

571571
hout->failmsg = towire_temporary_channel_failure(hout,
572572
channel_update_for_error(tmpctx,
573+
hout->in,
573574
hout->key.channel));
574575

575576
/* Assign a temporary state (we're about to free it!) so checks
@@ -619,7 +620,7 @@ static void rcvd_htlc_reply(struct subd *subd, const u8 *msg, const int *fds UNU
619620
*/
620621
/* We still append the channel_update (if we have one!) FIXME: provide an option? */
621622
if (fromwire_peektype(failmsg) & UPDATE) {
622-
const u8 *update = channel_update_for_error(tmpctx, hout->key.channel);
623+
const u8 *update = channel_update_for_error(tmpctx, hout->in, hout->key.channel);
623624
towire(&failmsg, update, tal_bytelen(update));
624625
}
625626
hout->failmsg = tal_steal(hout, failmsg);
@@ -725,7 +726,7 @@ const u8 *send_htlc_out(const tal_t *ctx,
725726
log_info(out->log, "Attempt to send HTLC but unowned (%s)",
726727
channel_state_name(out));
727728
return towire_temporary_channel_failure(ctx,
728-
channel_update_for_error(tmpctx, out));
729+
channel_update_for_error(tmpctx, in, out));
729730
}
730731

731732
/* Note: we allow outgoing HTLCs before sync, for fast startup. */
@@ -865,7 +866,7 @@ static void forward_htlc(struct htlc_in *hin,
865866
next->old_feerate_ppm)) {
866867
failmsg = towire_fee_insufficient(tmpctx, hin->msat,
867868
channel_update_for_error(tmpctx,
868-
next));
869+
hin, next));
869870
goto fail;
870871
}
871872
log_info(hin->key.channel->log,
@@ -879,7 +880,7 @@ static void forward_htlc(struct htlc_in *hin,
879880
|| amount_msat_less(amt_to_forward, next->old_htlc_minimum_msat)
880881
|| amount_msat_greater(amt_to_forward, next->old_htlc_maximum_msat)) {
881882
failmsg = towire_temporary_channel_failure(tmpctx,
882-
channel_update_for_error(tmpctx, next));
883+
channel_update_for_error(tmpctx, hin, next));
883884
goto fail;
884885
}
885886
log_info(hin->key.channel->log,
@@ -889,7 +890,7 @@ static void forward_htlc(struct htlc_in *hin,
889890
if (!check_cltv(hin, cltv_expiry, outgoing_cltv_value,
890891
ld->config.cltv_expiry_delta)) {
891892
failmsg = towire_incorrect_cltv_expiry(tmpctx, cltv_expiry,
892-
channel_update_for_error(tmpctx, next));
893+
channel_update_for_error(tmpctx, hin, next));
893894
goto fail;
894895
}
895896

@@ -908,7 +909,7 @@ static void forward_htlc(struct htlc_in *hin,
908909
outgoing_cltv_value,
909910
get_block_height(ld->topology));
910911
failmsg = towire_expiry_too_soon(tmpctx,
911-
channel_update_for_error(tmpctx, next));
912+
channel_update_for_error(tmpctx, hin, next));
912913
goto fail;
913914
}
914915

0 commit comments

Comments
 (0)