2019-08-31 00:11:57 +03:00
|
|
|
package hop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2019-09-05 16:05:38 +03:00
|
|
|
"fmt"
|
2019-08-31 00:11:57 +03:00
|
|
|
"io"
|
|
|
|
|
2019-10-31 07:21:10 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2019-08-31 00:11:57 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/record"
|
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
|
|
|
)
|
|
|
|
|
2019-10-31 07:19:08 +03:00
|
|
|
// PayloadViolation is an enum encapsulating the possible invalid payload
|
|
|
|
// violations that can occur when processing or validating a payload.
|
|
|
|
type PayloadViolation byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// OmittedViolation indicates that a type was expected to be found the
|
|
|
|
// payload but was absent.
|
|
|
|
OmittedViolation PayloadViolation = iota
|
|
|
|
|
|
|
|
// IncludedViolation indicates that a type was expected to be omitted
|
|
|
|
// from the payload but was present.
|
|
|
|
IncludedViolation
|
|
|
|
|
|
|
|
// RequiredViolation indicates that an unknown even type was found in
|
|
|
|
// the payload that we could not process.
|
|
|
|
RequiredViolation
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns a human-readable description of the violation as a verb.
|
|
|
|
func (v PayloadViolation) String() string {
|
|
|
|
switch v {
|
|
|
|
case OmittedViolation:
|
|
|
|
return "omitted"
|
|
|
|
|
|
|
|
case IncludedViolation:
|
|
|
|
return "included"
|
|
|
|
|
|
|
|
case RequiredViolation:
|
|
|
|
return "required"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "unknown violation"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:05:38 +03:00
|
|
|
// ErrInvalidPayload is an error returned when a parsed onion payload either
|
|
|
|
// included or omitted incorrect records for a particular hop type.
|
|
|
|
type ErrInvalidPayload struct {
|
|
|
|
// Type the record's type that cause the violation.
|
|
|
|
Type tlv.Type
|
|
|
|
|
2019-10-31 07:19:08 +03:00
|
|
|
// Violation is an enum indicating the type of violation detected in
|
|
|
|
// processing Type.
|
|
|
|
Violation PayloadViolation
|
2019-09-05 16:05:38 +03:00
|
|
|
|
|
|
|
// FinalHop if true, indicates that the violation is for the final hop
|
|
|
|
// in the route (identified by next hop id), otherwise the violation is
|
|
|
|
// for an intermediate hop.
|
|
|
|
FinalHop bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a human-readable description of the invalid payload error.
|
|
|
|
func (e ErrInvalidPayload) Error() string {
|
|
|
|
hopType := "intermediate"
|
|
|
|
if e.FinalHop {
|
|
|
|
hopType = "final"
|
|
|
|
}
|
|
|
|
|
2019-10-31 07:19:08 +03:00
|
|
|
return fmt.Sprintf("onion payload for %s hop %v record with type %d",
|
|
|
|
hopType, e.Violation, e.Type)
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:11:57 +03:00
|
|
|
// Payload encapsulates all information delivered to a hop in an onion payload.
|
|
|
|
// A Hop can represent either a TLV or legacy payload. The primary forwarding
|
|
|
|
// instruction can be accessed via ForwardingInfo, and additional records can be
|
|
|
|
// accessed by other member functions.
|
|
|
|
type Payload struct {
|
|
|
|
// FwdInfo holds the basic parameters required for HTLC forwarding, e.g.
|
|
|
|
// amount, cltv, and next hop.
|
|
|
|
FwdInfo ForwardingInfo
|
2019-11-05 02:10:00 +03:00
|
|
|
|
|
|
|
// MPP holds the info provided in an option_mpp record when parsed from
|
|
|
|
// a TLV onion payload.
|
|
|
|
MPP *record.MPP
|
2019-11-19 14:32:56 +03:00
|
|
|
|
2021-03-03 20:54:11 +03:00
|
|
|
// AMP holds the info provided in an option_amp record when parsed from
|
|
|
|
// a TLV onion payload.
|
|
|
|
AMP *record.AMP
|
|
|
|
|
2019-11-19 14:32:56 +03:00
|
|
|
// customRecords are user-defined records in the custom type range that
|
|
|
|
// were included in the payload.
|
2019-12-12 02:01:55 +03:00
|
|
|
customRecords record.CustomSet
|
2019-08-31 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLegacyPayload builds a Payload from the amount, cltv, and next hop
|
|
|
|
// parameters provided by leegacy onion payloads.
|
|
|
|
func NewLegacyPayload(f *sphinx.HopData) *Payload {
|
|
|
|
nextHop := binary.BigEndian.Uint64(f.NextAddress[:])
|
|
|
|
|
|
|
|
return &Payload{
|
|
|
|
FwdInfo: ForwardingInfo{
|
|
|
|
Network: BitcoinNetwork,
|
|
|
|
NextHop: lnwire.NewShortChanIDFromInt(nextHop),
|
|
|
|
AmountToForward: lnwire.MilliSatoshi(f.ForwardAmount),
|
|
|
|
OutgoingCTLV: f.OutgoingCltv,
|
|
|
|
},
|
2019-12-12 02:01:55 +03:00
|
|
|
customRecords: make(record.CustomSet),
|
2019-08-31 00:11:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPayloadFromReader builds a new Hop from the passed io.Reader. The reader
|
|
|
|
// should correspond to the bytes encapsulated in a TLV onion payload.
|
|
|
|
func NewPayloadFromReader(r io.Reader) (*Payload, error) {
|
|
|
|
var (
|
|
|
|
cid uint64
|
|
|
|
amt uint64
|
|
|
|
cltv uint32
|
2019-11-05 02:10:00 +03:00
|
|
|
mpp = &record.MPP{}
|
2019-08-31 00:11:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
tlvStream, err := tlv.NewStream(
|
|
|
|
record.NewAmtToFwdRecord(&amt),
|
|
|
|
record.NewLockTimeRecord(&cltv),
|
|
|
|
record.NewNextHopIDRecord(&cid),
|
2019-11-05 02:10:00 +03:00
|
|
|
mpp.Record(),
|
2019-08-31 00:11:57 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:05:38 +03:00
|
|
|
parsedTypes, err := tlvStream.DecodeWithParsedTypes(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate whether the sender properly included or omitted tlv records
|
|
|
|
// in accordance with BOLT 04.
|
2019-10-31 07:21:10 +03:00
|
|
|
nextHop := lnwire.NewShortChanIDFromInt(cid)
|
2019-09-05 16:05:38 +03:00
|
|
|
err = ValidateParsedPayloadTypes(parsedTypes, nextHop)
|
2019-08-31 00:11:57 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:06:49 +03:00
|
|
|
// Check for violation of the rules for mandatory fields.
|
|
|
|
violatingType := getMinRequiredViolation(parsedTypes)
|
|
|
|
if violatingType != nil {
|
|
|
|
return nil, ErrInvalidPayload{
|
|
|
|
Type: *violatingType,
|
|
|
|
Violation: RequiredViolation,
|
|
|
|
FinalHop: nextHop == Exit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 02:10:00 +03:00
|
|
|
// If no MPP field was parsed, set the MPP field on the resulting
|
|
|
|
// payload to nil.
|
|
|
|
if _, ok := parsedTypes[record.MPPOnionType]; !ok {
|
|
|
|
mpp = nil
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:32:56 +03:00
|
|
|
// Filter out the custom records.
|
|
|
|
customRecords := NewCustomRecords(parsedTypes)
|
|
|
|
|
2019-08-31 00:11:57 +03:00
|
|
|
return &Payload{
|
|
|
|
FwdInfo: ForwardingInfo{
|
|
|
|
Network: BitcoinNetwork,
|
2019-09-05 16:05:38 +03:00
|
|
|
NextHop: nextHop,
|
2019-08-31 00:11:57 +03:00
|
|
|
AmountToForward: lnwire.MilliSatoshi(amt),
|
|
|
|
OutgoingCTLV: cltv,
|
|
|
|
},
|
2019-11-19 14:32:56 +03:00
|
|
|
MPP: mpp,
|
|
|
|
customRecords: customRecords,
|
2019-08-31 00:11:57 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForwardingInfo returns the basic parameters required for HTLC forwarding,
|
|
|
|
// e.g. amount, cltv, and next hop.
|
|
|
|
func (h *Payload) ForwardingInfo() ForwardingInfo {
|
|
|
|
return h.FwdInfo
|
|
|
|
}
|
2019-09-05 16:05:38 +03:00
|
|
|
|
2019-11-19 14:32:56 +03:00
|
|
|
// NewCustomRecords filters the types parsed from the tlv stream for custom
|
|
|
|
// records.
|
2019-12-12 02:01:55 +03:00
|
|
|
func NewCustomRecords(parsedTypes tlv.TypeMap) record.CustomSet {
|
|
|
|
customRecords := make(record.CustomSet)
|
2019-11-19 14:32:56 +03:00
|
|
|
for t, parseResult := range parsedTypes {
|
2019-12-12 02:01:55 +03:00
|
|
|
if parseResult == nil || t < record.CustomTypeStart {
|
2019-11-19 14:32:56 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
customRecords[uint64(t)] = parseResult
|
|
|
|
}
|
|
|
|
return customRecords
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:05:38 +03:00
|
|
|
// ValidateParsedPayloadTypes checks the types parsed from a hop payload to
|
|
|
|
// ensure that the proper fields are either included or omitted. The finalHop
|
|
|
|
// boolean should be true if the payload was parsed for an exit hop. The
|
|
|
|
// requirements for this method are described in BOLT 04.
|
2019-11-19 14:05:20 +03:00
|
|
|
func ValidateParsedPayloadTypes(parsedTypes tlv.TypeMap,
|
2019-09-05 16:05:38 +03:00
|
|
|
nextHop lnwire.ShortChannelID) error {
|
|
|
|
|
|
|
|
isFinalHop := nextHop == Exit
|
|
|
|
|
|
|
|
_, hasAmt := parsedTypes[record.AmtOnionType]
|
|
|
|
_, hasLockTime := parsedTypes[record.LockTimeOnionType]
|
|
|
|
_, hasNextHop := parsedTypes[record.NextHopOnionType]
|
2019-11-05 02:10:00 +03:00
|
|
|
_, hasMPP := parsedTypes[record.MPPOnionType]
|
2019-09-05 16:05:38 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
// All hops must include an amount to forward.
|
|
|
|
case !hasAmt:
|
|
|
|
return ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.AmtOnionType,
|
|
|
|
Violation: OmittedViolation,
|
|
|
|
FinalHop: isFinalHop,
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// All hops must include a cltv expiry.
|
|
|
|
case !hasLockTime:
|
|
|
|
return ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.LockTimeOnionType,
|
|
|
|
Violation: OmittedViolation,
|
|
|
|
FinalHop: isFinalHop,
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The exit hop should omit the next hop id. If nextHop != Exit, the
|
|
|
|
// sender must have included a record, so we don't need to test for its
|
|
|
|
// inclusion at intermediate hops directly.
|
|
|
|
case isFinalHop && hasNextHop:
|
|
|
|
return ErrInvalidPayload{
|
2019-10-31 07:19:08 +03:00
|
|
|
Type: record.NextHopOnionType,
|
|
|
|
Violation: IncludedViolation,
|
|
|
|
FinalHop: true,
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
2019-11-05 02:10:00 +03:00
|
|
|
|
|
|
|
// Intermediate nodes should never receive MPP fields.
|
|
|
|
case !isFinalHop && hasMPP:
|
|
|
|
return ErrInvalidPayload{
|
|
|
|
Type: record.MPPOnionType,
|
|
|
|
Violation: IncludedViolation,
|
|
|
|
FinalHop: isFinalHop,
|
|
|
|
}
|
2019-09-05 16:05:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-05 02:10:32 +03:00
|
|
|
|
|
|
|
// MultiPath returns the record corresponding the option_mpp parsed from the
|
|
|
|
// onion payload.
|
|
|
|
func (h *Payload) MultiPath() *record.MPP {
|
|
|
|
return h.MPP
|
|
|
|
}
|
2019-11-10 14:06:49 +03:00
|
|
|
|
2021-03-03 20:54:11 +03:00
|
|
|
// AMPRecord returns the record corresponding with option_amp parsed from the
|
|
|
|
// onion payload.
|
|
|
|
func (h *Payload) AMPRecord() *record.AMP {
|
|
|
|
return h.AMP
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:32:56 +03:00
|
|
|
// CustomRecords returns the custom tlv type records that were parsed from the
|
|
|
|
// payload.
|
2019-12-12 02:01:55 +03:00
|
|
|
func (h *Payload) CustomRecords() record.CustomSet {
|
2019-11-19 14:32:56 +03:00
|
|
|
return h.customRecords
|
|
|
|
}
|
|
|
|
|
2019-11-10 14:06:49 +03:00
|
|
|
// getMinRequiredViolation checks for unrecognized required (even) fields in the
|
|
|
|
// standard range and returns the lowest required type. Always returning the
|
|
|
|
// lowest required type allows a failure message to be deterministic.
|
2019-11-19 14:05:20 +03:00
|
|
|
func getMinRequiredViolation(set tlv.TypeMap) *tlv.Type {
|
2019-11-10 14:06:49 +03:00
|
|
|
var (
|
|
|
|
requiredViolation bool
|
|
|
|
minRequiredViolationType tlv.Type
|
|
|
|
)
|
2019-11-19 14:05:20 +03:00
|
|
|
for t, parseResult := range set {
|
2019-11-10 14:06:49 +03:00
|
|
|
// If a type is even but not known to us, we cannot process the
|
|
|
|
// payload. We are required to understand a field that we don't
|
|
|
|
// support.
|
2019-11-11 12:37:24 +03:00
|
|
|
//
|
|
|
|
// We always accept custom fields, because a higher level
|
|
|
|
// application may understand them.
|
2019-12-12 02:01:55 +03:00
|
|
|
if parseResult == nil || t%2 != 0 ||
|
|
|
|
t >= record.CustomTypeStart {
|
|
|
|
|
2019-11-10 14:06:49 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !requiredViolation || t < minRequiredViolationType {
|
|
|
|
minRequiredViolationType = t
|
|
|
|
}
|
|
|
|
requiredViolation = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if requiredViolation {
|
|
|
|
return &minRequiredViolationType
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|