-
Notifications
You must be signed in to change notification settings - Fork 911
/
onion_message.c
171 lines (148 loc) · 5.08 KB
/
onion_message.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*~ This contains all the code to handle onion messages. */
#include "config.h"
#include <ccan/cast/cast.h>
#include <ccan/tal/str/str.h>
#include <common/blindedpath.h>
#include <common/blinding.h>
#include <common/daemon_conn.h>
#include <common/ecdh_hsmd.h>
#include <common/features.h>
#include <common/onion_message_parse.h>
#include <common/sphinx.h>
#include <common/status.h>
#include <common/wire_error.h>
#include <connectd/connectd.h>
#include <connectd/connectd_wiregen.h>
#include <connectd/multiplex.h>
#include <connectd/onion_message.h>
#include <wire/peer_wire.h>
void onionmsg_req(struct daemon *daemon, const u8 *msg)
{
struct node_id id;
u8 *onionmsg;
struct pubkey path_key;
struct peer *peer;
if (!fromwire_connectd_send_onionmsg(msg, msg, &id, &onionmsg, &path_key))
master_badmsg(WIRE_CONNECTD_SEND_ONIONMSG, msg);
/* Even though lightningd checks for valid ids, there's a race
* where it might vanish before we read this command. */
peer = peer_htable_get(daemon->peers, &id);
if (peer) {
u8 *omsg = towire_onion_message(NULL, &path_key, onionmsg);
inject_peer_msg(peer, take(omsg));
}
}
static const char *handle_onion(const tal_t *ctx,
struct daemon *daemon,
const struct pubkey *path_key,
const u8 *onion)
{
u8 *next_onion_msg;
struct sciddir_or_pubkey next_node;
struct tlv_onionmsg_tlv *final_om;
struct pubkey final_alias;
struct secret *final_path_id;
const char *err;
err = onion_message_parse(tmpctx, onion, path_key,
&daemon->mykey,
&next_onion_msg, &next_node,
&final_om, &final_alias, &final_path_id);
if (err) {
return tal_steal(ctx, err);
}
if (final_om) {
u8 *omsg;
/* We re-marshall here by policy, before handing to lightningd */
omsg = tal_arr(tmpctx, u8, 0);
towire_tlvstream_raw(&omsg, final_om->fields);
daemon_conn_send(daemon->master,
take(towire_connectd_got_onionmsg_to_us(NULL,
final_path_id,
final_om->reply_path,
omsg)));
} else {
struct node_id next_node_id;
struct peer *next_peer;
assert(next_onion_msg);
/* BOLT-offers #4:
* - if it is not the final node according to the onion encryption:
*...
* - if `next_node_id` is present:
* - the *next peer* is the peer with that node id.
* - otherwise, if `short_channel_id` is present and corresponds to an announced short_channel_id or a local alias for a channel:
* - the *next peer* is the peer at the other end of that channel.
* - otherwise:
* - MUST ignore the message.
* - SHOULD forward the message using `onion_message` to the *next peer*.
*/
/* Since an alias is legal here, we can't simply lookup in gossmap. */
if (!next_node.is_pubkey) {
struct scid_to_node_id *scid_to_node_id;
scid_to_node_id = scid_htable_get(daemon->scid_htable, next_node.scidd.scid);
if (!scid_to_node_id) {
return tal_fmt(ctx, "onion msg: unknown next scid %s",
fmt_short_channel_id(tmpctx, next_node.scidd.scid));
}
next_node_id = scid_to_node_id->node_id;
} else {
node_id_from_pubkey(&next_node_id, &next_node.pubkey);
}
next_peer = peer_htable_get(daemon->peers, &next_node_id);
if (!next_peer) {
return tal_fmt(ctx, "onion msg: unknown next peer %s",
fmt_sciddir_or_pubkey(tmpctx, &next_node));
}
inject_peer_msg(next_peer, take(next_onion_msg));
}
return NULL;
}
/* Peer sends an onion msg, or (if peer NULL) lightningd injects one. */
void handle_onion_message(struct daemon *daemon,
struct peer *peer, const u8 *msg)
{
struct pubkey path_key;
u8 *onion;
u64 msec;
struct timemono now = time_mono();
/* Ignore unless explicitly turned on. */
if (!feature_offered(daemon->our_features->bits[NODE_ANNOUNCE_FEATURE],
OPT_ONION_MESSAGES))
return;
/* Adjust tokens if they're entitled to more */
msec = time_to_msec(timemono_between(now, peer->onionmsg_last_incoming));
peer->onionmsg_incoming_tokens += msec;
if (peer->onionmsg_incoming_tokens > ONION_MSG_TOKENS_MAX)
peer->onionmsg_incoming_tokens = ONION_MSG_TOKENS_MAX;
peer->onionmsg_last_incoming = now;
/* No tokens left? Limit it */
if (peer->onionmsg_incoming_tokens < ONION_MSG_MSEC) {
/* Warn once, for debugging */
if (!peer->onionmsg_limit_warned) {
inject_peer_msg(peer,
take(towire_warningfmt(NULL, NULL,
"Ratelimited onion_message: exceeded one per %umsec",
ONION_MSG_MSEC)));
peer->onionmsg_limit_warned = true;
}
return;
}
peer->onionmsg_incoming_tokens -= ONION_MSG_MSEC;
if (!fromwire_onion_message(msg, msg, &path_key, &onion)) {
inject_peer_msg(peer,
take(towire_warningfmt(NULL, NULL,
"Bad onion_message")));
return;
}
handle_onion(tmpctx, daemon, &path_key, onion);
}
void inject_onionmsg_req(struct daemon *daemon, const u8 *msg)
{
u8 *onionmsg;
struct pubkey path_key;
const char *err;
if (!fromwire_connectd_inject_onionmsg(msg, msg, &path_key, &onionmsg))
master_badmsg(WIRE_CONNECTD_INJECT_ONIONMSG, msg);
err = handle_onion(tmpctx, daemon, &path_key, onionmsg);
daemon_conn_send(daemon->master,
take(towire_connectd_inject_onionmsg_reply(NULL, err ? err : "")));
}