2017-02-21 05:45:29 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/crypto_sync.h>
|
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/dev_disconnect.h>
|
2018-08-02 08:49:55 +02:00
|
|
|
#include <common/peer_failed.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2018-08-09 02:30:30 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/socket.h>
|
2017-05-24 12:10:16 +02:00
|
|
|
#include <wire/wire.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
void sync_crypto_write(struct per_peer_state *pps, const void *msg TAKES)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
bool post_sabotage = false;
|
2017-06-27 04:55:06 +02:00
|
|
|
int type = fromwire_peektype(msg);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2018-02-05 05:09:28 +01:00
|
|
|
u8 *enc;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2019-11-17 12:42:33 +01:00
|
|
|
status_peer_io(LOG_IO_OUT, NULL, msg);
|
2019-06-03 20:11:25 +02:00
|
|
|
enc = cryptomsg_encrypt_msg(NULL, &pps->cs, msg);
|
2018-02-05 05:09:28 +01:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-06-27 04:55:06 +02:00
|
|
|
switch (dev_disconnect(type)) {
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_BEFORE:
|
2019-06-03 20:11:25 +02:00
|
|
|
dev_sabotage_fd(pps->peer_fd);
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_DROPPKT:
|
|
|
|
enc = tal_free(enc); /* FALL THRU */
|
|
|
|
case DEV_DISCONNECT_AFTER:
|
|
|
|
post_sabotage = true;
|
|
|
|
break;
|
2017-09-06 03:07:19 +02:00
|
|
|
case DEV_DISCONNECT_BLACKHOLE:
|
2019-06-03 20:11:25 +02:00
|
|
|
dev_blackhole_fd(pps->peer_fd);
|
2017-09-06 03:07:19 +02:00
|
|
|
break;
|
2017-09-26 06:57:31 +02:00
|
|
|
case DEV_DISCONNECT_NORMAL:
|
2017-05-24 12:10:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2019-06-03 20:11:25 +02:00
|
|
|
if (!write_all(pps->peer_fd, enc, tal_count(enc)))
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(enc);
|
2017-05-24 12:10:16 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-05-24 12:10:16 +02:00
|
|
|
if (post_sabotage)
|
2019-06-03 20:11:25 +02:00
|
|
|
dev_sabotage_fd(pps->peer_fd);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:30:30 +02:00
|
|
|
/* We're happy for the kernel to batch update and gossip messages, but a
|
|
|
|
* commitment message, for example, should be instantly sent. There's no
|
|
|
|
* great way of doing this, unfortunately.
|
|
|
|
*
|
|
|
|
* Setting TCP_NODELAY on Linux flushes the socket, which really means
|
|
|
|
* we'd want to toggle on then off it *after* sending. But Linux has
|
|
|
|
* TCP_CORK. On FreeBSD, it seems (looking at source) not to, so
|
|
|
|
* there we'd want to set it before the send, and reenable it
|
|
|
|
* afterwards. Even if this is wrong on other non-Linux platforms, it
|
|
|
|
* only means one extra packet.
|
|
|
|
*/
|
2019-06-03 20:11:25 +02:00
|
|
|
void sync_crypto_write_no_delay(struct per_peer_state *pps,
|
2018-08-09 02:30:30 +02:00
|
|
|
const void *msg TAKES)
|
|
|
|
{
|
|
|
|
int val;
|
|
|
|
int opt;
|
|
|
|
const char *optname;
|
|
|
|
static bool complained = false;
|
|
|
|
|
|
|
|
#ifdef TCP_CORK
|
|
|
|
opt = TCP_CORK;
|
|
|
|
optname = "TCP_CORK";
|
|
|
|
#elif defined(TCP_NODELAY)
|
|
|
|
opt = TCP_NODELAY;
|
|
|
|
optname = "TCP_NODELAY";
|
|
|
|
#else
|
|
|
|
#error "Please report platform with neither TCP_CORK nor TCP_NODELAY?"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
val = 1;
|
2019-06-03 20:11:25 +02:00
|
|
|
if (setsockopt(pps->peer_fd, IPPROTO_TCP, opt, &val, sizeof(val)) != 0) {
|
2018-08-09 02:30:30 +02:00
|
|
|
/* This actually happens in testing, where we blackhole the fd */
|
|
|
|
if (!complained) {
|
2019-06-30 02:42:44 +02:00
|
|
|
status_unusual("setsockopt %s=1: %s",
|
|
|
|
optname,
|
|
|
|
strerror(errno));
|
2018-08-09 02:30:30 +02:00
|
|
|
complained = true;
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 20:11:25 +02:00
|
|
|
sync_crypto_write(pps, msg);
|
2018-08-09 02:30:30 +02:00
|
|
|
|
|
|
|
val = 0;
|
2019-06-03 20:11:25 +02:00
|
|
|
setsockopt(pps->peer_fd, IPPROTO_TCP, opt, &val, sizeof(val));
|
2018-08-09 02:30:30 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
u8 *sync_crypto_read(const tal_t *ctx, struct per_peer_state *pps)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
|
|
|
u8 hdr[18], *enc, *dec;
|
|
|
|
u16 len;
|
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
if (!read_all(pps->peer_fd, hdr, sizeof(hdr))) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed reading header: %s", strerror(errno));
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
if (!cryptomsg_decrypt_header(&pps->cs, hdr, &len)) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed hdr decrypt with rn=%"PRIu64,
|
2019-06-03 20:11:25 +02:00
|
|
|
pps->cs.rn-1);
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-02-24 06:52:35 +01:00
|
|
|
enc = tal_arr(ctx, u8, len + 16);
|
2019-06-03 20:11:25 +02:00
|
|
|
if (!read_all(pps->peer_fd, enc, tal_count(enc))) {
|
2019-09-08 18:39:26 +02:00
|
|
|
status_debug("Failed reading body: %s", strerror(errno));
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2019-06-03 20:11:25 +02:00
|
|
|
dec = cryptomsg_decrypt_body(ctx, &pps->cs, enc);
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(enc);
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!dec)
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-24 06:52:56 +01:00
|
|
|
else
|
2019-11-17 12:42:33 +01:00
|
|
|
status_peer_io(LOG_IO_IN, NULL, dec);
|
2018-02-05 05:09:28 +01:00
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
return dec;
|
|
|
|
}
|