Skip to content

Commit 554c3ec

Browse files
rustyrussellcdecker
authored andcommitted
channeld: process onion packet ourselves.
This covers all the cases where an onion can be malformed; this means we know in advance that it's bad. That allows us to distinguish two cases: where lightningd rejects the onion as bad, and where the next peer rejects the next onion as bad. Both of those (will) set failcode to one of the BADONION values. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 59febcb commit 554c3ec

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

channeld/channeld.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,18 +528,18 @@ static void handle_peer_announcement_signatures(struct peer *peer, const u8 *msg
528528
}
529529

530530
static struct secret *get_shared_secret(const tal_t *ctx,
531-
const struct htlc *htlc)
531+
const struct htlc *htlc,
532+
enum onion_type *why_bad)
532533
{
533534
struct pubkey ephemeral;
534535
struct onionpacket *op;
535536
struct secret *secret = tal(ctx, struct secret);
536537
const u8 *msg;
537-
/* FIXME: Use this! */
538-
enum onion_type why_bad;
538+
struct route_step *rs;
539539

540540
/* We unwrap the onion now. */
541541
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE,
542-
&why_bad);
542+
why_bad);
543543
if (!op)
544544
return tal_free(secret);
545545

@@ -549,6 +549,16 @@ static struct secret *get_shared_secret(const tal_t *ctx,
549549
if (!fromwire_hsm_ecdh_resp(msg, secret))
550550
status_failed(STATUS_FAIL_HSM_IO, "Reading ecdh response");
551551

552+
/* We make sure we can parse onion packet, so we know if shared secret
553+
* is actually valid (this checks hmac). */
554+
rs = process_onionpacket(tmpctx, op, secret->data,
555+
htlc->rhash.u.u8,
556+
sizeof(htlc->rhash));
557+
if (!rs) {
558+
*why_bad = WIRE_INVALID_ONION_HMAC;
559+
return tal_free(secret);
560+
}
561+
552562
return secret;
553563
}
554564

@@ -581,7 +591,8 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
581591

582592
/* If this is wrong, we don't complain yet; when it's confirmed we'll
583593
* send it to the master which handles all HTLC failures. */
584-
htlc->shared_secret = get_shared_secret(htlc, htlc);
594+
htlc->shared_secret = get_shared_secret(htlc, htlc,
595+
&htlc->why_bad_onion);
585596
}
586597

587598
static void handle_peer_feechange(struct peer *peer, const u8 *msg)
@@ -2593,7 +2604,8 @@ static void init_shared_secrets(struct channel *channel,
25932604
continue;
25942605

25952606
htlc = channel_get_htlc(channel, REMOTE, htlcs[i].id);
2596-
htlc->shared_secret = get_shared_secret(htlc, htlc);
2607+
htlc->shared_secret = get_shared_secret(htlc, htlc,
2608+
&htlc->why_bad_onion);
25972609
}
25982610
}
25992611

channeld/channeld_htlc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct htlc {
2323

2424
/* The routing shared secret (only for incoming) */
2525
struct secret *shared_secret;
26+
/* If incoming HTLC has shared_secret, this is which BADONION error */
27+
enum onion_type why_bad_onion;
2628

2729
/* FIXME: We could union these together: */
2830
/* Routing information sent with this HTLC. */

lightningd/peer_htlcs.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,8 @@ static bool peer_accepted_htlc(struct channel *channel,
646646
goto out;
647647
}
648648

649+
/* FIXME: Have channeld hand through just the route_step! */
650+
649651
/* channeld tests this, so it should pass. */
650652
op = parse_onionpacket(tmpctx, hin->onion_routing_packet,
651653
sizeof(hin->onion_routing_packet),
@@ -663,8 +665,11 @@ static bool peer_accepted_htlc(struct channel *channel,
663665
hin->payment_hash.u.u8,
664666
sizeof(hin->payment_hash));
665667
if (!rs) {
666-
*failcode = WIRE_INVALID_ONION_HMAC;
667-
goto out;
668+
channel_internal_error(channel,
669+
"bad process_onionpacket in got_revoke: %s",
670+
tal_hexstr(channel, hin->onion_routing_packet,
671+
sizeof(hin->onion_routing_packet)));
672+
return false;
668673
}
669674

670675
/* Unknown realm isn't a bad onion, it's a normal failure. */

0 commit comments

Comments
 (0)