2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2019-12-04 23:02:54 +01:00
|
|
|
#include <assert.h>
|
2021-05-08 23:09:41 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2018-01-31 03:53:35 +01:00
|
|
|
#include <common/crypto_sync.h>
|
2019-09-06 08:09:36 +02:00
|
|
|
#include <common/gossip_rcvd_filter.h>
|
2019-05-06 04:00:06 +02:00
|
|
|
#include <common/gossip_store.h>
|
2018-02-19 02:06:15 +01:00
|
|
|
#include <common/peer_failed.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2021-10-07 14:53:18 +02:00
|
|
|
#include <common/ping.h>
|
2018-01-31 03:53:35 +01:00
|
|
|
#include <common/read_peer_msg.h>
|
|
|
|
#include <common/status.h>
|
|
|
|
#include <common/wire_error.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wire/peer_wire.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
u8 *peer_or_gossip_sync_read(const tal_t *ctx,
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps,
|
2018-08-02 08:49:55 +02:00
|
|
|
bool *from_gossipd)
|
|
|
|
{
|
|
|
|
fd_set readfds;
|
|
|
|
u8 *msg;
|
|
|
|
|
2019-06-03 20:15:25 +02:00
|
|
|
for (;;) {
|
|
|
|
struct timeval tv, *tptr;
|
|
|
|
struct timerel trel;
|
|
|
|
|
|
|
|
if (time_to_next_gossip(pps, &trel)) {
|
|
|
|
tv = timerel_to_timeval(trel);
|
|
|
|
tptr = &tv;
|
|
|
|
} else
|
|
|
|
tptr = NULL;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(pps->peer_fd, &readfds);
|
|
|
|
FD_SET(pps->gossip_fd, &readfds);
|
|
|
|
|
|
|
|
if (select(pps->peer_fd > pps->gossip_fd
|
|
|
|
? pps->peer_fd + 1 : pps->gossip_fd + 1,
|
|
|
|
&readfds, NULL, NULL, tptr) != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* We timed out; look in gossip_store. Failure resets timer. */
|
|
|
|
msg = gossip_store_next(tmpctx, pps);
|
|
|
|
if (msg) {
|
|
|
|
*from_gossipd = true;
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 08:49:55 +02:00
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
if (FD_ISSET(pps->peer_fd, &readfds)) {
|
|
|
|
msg = sync_crypto_read(ctx, pps);
|
2019-01-29 01:41:55 +01:00
|
|
|
*from_gossipd = false;
|
2018-08-02 08:49:55 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
msg = wire_sync_read(ctx, pps->gossip_fd);
|
2019-01-29 01:41:55 +01:00
|
|
|
if (!msg)
|
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
|
|
"Error reading gossip msg: %s",
|
|
|
|
strerror(errno));
|
|
|
|
*from_gossipd = true;
|
2018-08-02 08:49:55 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_peer_error(const tal_t *ctx, const u8 *msg,
|
|
|
|
const struct channel_id *channel_id,
|
2021-02-02 13:47:01 +01:00
|
|
|
char **desc, bool *warning)
|
2018-08-02 08:49:55 +02:00
|
|
|
{
|
|
|
|
struct channel_id err_chanid;
|
|
|
|
|
2021-02-02 13:46:01 +01:00
|
|
|
if (fromwire_peektype(msg) == WIRE_ERROR)
|
|
|
|
*warning = false;
|
|
|
|
else if (fromwire_peektype(msg) == WIRE_WARNING)
|
|
|
|
*warning = true;
|
|
|
|
else
|
2018-08-02 08:49:55 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*desc = sanitize_error(ctx, msg, &err_chanid);
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The channel is referred to by `channel_id`, unless `channel_id` is
|
|
|
|
* 0 (i.e. all bytes are 0), in which case it refers to all channels.
|
|
|
|
* ...
|
|
|
|
* The receiving node:
|
|
|
|
* - upon receiving `error`:
|
|
|
|
* - MUST fail the channel referred to by the error message, if that
|
|
|
|
* channel is with the sending node.
|
|
|
|
* - if no existing channel is referred to by the message:
|
|
|
|
* - MUST ignore the message.
|
|
|
|
*/
|
2021-02-02 13:47:01 +01:00
|
|
|
/* FIXME: The spec changed, so for *errors* all 0 is not special.
|
|
|
|
* But old gossipd would send these, so we turn them into warnings */
|
|
|
|
if (channel_id_is_all(&err_chanid))
|
|
|
|
*warning = true;
|
|
|
|
else if (!channel_id_eq(&err_chanid, channel_id))
|
2018-08-02 08:49:55 +02:00
|
|
|
*desc = tal_free(*desc);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_wrong_channel(const u8 *msg, const struct channel_id *expected,
|
|
|
|
struct channel_id *actual)
|
|
|
|
{
|
|
|
|
if (!extract_channel_id(msg, actual))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !channel_id_eq(expected, actual);
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
void handle_gossip_msg(struct per_peer_state *pps, const u8 *msg TAKES)
|
2018-04-26 06:51:01 +02:00
|
|
|
{
|
|
|
|
u8 *gossip;
|
|
|
|
|
2021-05-22 09:09:53 +02:00
|
|
|
/* It's a raw gossip msg: this copies or takes() */
|
|
|
|
gossip = tal_dup_talarr(tmpctx, u8, msg);
|
2018-04-26 06:51:01 +02:00
|
|
|
|
2021-02-03 03:51:41 +01:00
|
|
|
/* Gossipd can send us gossip messages, OR warnings */
|
|
|
|
if (fromwire_peektype(gossip) == WIRE_WARNING) {
|
2019-06-03 20:11:25 +02:00
|
|
|
sync_crypto_write(pps, gossip);
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-04-26 06:51:01 +02:00
|
|
|
} else {
|
2019-06-03 20:19:25 +02:00
|
|
|
sync_crypto_write(pps, gossip);
|
2018-04-26 06:51:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:19:25 +02:00
|
|
|
/* takes iff returns true */
|
|
|
|
bool handle_timestamp_filter(struct per_peer_state *pps, const u8 *msg TAKES)
|
|
|
|
{
|
2021-05-08 23:09:41 +02:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2019-06-03 20:19:25 +02:00
|
|
|
u32 first_timestamp, timestamp_range;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_timestamp_filter(msg, &chain_hash,
|
|
|
|
&first_timestamp,
|
|
|
|
×tamp_range)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-08 23:09:41 +02:00
|
|
|
if (!bitcoin_blkid_eq(&chainparams->genesis_blockhash, &chain_hash)) {
|
|
|
|
sync_crypto_write(pps,
|
|
|
|
take(towire_warningfmt(NULL, NULL,
|
|
|
|
"gossip_timestamp_filter"
|
|
|
|
" for bad chain: %s",
|
|
|
|
tal_hex(tmpctx, take(msg)))));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:19:25 +02:00
|
|
|
gossip_setup_timestamp_filter(pps, first_timestamp, timestamp_range);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
bool handle_peer_gossip_or_error(struct per_peer_state *pps,
|
2018-08-02 08:49:55 +02:00
|
|
|
const struct channel_id *channel_id,
|
2019-12-13 11:16:14 +01:00
|
|
|
bool soft_error,
|
2018-08-02 08:49:55 +02:00
|
|
|
const u8 *msg TAKES)
|
|
|
|
{
|
|
|
|
char *err;
|
2021-02-02 13:47:01 +01:00
|
|
|
bool warning;
|
2021-10-07 14:53:18 +02:00
|
|
|
u8 *pong;
|
2018-08-02 08:49:55 +02:00
|
|
|
|
2019-12-04 23:02:54 +01:00
|
|
|
#if DEVELOPER
|
|
|
|
/* Any odd-typed unknown message is handled by the caller, so if we
|
|
|
|
* find one here it's an error. */
|
|
|
|
assert(!is_unknown_msg_discardable(msg));
|
|
|
|
#else
|
2019-08-05 06:48:05 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* A receiving node:
|
|
|
|
* - upon receiving a message of _odd_, unknown type:
|
|
|
|
* - MUST ignore the received message.
|
|
|
|
*/
|
|
|
|
if (is_unknown_msg_discardable(msg))
|
|
|
|
goto handled;
|
2019-12-04 23:02:54 +01:00
|
|
|
#endif
|
2019-08-05 06:48:05 +02:00
|
|
|
|
2019-06-03 20:19:25 +02:00
|
|
|
if (handle_timestamp_filter(pps, msg))
|
|
|
|
return true;
|
2021-10-07 14:53:18 +02:00
|
|
|
else if (check_ping_make_pong(NULL, msg, &pong)) {
|
|
|
|
if (pong)
|
|
|
|
sync_crypto_write(pps, take(pong));
|
|
|
|
return true;
|
|
|
|
} else if (is_msg_for_gossipd(msg)) {
|
2019-09-06 08:09:36 +02:00
|
|
|
gossip_rcvd_filter_add(pps->grf, msg);
|
2019-06-03 20:11:25 +02:00
|
|
|
wire_sync_write(pps->gossip_fd, msg);
|
2018-08-02 08:49:55 +02:00
|
|
|
/* wire_sync_write takes, so don't take again. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:47:01 +01:00
|
|
|
if (is_peer_error(tmpctx, msg, channel_id, &err, &warning)) {
|
2018-08-02 08:49:55 +02:00
|
|
|
/* Ignore unknown channel errors. */
|
2021-02-02 13:47:01 +01:00
|
|
|
if (!err)
|
|
|
|
goto handled;
|
|
|
|
|
|
|
|
/* We hang up when a warning is received. */
|
|
|
|
peer_failed_received_errmsg(pps, err, channel_id,
|
|
|
|
soft_error || warning);
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
goto handled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
handled:
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
return true;
|
|
|
|
}
|