Skip to content

Commit f5ced1d

Browse files
rustyrussellcdecker
authored andcommitted
channeld: handle malformed onion properly.
When the next node tells us the onion is malformed, we now actually report the failcode to lightningd (rather than generating an invalid error as we do now). We could generate the onion at this point, but except we don't know the shared secret; we'd have to plumb that through from the incoming channeld's HTLC. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 85ce1ff commit f5ced1d

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ changes.
4040
- JSON API: commands are once again read even if one hasn't responded yet (broken in 0.6.2).
4141
- Protocol: allow lnd to send `update_fee` before `funding_locked`.
4242
- Protocol: fix limit on how much funder can send (fee was 1000x too small)
43+
- Protocol: don't send invalid onion errors if peer says onion was bad.
4344
- pylightning: handle multiple simultanous RPC replies reliably.
4445

4546

channeld/channeld.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,13 +1240,13 @@ static u8 *got_commitsig_msg(const tal_t *ctx,
12401240
} else if (htlc->state == RCVD_REMOVE_COMMIT) {
12411241
if (htlc->r) {
12421242
struct fulfilled_htlc *f;
1243-
assert(!htlc->fail);
1243+
assert(!htlc->fail && !htlc->failcode);
12441244
f = tal_arr_expand(&fulfilled);
12451245
f->id = htlc->id;
12461246
f->payment_preimage = *htlc->r;
12471247
} else {
12481248
struct failed_htlc *f;
1249-
assert(htlc->fail);
1249+
assert(htlc->fail || htlc->failcode);
12501250
f = tal(failed, struct failed_htlc);
12511251
f->id = htlc->id;
12521252
f->failcode = htlc->failcode;
@@ -1585,7 +1585,6 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
15851585
struct sha256 sha256_of_onion;
15861586
u16 failure_code;
15871587
struct htlc *htlc;
1588-
u8 *fail;
15891588

15901589
if (!fromwire_update_fail_malformed_htlc(msg, &channel_id, &id,
15911590
&sha256_of_onion,
@@ -1602,12 +1601,16 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
16021601
* `update_fail_malformed_htlc`:
16031602
* - MUST fail the channel.
16041603
*/
1605-
if (!(failure_code & BADONION)) {
1604+
/* We only handle these cases. */
1605+
if (failure_code != WIRE_INVALID_ONION_VERSION
1606+
&& failure_code != WIRE_INVALID_ONION_HMAC
1607+
&& failure_code != WIRE_INVALID_ONION_KEY) {
16061608
peer_failed(&peer->cs,
16071609
&peer->channel_id,
16081610
"Bad update_fail_malformed_htlc failure code %u",
16091611
failure_code);
16101612
}
1613+
assert(failure_code & BADONION);
16111614

16121615
e = channel_fail_htlc(peer->channel, LOCAL, id, &htlc);
16131616
switch (e) {
@@ -1620,20 +1623,9 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
16201623
* - MAY retry or choose an alternate error response.
16211624
*/
16221625

1623-
/* BOLT #2:
1624-
*
1625-
* - otherwise, a receiving node which has an outgoing HTLC
1626-
* canceled by `update_fail_malformed_htlc`:
1627-
*
1628-
* - MUST return an error in the `update_fail_htlc` sent to
1629-
* the link which originally sent the HTLC, using the
1630-
* `failure_code` given and setting the data to
1631-
* `sha256_of_onion`.
1632-
*/
1633-
fail = tal_arr(htlc, u8, 0);
1634-
towire_u16(&fail, failure_code);
1635-
towire_sha256(&fail, &sha256_of_onion);
1636-
htlc->fail = fail;
1626+
/* This is the only case where we set failcode for a non-local
1627+
* failure; in a way, it is, since we have to report it. */
1628+
htlc->failcode = failure_code;
16371629
start_commit_timer(peer);
16381630
return;
16391631
case CHANNEL_ERR_NO_SUCH_ID:

0 commit comments

Comments
 (0)