@@ -14,12 +14,13 @@ use secp256k1;
14
14
use chain:: chaininterface:: { BroadcasterInterface , ChainListener , ChainWatchInterface , FeeEstimator } ;
15
15
use ln:: channel:: { Channel , ChannelKeys } ;
16
16
use ln:: channelmonitor:: ManyChannelMonitor ;
17
- use ln:: router:: Route ;
17
+ use ln:: router:: { Route , RouteHop } ;
18
18
use ln:: msgs;
19
19
use ln:: msgs:: { HandleError , ChannelMessageHandler , MsgEncodable , MsgDecodable } ;
20
20
use util:: { byte_utils, events, internal_traits, rng} ;
21
21
use util:: sha2:: Sha256 ;
22
22
23
+ use crypto;
23
24
use crypto:: mac:: { Mac , MacResult } ;
24
25
use crypto:: hmac:: Hmac ;
25
26
use crypto:: digest:: Digest ;
@@ -91,12 +92,14 @@ enum PendingOutboundHTLC {
91
92
} ,
92
93
OutboundRoute {
93
94
route : Route ,
95
+ session_priv : SecretKey ,
94
96
} ,
95
97
/// Used for channel rebalancing
96
98
CycledRoute {
97
99
source_short_channel_id : u64 ,
98
100
incoming_packet_shared_secret : SharedSecret ,
99
101
route : Route ,
102
+ session_priv : SecretKey ,
100
103
}
101
104
}
102
105
@@ -336,8 +339,9 @@ impl ChannelManager {
336
339
res
337
340
}
338
341
339
- fn construct_onion_keys ( secp_ctx : & Secp256k1 , route : & Route , session_priv : & SecretKey ) -> Result < Vec < OnionKeys > , HandleError > {
340
- let mut res = Vec :: with_capacity ( route. hops . len ( ) ) ;
342
+ // can only fail if an intermediary hop has an invalid public key or session_priv is invalid
343
+ #[ inline]
344
+ fn construct_onion_keys_callback < FType : FnMut ( SharedSecret , [ u8 ; 32 ] , PublicKey , & RouteHop ) > ( secp_ctx : & Secp256k1 , route : & Route , session_priv : & SecretKey , mut callback : FType ) -> Result < ( ) , HandleError > {
341
345
let mut blinded_priv = session_priv. clone ( ) ;
342
346
let mut blinded_pub = secp_call ! ( PublicKey :: from_secret_key( secp_ctx, & blinded_priv) ) ;
343
347
let mut first_iteration = true ;
@@ -360,18 +364,29 @@ impl ChannelManager {
360
364
secp_call ! ( blinded_priv. mul_assign( secp_ctx, & secp_call!( SecretKey :: from_slice( secp_ctx, & blinding_factor) ) ) ) ;
361
365
blinded_pub = secp_call ! ( PublicKey :: from_secret_key( secp_ctx, & blinded_priv) ) ;
362
366
367
+ callback ( shared_secret, blinding_factor, ephemeral_pubkey, hop) ;
368
+ }
369
+
370
+ Ok ( ( ) )
371
+ }
372
+
373
+ // can only fail if an intermediary hop has an invalid public key or session_priv is invalid
374
+ fn construct_onion_keys ( secp_ctx : & Secp256k1 , route : & Route , session_priv : & SecretKey ) -> Result < Vec < OnionKeys > , HandleError > {
375
+ let mut res = Vec :: with_capacity ( route. hops . len ( ) ) ;
376
+
377
+ Self :: construct_onion_keys_callback ( secp_ctx, route, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _| {
363
378
let ( rho, mu) = ChannelManager :: gen_rho_mu_from_shared_secret ( & shared_secret) ;
364
379
365
380
res. push ( OnionKeys {
366
381
#[ cfg( test) ]
367
- shared_secret : shared_secret ,
382
+ shared_secret,
368
383
#[ cfg( test) ]
369
- blinding_factor : blinding_factor ,
370
- ephemeral_pubkey : ephemeral_pubkey ,
371
- rho : rho ,
372
- mu : mu ,
384
+ blinding_factor : _blinding_factor ,
385
+ ephemeral_pubkey,
386
+ rho,
387
+ mu,
373
388
} ) ;
374
- }
389
+ } ) ? ;
375
390
376
391
Ok ( res)
377
392
}
@@ -602,7 +617,8 @@ impl ChannelManager {
602
617
} ;
603
618
604
619
if channel_state. claimable_htlcs . insert ( payment_hash, PendingOutboundHTLC :: OutboundRoute {
605
- route : route,
620
+ route,
621
+ session_priv,
606
622
} ) . is_some ( ) {
607
623
// TODO: We need to track these better, we're not generating these, so a
608
624
// third-party might make this happen:
@@ -747,7 +763,7 @@ impl ChannelManager {
747
763
for failed_forward in failed_forwards. drain ( ..) {
748
764
match failed_forward. 2 {
749
765
None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & failed_forward. 0 , HTLCFailReason :: Reason { failure_code : failed_forward. 1 , data : Vec :: new ( ) } ) ,
750
- Some ( chan_update) => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & failed_forward. 0 , HTLCFailReason :: Reason { failure_code : failed_forward. 1 , data : chan_update. encode ( ) } ) ,
766
+ Some ( chan_update) => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , & failed_forward. 0 , HTLCFailReason :: Reason { failure_code : failed_forward. 1 , data : chan_update. encode_with_len ( ) } ) ,
751
767
} ;
752
768
}
753
769
@@ -774,7 +790,11 @@ impl ChannelManager {
774
790
} ;
775
791
776
792
match pending_htlc {
777
- PendingOutboundHTLC :: CycledRoute { source_short_channel_id, incoming_packet_shared_secret, .. } => {
793
+ PendingOutboundHTLC :: CycledRoute { source_short_channel_id, incoming_packet_shared_secret, route, session_priv } => {
794
+ channel_state. claimable_htlcs . insert ( payment_hash. clone ( ) , PendingOutboundHTLC :: OutboundRoute {
795
+ route,
796
+ session_priv,
797
+ } ) ;
778
798
pending_htlc = PendingOutboundHTLC :: IntermediaryHopData { source_short_channel_id, incoming_packet_shared_secret } ;
779
799
} ,
780
800
_ => { }
@@ -783,8 +803,8 @@ impl ChannelManager {
783
803
match pending_htlc {
784
804
PendingOutboundHTLC :: CycledRoute { .. } => { panic ! ( "WAT" ) ; } ,
785
805
PendingOutboundHTLC :: OutboundRoute { .. } => {
786
- //TODO: DECRYPT route from OutboundRoute
787
806
mem:: drop ( channel_state) ;
807
+
788
808
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
789
809
pending_events. push ( events:: Event :: PaymentFailed {
790
810
payment_hash : payment_hash. clone ( )
@@ -858,13 +878,13 @@ impl ChannelManager {
858
878
} ;
859
879
860
880
match pending_htlc {
861
- PendingOutboundHTLC :: CycledRoute { source_short_channel_id, incoming_packet_shared_secret, route } => {
881
+ PendingOutboundHTLC :: CycledRoute { source_short_channel_id, incoming_packet_shared_secret, route, session_priv } => {
862
882
if from_user { // This was the end hop back to us
863
883
pending_htlc = PendingOutboundHTLC :: IntermediaryHopData { source_short_channel_id, incoming_packet_shared_secret } ;
864
- channel_state. claimable_htlcs . insert ( payment_hash, PendingOutboundHTLC :: OutboundRoute { route } ) ;
884
+ channel_state. claimable_htlcs . insert ( payment_hash, PendingOutboundHTLC :: OutboundRoute { route, session_priv } ) ;
865
885
} else { // This came from the first upstream node
866
886
// Bank error in our favor! Maybe we should tell the user this somehow???
867
- pending_htlc = PendingOutboundHTLC :: OutboundRoute { route } ;
887
+ pending_htlc = PendingOutboundHTLC :: OutboundRoute { route, session_priv } ;
868
888
channel_state. claimable_htlcs . insert ( payment_hash, PendingOutboundHTLC :: IntermediaryHopData { source_short_channel_id, incoming_packet_shared_secret } ) ;
869
889
}
870
890
} ,
@@ -1332,7 +1352,7 @@ impl ChannelMessageHandler for ChannelManager {
1332
1352
let chan = channel_state. by_id . get_mut ( & forwarding_id) . unwrap ( ) ;
1333
1353
if !chan. is_live ( ) {
1334
1354
let chan_update = self . get_channel_update ( chan) . unwrap ( ) ;
1335
- return_err ! ( "Forwarding channel is not in a ready state." , 0x4000 | 10 , & chan_update. encode ( ) [ ..] ) ;
1355
+ return_err ! ( "Forwarding channel is not in a ready state." , 0x4000 | 7 , & chan_update. encode_with_len ( ) [ ..] ) ;
1336
1356
}
1337
1357
}
1338
1358
@@ -1376,16 +1396,17 @@ impl ChannelMessageHandler for ChannelManager {
1376
1396
match claimable_htlcs_entry {
1377
1397
hash_map:: Entry :: Occupied ( mut e) => {
1378
1398
let outbound_route = e. get_mut ( ) ;
1379
- let route = match outbound_route {
1380
- & mut PendingOutboundHTLC :: OutboundRoute { ref route } => {
1381
- route. clone ( )
1399
+ let ( route, session_priv ) = match outbound_route {
1400
+ & mut PendingOutboundHTLC :: OutboundRoute { ref route, ref session_priv } => {
1401
+ ( route. clone ( ) , session_priv . clone ( ) )
1382
1402
} ,
1383
1403
_ => { panic ! ( "WAT" ) } ,
1384
1404
} ;
1385
1405
* outbound_route = PendingOutboundHTLC :: CycledRoute {
1386
1406
source_short_channel_id,
1387
1407
incoming_packet_shared_secret : shared_secret,
1388
- route
1408
+ route,
1409
+ session_priv,
1389
1410
} ;
1390
1411
} ,
1391
1412
hash_map:: Entry :: Vacant ( e) => {
@@ -1422,16 +1443,78 @@ impl ChannelMessageHandler for ChannelManager {
1422
1443
Ok ( ( ) )
1423
1444
}
1424
1445
1425
- fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) -> Result < ( ) , HandleError > {
1446
+ fn handle_update_fail_htlc ( & self , their_node_id : & PublicKey , msg : & msgs:: UpdateFailHTLC ) -> Result < Option < msgs :: HTLCFailChannelUpdate > , HandleError > {
1426
1447
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
1427
- match channel_state. by_id . get_mut ( & msg. channel_id ) {
1448
+ let payment_hash = match channel_state. by_id . get_mut ( & msg. channel_id ) {
1428
1449
Some ( chan) => {
1429
1450
if chan. get_their_node_id ( ) != * their_node_id {
1430
1451
return Err ( HandleError { err : "Got a message for a channel from the wrong node!" , msg : None } )
1431
1452
}
1432
1453
chan. update_fail_htlc ( & msg, HTLCFailReason :: ErrorPacket { err : msg. reason . clone ( ) } )
1433
1454
} ,
1434
1455
None => return Err ( HandleError { err : "Failed to find corresponding channel" , msg : None } )
1456
+ } ?;
1457
+
1458
+ if let Some ( pending_htlc) = channel_state. claimable_htlcs . get ( & payment_hash) {
1459
+ match pending_htlc {
1460
+ & PendingOutboundHTLC :: OutboundRoute { ref route, ref session_priv } => {
1461
+ // Handle packed channel/node updates for passing back for the route handler
1462
+ let mut packet_decrypted = msg. reason . data . clone ( ) ;
1463
+ let mut res = None ;
1464
+ Self :: construct_onion_keys_callback ( & self . secp_ctx , & route, & session_priv, |shared_secret, _, _, route_hop| {
1465
+ if res. is_some ( ) { return ; }
1466
+
1467
+ let ammag = ChannelManager :: gen_ammag_from_shared_secret ( & shared_secret) ;
1468
+
1469
+ let mut decryption_tmp = Vec :: with_capacity ( packet_decrypted. len ( ) ) ;
1470
+ decryption_tmp. resize ( packet_decrypted. len ( ) , 0 ) ;
1471
+ let mut chacha = ChaCha20 :: new ( & ammag, & [ 0u8 ; 8 ] ) ;
1472
+ chacha. process ( & packet_decrypted, & mut decryption_tmp[ ..] ) ;
1473
+ packet_decrypted = decryption_tmp;
1474
+
1475
+ if let Ok ( err_packet) = msgs:: DecodedOnionErrorPacket :: decode ( & packet_decrypted) {
1476
+ if err_packet. failuremsg . len ( ) >= 2 {
1477
+ let um = ChannelManager :: gen_um_from_shared_secret ( & shared_secret) ;
1478
+
1479
+ let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & um) ;
1480
+ hmac. input ( & err_packet. encode ( ) [ 32 ..] ) ;
1481
+ let mut calc_tag = [ 0u8 ; 32 ] ;
1482
+ hmac. raw_result ( & mut calc_tag) ;
1483
+ if crypto:: util:: fixed_time_eq ( & calc_tag, & err_packet. hmac ) {
1484
+ const UNKNOWN_CHAN : u16 = 0x4000 |10 ;
1485
+ const TEMP_CHAN_FAILURE : u16 = 0x4000 |7 ;
1486
+ match byte_utils:: slice_to_be16 ( & err_packet. failuremsg [ 0 ..2 ] ) {
1487
+ TEMP_CHAN_FAILURE => {
1488
+ if err_packet. failuremsg . len ( ) >= 4 {
1489
+ let update_len = byte_utils:: slice_to_be16 ( & err_packet. failuremsg [ 2 ..4 ] ) as usize ;
1490
+ if err_packet. failuremsg . len ( ) >= 4 + update_len {
1491
+ if let Ok ( chan_update) = msgs:: ChannelUpdate :: decode ( & err_packet. failuremsg [ 4 ..4 + update_len] ) {
1492
+ res = Some ( msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage {
1493
+ msg : chan_update,
1494
+ } ) ;
1495
+ }
1496
+ }
1497
+ }
1498
+ } ,
1499
+ UNKNOWN_CHAN => {
1500
+ // No such next-hop. We know this came from the
1501
+ // current node as the HMAC validated.
1502
+ res = Some ( msgs:: HTLCFailChannelUpdate :: ChannelClosed {
1503
+ short_channel_id : route_hop. short_channel_id
1504
+ } ) ;
1505
+ } ,
1506
+ _ => { } , //TODO: Enumerate all of these!
1507
+ }
1508
+ }
1509
+ }
1510
+ }
1511
+ } ) . unwrap ( ) ;
1512
+ Ok ( res)
1513
+ } ,
1514
+ _ => { Ok ( None ) } ,
1515
+ }
1516
+ } else {
1517
+ Ok ( None )
1435
1518
}
1436
1519
}
1437
1520
@@ -2169,14 +2252,7 @@ mod tests {
2169
2252
claim_payment ( & origin, expected_route, our_payment_preimage) ;
2170
2253
}
2171
2254
2172
- fn send_failed_payment ( origin_node : & Node , expected_route : & [ & Node ] ) {
2173
- let route = origin_node. router . get_route ( & expected_route. last ( ) . unwrap ( ) . node . get_our_node_id ( ) , & Vec :: new ( ) , 1000000 , 142 ) . unwrap ( ) ;
2174
- assert_eq ! ( route. hops. len( ) , expected_route. len( ) ) ;
2175
- for ( node, hop) in expected_route. iter ( ) . zip ( route. hops . iter ( ) ) {
2176
- assert_eq ! ( hop. pubkey, node. node. get_our_node_id( ) ) ;
2177
- }
2178
- let our_payment_hash = send_along_route ( origin_node, route, expected_route, 1000000 ) . 1 ;
2179
-
2255
+ fn fail_payment ( origin_node : & Node , expected_route : & [ & Node ] , our_payment_hash : [ u8 ; 32 ] ) {
2180
2256
assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash) ) ;
2181
2257
2182
2258
let mut next_msgs: Option < ( msgs:: UpdateFailHTLC , msgs:: CommitmentSigned ) > = None ;
@@ -2288,7 +2364,8 @@ mod tests {
2288
2364
send_payment ( & nodes[ 3 ] , & vec ! ( & nodes[ 2 ] , & nodes[ 1 ] ) [ ..] , 1000000 ) ;
2289
2365
2290
2366
// Test failure packets
2291
- send_failed_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] , & nodes[ 3 ] ) [ ..] ) ;
2367
+ let payment_hash_1 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] , & nodes[ 3 ] ) [ ..] , 1000000 ) . 1 ;
2368
+ fail_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] , & nodes[ 3 ] ) [ ..] , payment_hash_1) ;
2292
2369
2293
2370
// Add a new channel that skips 3
2294
2371
let chan_4 = create_announced_chan_between_nodes ( & nodes, 1 , 3 ) ;
@@ -2346,10 +2423,10 @@ mod tests {
2346
2423
} ) ;
2347
2424
hops[ 1 ] . fee_msat = chan_2. 1 . contents . fee_base_msat as u64 + chan_2. 1 . contents . fee_proportional_millionths as u64 * hops[ 2 ] . fee_msat as u64 / 1000000 ;
2348
2425
hops[ 0 ] . fee_msat = chan_3. 1 . contents . fee_base_msat as u64 + chan_3. 1 . contents . fee_proportional_millionths as u64 * hops[ 1 ] . fee_msat as u64 / 1000000 ;
2349
- let payment_preimage_2 = send_along_route ( & nodes[ 1 ] , Route { hops } , & vec ! ( & nodes[ 3 ] , & nodes[ 2 ] , & nodes[ 1 ] ) [ ..] , 1000000 ) . 0 ;
2426
+ let payment_hash_2 = send_along_route ( & nodes[ 1 ] , Route { hops } , & vec ! ( & nodes[ 3 ] , & nodes[ 2 ] , & nodes[ 1 ] ) [ ..] , 1000000 ) . 1 ;
2350
2427
2351
2428
// Claim the rebalances...
2352
- claim_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 3 ] , & nodes[ 2 ] , & nodes[ 1 ] ) [ ..] , payment_preimage_2 ) ;
2429
+ fail_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 3 ] , & nodes[ 2 ] , & nodes[ 1 ] ) [ ..] , payment_hash_2 ) ;
2353
2430
claim_payment ( & nodes[ 1 ] , & vec ! ( & nodes[ 2 ] , & nodes[ 3 ] , & nodes[ 1 ] ) [ ..] , payment_preimage_1) ;
2354
2431
2355
2432
// Add a duplicate new channel from 2 to 4
0 commit comments