| 1453 | } |
| 1454 | |
| 1455 | static bool peer_failed_our_htlc(struct channel *channel, |
| 1456 | const struct failed_htlc *failed) |
| 1457 | { |
| 1458 | struct htlc_out *hout; |
| 1459 | struct lightningd *ld = channel->peer->ld; |
| 1460 | |
| 1461 | hout = find_htlc_out(&ld->htlcs_out, channel, failed->id); |
| 1462 | if (!hout) { |
| 1463 | channel_internal_error(channel, |
| 1464 | "failed_our_htlc unknown htlc %"PRIu64, |
| 1465 | failed->id); |
| 1466 | return false; |
| 1467 | } |
| 1468 | |
| 1469 | if (!htlc_out_update_state(channel, hout, RCVD_REMOVE_COMMIT)) |
| 1470 | return false; |
| 1471 | |
| 1472 | if (failed->sha256_of_onion) { |
| 1473 | struct sha256 our_sha256_of_onion; |
| 1474 | u8 *failmsg; |
| 1475 | |
| 1476 | /* BOLT #2: |
| 1477 | * |
| 1478 | * - if the `sha256_of_onion` in `update_fail_malformed_htlc` |
| 1479 | * doesn't match the onion it sent: |
| 1480 | * - MAY retry or choose an alternate error response. |
| 1481 | */ |
| 1482 | sha256(&our_sha256_of_onion, hout->onion_routing_packet, |
| 1483 | sizeof(hout->onion_routing_packet)); |
| 1484 | if (!sha256_eq(failed->sha256_of_onion, &our_sha256_of_onion)) |
| 1485 | log_unusual(channel->log, |
| 1486 | "update_fail_malformed_htlc for bad onion" |
| 1487 | " for htlc with id %"PRIu64".", |
| 1488 | hout->key.id); |
| 1489 | |
| 1490 | /* BOLT #2: |
| 1491 | * |
| 1492 | * - otherwise, a receiving node which has an outgoing HTLC |
| 1493 | * canceled by `update_fail_malformed_htlc`: |
| 1494 | * |
| 1495 | * - MUST return an error in the `update_fail_htlc` |
| 1496 | * sent to the link which originally sent the HTLC, using the |
| 1497 | * `failure_code` given and setting the data to |
| 1498 | * `sha256_of_onion`. |
| 1499 | */ |
| 1500 | /* All badonion codes are the same form, so we make them |
| 1501 | * manually, which covers any unknown cases too. Grep fodder: |
| 1502 | * towire_invalid_onion_version, towire_invalid_onion_hmac, |
| 1503 | * towire_invalid_onion_key. */ |
| 1504 | failmsg = tal_arr(hout, u8, 0); |
| 1505 | towire_u16(&failmsg, failed->badonion); |
| 1506 | towire_sha256(&failmsg, failed->sha256_of_onion); |
| 1507 | hout->failmsg = failmsg; |
| 1508 | } else { |
| 1509 | hout->failonion = dup_onionreply(hout, failed->onion); |
| 1510 | } |
| 1511 | |
| 1512 | log_debug(channel->log, "Our HTLC %"PRIu64" failed (%u)", failed->id, |
no test coverage detected