2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2020-05-16 03:29:05 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <common/dev_disconnect.h>
|
2022-01-08 14:17:29 +01:00
|
|
|
#include <common/memleak.h>
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/wire_error.h>
|
|
|
|
#include <connectd/connectd.h>
|
2020-08-25 04:16:22 +02:00
|
|
|
#include <connectd/connectd_wiregen.h>
|
2018-09-27 23:04:19 +02:00
|
|
|
#include <connectd/peer_exchange_initmsg.h>
|
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
|
|
|
|
/* Temporary structure for us to read peer message in */
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer {
|
2018-09-27 23:04:19 +02:00
|
|
|
struct daemon *daemon;
|
|
|
|
|
|
|
|
/* The ID of the peer */
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id id;
|
2018-09-27 23:04:19 +02:00
|
|
|
|
|
|
|
/* Where it's connected to/from. */
|
|
|
|
struct wireaddr_internal addr;
|
|
|
|
|
|
|
|
/* Crypto state for writing/reading peer initmsg */
|
|
|
|
struct crypto_state cs;
|
|
|
|
|
|
|
|
/* Buffer for reading/writing message. */
|
|
|
|
u8 *msg;
|
2021-03-24 05:37:50 +01:00
|
|
|
|
|
|
|
bool incoming;
|
2018-09-27 23:04:19 +02:00
|
|
|
};
|
|
|
|
|
2019-11-28 15:11:07 +01:00
|
|
|
static bool contains_common_chain(struct bitcoin_blkid *chains)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(chains); i++) {
|
2019-12-25 00:34:59 +01:00
|
|
|
if (bitcoin_blkid_eq(&chains[i], &chainparams->genesis_blockhash))
|
2019-11-28 15:11:07 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-27 23:04:19 +02:00
|
|
|
/* Here in case we need to read another message. */
|
2021-12-29 04:26:43 +01:00
|
|
|
static struct io_plan *read_init(struct io_conn *conn, struct early_peer *peer);
|
2018-09-27 23:04:19 +02:00
|
|
|
|
|
|
|
static struct io_plan *peer_init_received(struct io_conn *conn,
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer *peer)
|
2018-09-27 23:04:19 +02:00
|
|
|
{
|
2018-11-21 23:41:50 +01:00
|
|
|
u8 *msg = cryptomsg_decrypt_body(tmpctx, &peer->cs, peer->msg);
|
2019-10-11 04:52:04 +02:00
|
|
|
u8 *globalfeatures, *features;
|
2019-11-28 15:11:07 +01:00
|
|
|
struct tlv_init_tlvs *tlvs = tlv_init_tlvs_new(msg);
|
2018-09-27 23:04:19 +02:00
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
return io_close(conn);
|
|
|
|
|
2019-11-17 12:42:33 +01:00
|
|
|
status_peer_io(LOG_IO_IN, &peer->id, msg);
|
2018-09-27 23:04:19 +02:00
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* A receiving node:
|
|
|
|
* - upon receiving a message of _odd_, unknown type:
|
|
|
|
* - MUST ignore the received message.
|
|
|
|
*/
|
|
|
|
if (unlikely(is_unknown_msg_discardable(msg)))
|
|
|
|
return read_init(conn, peer);
|
|
|
|
|
2019-11-28 15:11:07 +01:00
|
|
|
if (!fromwire_init(tmpctx, msg, &globalfeatures, &features, tlvs)) {
|
2019-11-18 01:26:17 +01:00
|
|
|
status_peer_debug(&peer->id,
|
|
|
|
"bad fromwire_init '%s', closing",
|
|
|
|
tal_hex(tmpctx, msg));
|
2018-09-27 23:04:19 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:49:27 +02:00
|
|
|
/* BOLT #1:
|
2019-11-28 15:11:07 +01:00
|
|
|
* The receiving node:
|
|
|
|
* ...
|
|
|
|
* - upon receiving `networks` containing no common chains
|
|
|
|
* - MAY fail the connection.
|
|
|
|
*/
|
|
|
|
if (tlvs->networks) {
|
2020-05-06 12:41:54 +02:00
|
|
|
if (!contains_common_chain(tlvs->networks)) {
|
2019-11-28 15:11:07 +01:00
|
|
|
status_peer_debug(&peer->id,
|
|
|
|
"No common chain with this peer '%s', closing",
|
|
|
|
tal_hex(tmpctx, msg));
|
2021-02-03 03:51:41 +01:00
|
|
|
msg = towire_warningfmt(NULL, NULL, "No common network");
|
2019-12-25 21:11:04 +01:00
|
|
|
msg = cryptomsg_encrypt_msg(NULL, &peer->cs, take(msg));
|
|
|
|
return io_write(conn, msg, tal_count(msg), io_close_cb, NULL);
|
2019-11-28 15:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 04:52:04 +02:00
|
|
|
/* The globalfeatures field is now unused, but there was a
|
|
|
|
* window where it was: combine the two. */
|
2020-01-30 15:40:09 +01:00
|
|
|
features = featurebits_or(tmpctx, take(features), globalfeatures);
|
2019-10-11 04:52:04 +02:00
|
|
|
|
2021-12-29 04:26:43 +01:00
|
|
|
/* We can dispose of peer after next call. */
|
|
|
|
tal_steal(tmpctx, peer);
|
|
|
|
|
2018-09-27 23:04:19 +02:00
|
|
|
/* Usually return io_close_taken_fd, but may wait for old peer to
|
|
|
|
* be disconnected if it's a reconnect. */
|
|
|
|
return peer_connected(conn, peer->daemon, &peer->id,
|
2019-06-03 20:14:25 +02:00
|
|
|
&peer->addr, &peer->cs,
|
2021-03-24 05:37:50 +01:00
|
|
|
take(features),
|
|
|
|
peer->incoming);
|
2018-09-27 23:04:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_init_hdr_received(struct io_conn *conn,
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer *peer)
|
2018-09-27 23:04:19 +02:00
|
|
|
{
|
|
|
|
u16 len;
|
|
|
|
|
|
|
|
if (!cryptomsg_decrypt_header(&peer->cs, peer->msg, &len))
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
tal_free(peer->msg);
|
|
|
|
peer->msg = tal_arr(peer, u8, (u32)len + CRYPTOMSG_BODY_OVERHEAD);
|
|
|
|
return io_read(conn, peer->msg, tal_count(peer->msg),
|
|
|
|
peer_init_received, peer);
|
|
|
|
}
|
|
|
|
|
2021-12-29 04:26:43 +01:00
|
|
|
static struct io_plan *read_init(struct io_conn *conn, struct early_peer *peer)
|
2018-09-27 23:04:19 +02:00
|
|
|
{
|
|
|
|
/* Free our sent init msg. */
|
|
|
|
tal_free(peer->msg);
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The receiving node:
|
|
|
|
* - MUST wait to receive `init` before sending any other messages.
|
|
|
|
*/
|
|
|
|
peer->msg = tal_arr(peer, u8, CRYPTOMSG_HDR_SIZE);
|
|
|
|
return io_read(conn, peer->msg, tal_bytelen(peer->msg),
|
|
|
|
peer_init_hdr_received, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
static struct io_plan *peer_write_postclose(struct io_conn *conn,
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer *peer)
|
2018-09-27 23:04:19 +02:00
|
|
|
{
|
2021-06-14 23:07:36 +02:00
|
|
|
dev_sabotage_fd(io_conn_fd(conn), true);
|
|
|
|
return read_init(conn, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *peer_write_post_sabotage(struct io_conn *conn,
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer *peer)
|
2021-06-14 23:07:36 +02:00
|
|
|
{
|
|
|
|
dev_sabotage_fd(io_conn_fd(conn), false);
|
2018-09-27 23:04:19 +02:00
|
|
|
return read_init(conn, peer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct io_plan *peer_exchange_initmsg(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
2020-04-03 02:03:59 +02:00
|
|
|
const struct feature_set *our_features,
|
2018-09-27 23:04:19 +02:00
|
|
|
const struct crypto_state *cs,
|
2019-04-08 11:58:32 +02:00
|
|
|
const struct node_id *id,
|
2021-03-24 05:37:50 +01:00
|
|
|
const struct wireaddr_internal *addr,
|
2022-01-08 14:17:29 +01:00
|
|
|
struct oneshot *timeout,
|
2021-03-24 05:37:50 +01:00
|
|
|
bool incoming)
|
2018-09-27 23:04:19 +02:00
|
|
|
{
|
|
|
|
/* If conn is closed, forget peer */
|
2021-12-29 04:26:43 +01:00
|
|
|
struct early_peer *peer = tal(conn, struct early_peer);
|
|
|
|
struct io_plan *(*next)(struct io_conn *, struct early_peer *);
|
2019-11-28 15:11:07 +01:00
|
|
|
struct tlv_init_tlvs *tlvs;
|
2018-09-27 23:04:19 +02:00
|
|
|
|
|
|
|
peer->daemon = daemon;
|
|
|
|
peer->id = *id;
|
|
|
|
peer->addr = *addr;
|
|
|
|
peer->cs = *cs;
|
2021-03-24 05:37:50 +01:00
|
|
|
peer->incoming = incoming;
|
2018-09-27 23:04:19 +02:00
|
|
|
|
2022-01-08 14:17:29 +01:00
|
|
|
/* Attach timer to early peer, so it gets freed with it. */
|
|
|
|
notleak(tal_steal(peer, timeout));
|
|
|
|
|
2020-08-20 08:49:27 +02:00
|
|
|
/* BOLT #1:
|
2018-09-27 23:04:19 +02:00
|
|
|
*
|
|
|
|
* The sending node:
|
|
|
|
* - MUST send `init` as the first Lightning message for any
|
|
|
|
* connection.
|
2019-11-28 15:11:07 +01:00
|
|
|
* ...
|
|
|
|
* - SHOULD set `networks` to all chains it will gossip or open
|
|
|
|
* channels for.
|
2018-09-27 23:04:19 +02:00
|
|
|
*/
|
2019-11-28 15:11:07 +01:00
|
|
|
tlvs = tlv_init_tlvs_new(tmpctx);
|
2020-05-06 12:41:54 +02:00
|
|
|
tlvs->networks = tal_dup_arr(tlvs, struct bitcoin_blkid,
|
|
|
|
&chainparams->genesis_blockhash, 1, 0);
|
2019-11-28 15:11:07 +01:00
|
|
|
|
2019-10-11 04:52:04 +02:00
|
|
|
/* Initially, there were two sets of feature bits: global and local.
|
|
|
|
* Local affected peer nodes only, global affected everyone. Both were
|
|
|
|
* sent in the `init` message, but node_announcement only advertized
|
|
|
|
* globals.
|
|
|
|
*
|
|
|
|
* But we didn't have any globals for a long time, and it turned out
|
|
|
|
* that people wanted us to broadcast local features so they could do
|
|
|
|
* peer selection. We agreed that the number spaces should be distinct,
|
2019-11-23 01:19:23 +01:00
|
|
|
* but debate still raged on how to handle them.
|
2019-10-11 04:52:04 +02:00
|
|
|
*
|
|
|
|
* Meanwhile, we finally added a global bit to the spec, so now it
|
|
|
|
* matters. And LND v0.8 decided to make option_static_remotekey a
|
|
|
|
* GLOBAL bit, not a local bit, so we need to send that as a global
|
2019-11-23 01:19:23 +01:00
|
|
|
* bit here.
|
|
|
|
*
|
|
|
|
* Finally, we agreed that bits below 13 could be put in both, but
|
|
|
|
* from now on they'll all go in initfeatures. */
|
2018-09-27 23:04:19 +02:00
|
|
|
peer->msg = towire_init(NULL,
|
2020-04-03 02:03:59 +02:00
|
|
|
our_features->bits[GLOBAL_INIT_FEATURE],
|
|
|
|
our_features->bits[INIT_FEATURE],
|
2019-11-28 15:11:07 +01:00
|
|
|
tlvs);
|
2019-11-17 12:42:33 +01:00
|
|
|
status_peer_io(LOG_IO_OUT, &peer->id, peer->msg);
|
2018-09-27 23:04:19 +02:00
|
|
|
peer->msg = cryptomsg_encrypt_msg(peer, &peer->cs, take(peer->msg));
|
|
|
|
|
|
|
|
next = read_init;
|
|
|
|
#if DEVELOPER
|
|
|
|
switch (dev_disconnect(WIRE_INIT)) {
|
|
|
|
case DEV_DISCONNECT_BEFORE:
|
2021-06-14 23:07:36 +02:00
|
|
|
dev_sabotage_fd(io_conn_fd(conn), true);
|
2018-09-27 23:04:19 +02:00
|
|
|
break;
|
|
|
|
case DEV_DISCONNECT_AFTER:
|
|
|
|
next = peer_write_postclose;
|
|
|
|
break;
|
|
|
|
case DEV_DISCONNECT_BLACKHOLE:
|
|
|
|
dev_blackhole_fd(io_conn_fd(conn));
|
|
|
|
break;
|
|
|
|
case DEV_DISCONNECT_NORMAL:
|
|
|
|
break;
|
2021-06-14 23:07:36 +02:00
|
|
|
case DEV_DISCONNECT_DISABLE_AFTER:
|
|
|
|
next = peer_write_post_sabotage;
|
|
|
|
break;
|
2018-09-27 23:04:19 +02:00
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
|
|
|
return io_write(conn, peer->msg, tal_bytelen(peer->msg), next, peer);
|
|
|
|
}
|