33#include <ccan/mem/mem.h>
44#include <ccan/tal/str/str.h>
55#include <channeld/channeld_wiregen.h>
6+ #include <common/bigsize.h>
67#include <common/blinding.h>
78#include <common/configdir.h>
89#include <common/ecdh.h>
2324#include <lightningd/plugin_hook.h>
2425#include <lightningd/subd.h>
2526#include <onchaind/onchaind_wiregen.h>
27+ #include <stdio.h>
28+ #include <wire/onion_wiregen.h>
29+ #include <wire/peer_wiregen.h>
30+ #include <wire/tlvstream.h>
2631
2732#ifndef SUPERVERBOSE
2833#define SUPERVERBOSE (...)
@@ -695,6 +700,7 @@ const u8 *send_htlc_out(const tal_t *ctx,
695700 struct amount_msat final_msat ,
696701 const struct sha256 * payment_hash ,
697702 const struct pubkey * path_key ,
703+ const struct tlv_field * extra_tlvs ,
698704 u64 partid ,
699705 u64 groupid ,
700706 const u8 * onion_routing_packet ,
@@ -729,7 +735,8 @@ const u8 *send_htlc_out(const tal_t *ctx,
729735 /* Make peer's daemon own it, catch if it dies. */
730736 * houtp = new_htlc_out (out -> owner , out , amount , cltv ,
731737 payment_hash , onion_routing_packet ,
732- path_key , in == NULL ,
738+ path_key , extra_tlvs ,
739+ in == NULL ,
733740 final_msat ,
734741 partid , groupid , in );
735742 tal_add_destructor (* houtp , destroy_hout_subd_died );
@@ -742,6 +749,13 @@ const u8 *send_htlc_out(const tal_t *ctx,
742749 * houtp );
743750 }
744751
752+ if (extra_tlvs ) {
753+ raw_tlvs = tal_arr (tmpctx , u8 , 0 );
754+ towire_tlvstream_raw (& raw_tlvs ,
755+ tal_dup_talarr (tmpctx , struct tlv_field ,
756+ extra_tlvs ));
757+ }
758+
745759 msg = towire_channeld_offer_htlc (out , amount , cltv , payment_hash ,
746760 onion_routing_packet , path_key ,
747761 raw_tlvs );
@@ -797,7 +811,8 @@ static void forward_htlc(struct htlc_in *hin,
797811 const struct short_channel_id * forward_scid ,
798812 const struct channel_id * forward_to ,
799813 const u8 next_onion [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )],
800- const struct pubkey * next_path_key )
814+ const struct pubkey * next_path_key ,
815+ const struct tlv_field * extra_tlvs )
801816{
802817 const u8 * failmsg ;
803818 struct lightningd * ld = hin -> key .channel -> peer -> ld ;
@@ -912,7 +927,7 @@ static void forward_htlc(struct htlc_in *hin,
912927 failmsg = send_htlc_out (tmpctx , next , amt_to_forward ,
913928 outgoing_cltv_value , AMOUNT_MSAT (0 ),
914929 & hin -> payment_hash ,
915- next_path_key , 0 /* partid */ , 0 /* groupid */ ,
930+ next_path_key , extra_tlvs , 0 /* partid */ , 0 /* groupid */ ,
916931 next_onion , hin , & hout );
917932 if (!failmsg )
918933 return ;
@@ -942,6 +957,7 @@ struct htlc_accepted_hook_payload {
942957 u64 failtlvtype ;
943958 size_t failtlvpos ;
944959 const char * failexplanation ;
960+ u8 * extra_tlvs_raw ;
945961};
946962
947963static void
@@ -998,8 +1014,8 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
9981014 struct htlc_in * hin = request -> hin ;
9991015 struct lightningd * ld = request -> ld ;
10001016 struct preimage payment_preimage ;
1001- const jsmntok_t * resulttok , * paykeytok , * payloadtok , * fwdtok ;
1002- u8 * failonion ;
1017+ const jsmntok_t * resulttok , * paykeytok , * payloadtok , * fwdtok , * extra_tlvs_tok ;
1018+ u8 * failonion , * raw_tlvs ;
10031019
10041020 if (!toks || !buffer )
10051021 return true;
@@ -1013,6 +1029,49 @@ static bool htlc_accepted_hook_deserialize(struct htlc_accepted_hook_payload *re
10131029 json_strdup (tmpctx , buffer , toks ));
10141030 }
10151031
1032+ extra_tlvs_tok = json_get_member (buffer , toks , "extra_tlvs" );
1033+ if (extra_tlvs_tok ) {
1034+ size_t max ;
1035+ struct tlv_update_add_htlc_tlvs * check_extra_tlvs ;
1036+
1037+ raw_tlvs = json_tok_bin_from_hex (tmpctx , buffer ,
1038+ extra_tlvs_tok );
1039+ if (!raw_tlvs )
1040+ fatal ("Bad custom tlvs for htlc_accepted"
1041+ " hook: %.*s" ,
1042+ extra_tlvs_tok -> end - extra_tlvs_tok -> start ,
1043+ buffer + extra_tlvs_tok -> start );
1044+
1045+ max = tal_bytelen (raw_tlvs );
1046+
1047+ /* We check if the custom tlvs are still valid BOLT#1 tlvs.
1048+ * As these are appended to forwarded htlcs we check for valid
1049+ * update_add_htlc_tlvs (restricts to known even types).
1050+ * NOTE: We may be less strict and allow unknown evens .*/
1051+ const u8 * cursor = raw_tlvs ;
1052+ check_extra_tlvs = fromwire_tlv_update_add_htlc_tlvs (tmpctx ,
1053+ & cursor ,
1054+ & max );
1055+ if (!check_extra_tlvs ) {
1056+ fatal ("htlc_accepted_hook returned bad extra_tlvs %s" ,
1057+ tal_hex (tmpctx , raw_tlvs ));
1058+ }
1059+
1060+ /* If we got a blinded path key we replace the next path key
1061+ * with it. */
1062+ if (check_extra_tlvs -> blinded_path ) {
1063+ tal_free (request -> next_path_key );
1064+ request -> next_path_key
1065+ = tal_steal (request ,
1066+ check_extra_tlvs -> blinded_path );
1067+ }
1068+
1069+ /* We made it and got a valid extra_tlvs: Replace the current
1070+ * extra_tlvs with it. */
1071+ tal_free (request -> extra_tlvs_raw );
1072+ request -> extra_tlvs_raw = tal_steal (request , raw_tlvs );
1073+ }
1074+
10161075 payloadtok = json_get_member (buffer , toks , "payload" );
10171076 if (payloadtok ) {
10181077 u8 * payload = json_tok_bin_from_hex (rs , buffer , payloadtok );
@@ -1170,6 +1229,9 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p,
11701229 json_add_u32 (s , "cltv_expiry" , expiry );
11711230 json_add_s32 (s , "cltv_expiry_relative" , expiry - blockheight );
11721231 json_add_sha256 (s , "payment_hash" , & hin -> payment_hash );
1232+ if (p -> extra_tlvs_raw ) {
1233+ json_add_hex_talarr (s , "extra_tlvs" , p -> extra_tlvs_raw );
1234+ }
11731235 json_object_end (s );
11741236}
11751237
@@ -1200,13 +1262,25 @@ htlc_accepted_hook_final(struct htlc_accepted_hook_payload *request STEALS)
12001262 NULL , request -> failtlvtype ,
12011263 request -> failtlvpos )));
12021264 } else if (rs -> nextcase == ONION_FORWARD ) {
1265+ struct tlv_field * extra_tlvs ;
1266+
1267+ if (request -> extra_tlvs_raw ) {
1268+ const u8 * cursor = request -> extra_tlvs_raw ;
1269+ size_t max = tal_bytelen (cursor );
1270+ extra_tlvs = tal_arr (request , struct tlv_field , 0 );
1271+ fromwire_tlv (& cursor , & max , NULL , 0 , request ,
1272+ & extra_tlvs , NULL , NULL , NULL );
1273+ } else {
1274+ extra_tlvs = NULL ;
1275+ }
1276+
12031277 forward_htlc (hin , hin -> cltv_expiry ,
12041278 request -> payload -> amt_to_forward ,
12051279 request -> payload -> outgoing_cltv ,
12061280 request -> payload -> forward_channel ,
12071281 request -> fwd_channel_id ,
12081282 serialize_onionpacket (tmpctx , rs -> next ),
1209- request -> next_path_key );
1283+ request -> next_path_key , extra_tlvs );
12101284 } else
12111285 handle_localpay (hin ,
12121286 request -> payload -> amt_to_forward ,
@@ -1484,6 +1558,14 @@ static bool peer_accepted_htlc(const tal_t *ctx,
14841558 hook_payload -> fwd_channel_id
14851559 = calc_forwarding_channel (ld , hook_payload );
14861560
1561+ if (hin -> extra_tlvs ) {
1562+ hook_payload -> extra_tlvs_raw = tal_arr (hook_payload , u8 , 0 );
1563+ towire_tlvstream_raw (& hook_payload -> extra_tlvs_raw ,
1564+ hin -> extra_tlvs );
1565+ } else {
1566+ hook_payload -> extra_tlvs_raw = NULL ;
1567+ }
1568+
14871569 plugin_hook_call_htlc_accepted (ld , NULL , hook_payload );
14881570
14891571 /* Falling through here is ok, after all the HTLC locked */
@@ -2210,6 +2292,7 @@ static bool channel_added_their_htlc(struct channel *channel,
22102292 op ? & shared_secret : NULL ,
22112293 added -> path_key ,
22122294 added -> onion_routing_packet ,
2295+ added -> extra_tlvs ,
22132296 added -> fail_immediate );
22142297
22152298 /* Save an incoming htlc to the wallet */
@@ -2641,13 +2724,15 @@ const struct existing_htlc **peer_htlcs(const tal_t *ctx,
26412724 else
26422725 f = NULL ;
26432726
2727+
26442728 existing = new_existing_htlc (htlcs , hin -> key .id , hin -> hstate ,
26452729 hin -> msat , & hin -> payment_hash ,
26462730 hin -> cltv_expiry ,
26472731 hin -> onion_routing_packet ,
26482732 hin -> path_key ,
26492733 hin -> preimage ,
2650- f , NULL );
2734+ f ,
2735+ hin -> extra_tlvs );
26512736 tal_arr_expand (& htlcs , existing );
26522737 }
26532738
@@ -2679,7 +2764,8 @@ const struct existing_htlc **peer_htlcs(const tal_t *ctx,
26792764 hout -> onion_routing_packet ,
26802765 hout -> path_key ,
26812766 hout -> preimage ,
2682- f , NULL );
2767+ f ,
2768+ hout -> extra_tlvs );
26832769 tal_arr_expand (& htlcs , existing );
26842770 }
26852771
0 commit comments