@@ -1044,22 +1044,32 @@ where
10441044 let amt_to_forward = htlc_msat - route_hop. fee_msat ;
10451045 htlc_msat = amt_to_forward;
10461046
1047- let err_packet = match decrypt_onion_error_packet ( & mut encrypted_packet, shared_secret) {
1048- Ok ( p) => p,
1049- Err ( _) => return ,
1050- } ;
1047+ let decrypt_result = decrypt_onion_error_packet ( & mut encrypted_packet, shared_secret) ;
1048+
10511049 let um = gen_um_from_shared_secret ( shared_secret. as_ref ( ) ) ;
10521050 let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
1053- hmac. input ( & err_packet . encode ( ) [ 32 ..] ) ;
1051+ hmac. input ( & encrypted_packet [ 32 ..] ) ;
10541052
1055- if !fixed_time_eq ( & Hmac :: from_engine ( hmac) . to_byte_array ( ) , & err_packet . hmac ) {
1053+ if !fixed_time_eq ( & Hmac :: from_engine ( hmac) . to_byte_array ( ) , & encrypted_packet [ .. 32 ] ) {
10561054 return ;
10571055 }
1056+
1057+ let err_packet = match decrypt_result {
1058+ Ok ( p) => p,
1059+ Err ( _) => {
1060+ log_warn ! ( logger, "Unreadable failure from {}" , route_hop. pubkey) ;
1061+
1062+ return ;
1063+ }
1064+ } ;
1065+
10581066 let error_code_slice = match err_packet. failuremsg . get ( 0 ..2 ) {
10591067 Some ( s) => s,
10601068 None => {
10611069 // Useless packet that we can't use but it passed HMAC, so it definitely came from the peer
10621070 // in question
1071+ log_warn ! ( logger, "Missing error code in failure from {}" , route_hop. pubkey) ;
1072+
10631073 let network_update = Some ( NetworkUpdate :: NodeFailure {
10641074 node_id : route_hop. pubkey ,
10651075 is_permanent : true ,
@@ -1219,6 +1229,12 @@ where
12191229 } else {
12201230 // only not set either packet unparseable or hmac does not match with any
12211231 // payment not retryable only when garbage is from the final node
1232+ log_warn ! (
1233+ logger,
1234+ "Non-attributable failure encountered on route {}" ,
1235+ path. hops. iter( ) . map( |h| h. pubkey. to_string( ) ) . collect:: <Vec <_>>( ) . join( "->" )
1236+ ) ;
1237+
12221238 DecodedOnionFailure {
12231239 network_update : None ,
12241240 short_channel_id : None ,
@@ -2155,6 +2171,54 @@ mod tests {
21552171 assert_eq ! ( decrypted_failure. onion_error_code, Some ( 0x2002 ) ) ;
21562172 }
21572173
2174+ #[ test]
2175+ fn test_non_attributable_failure_packet_onion ( ) {
2176+ // Create a failure packet with bogus data.
2177+ let packet = vec ! [ 1u8 ; 292 ] ;
2178+
2179+ // In the current protocol, it is unfortunately not possible to identify the failure source.
2180+ let decrypted_failure = test_failure_attribution ( & packet) ;
2181+ assert_eq ! ( decrypted_failure. short_channel_id, None ) ;
2182+ }
2183+
2184+ #[ test]
2185+ fn test_missing_error_code ( ) {
2186+ // Create a failure packet with a valid hmac and structure, but no error code.
2187+ let onion_keys: Vec < OnionKeys > = build_test_onion_keys ( ) ;
2188+ let shared_secret = onion_keys[ 0 ] . shared_secret . as_ref ( ) ;
2189+ let um = gen_um_from_shared_secret ( & shared_secret) ;
2190+
2191+ let failuremsg = vec ! [ 1 ] ;
2192+ let pad = Vec :: new ( ) ;
2193+ let mut packet = msgs:: DecodedOnionErrorPacket { hmac : [ 0 ; 32 ] , failuremsg, pad } ;
2194+
2195+ let mut hmac = HmacEngine :: < Sha256 > :: new ( & um) ;
2196+ hmac. input ( & packet. encode ( ) [ 32 ..] ) ;
2197+ packet. hmac = Hmac :: from_engine ( hmac) . to_byte_array ( ) ;
2198+
2199+ let packet = encrypt_failure_packet ( shared_secret, & packet. encode ( ) [ ..] ) ;
2200+
2201+ let decrypted_failure = test_failure_attribution ( & packet. data ) ;
2202+ assert_eq ! ( decrypted_failure. short_channel_id, Some ( 0 ) ) ;
2203+ }
2204+
2205+ fn test_failure_attribution ( packet : & [ u8 ] ) -> DecodedOnionFailure {
2206+ let logger: Arc < TestLogger > = Arc :: new ( TestLogger :: new ( ) ) ;
2207+ let ctx_full = Secp256k1 :: new ( ) ;
2208+ let path = build_test_path ( ) ;
2209+ let htlc_source = HTLCSource :: OutboundRoute {
2210+ path,
2211+ session_priv : get_test_session_key ( ) ,
2212+ first_hop_htlc_msat : 0 ,
2213+ payment_id : PaymentId ( [ 1 ; 32 ] ) ,
2214+ } ;
2215+
2216+ let decrypted_failure =
2217+ process_onion_failure ( & ctx_full, & logger, & htlc_source, packet. into ( ) ) ;
2218+
2219+ decrypted_failure
2220+ }
2221+
21582222 struct RawOnionHopData {
21592223 data : Vec < u8 > ,
21602224 }
0 commit comments