Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipdiscovery: adds autobool config switch #5841

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions doc/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ Note that if you already have a channel open to them, you'll need to close it be
There is no risk to your channels if your IP address changes.
Other nodes might not be able to connect to you, but your node can still connect to them.
But Core Lightning also has an integrated IPv4/6 address discovery mechanism.
If your node detects an new public address, it will update its announcement.
If your node detects an new public address, it can update its announcement.
For this to work binhind a NAT router you need to forward the default TCP port 9735 to your node.
IP discovery is only active if no other addresses are announced.

Note: Per default and for privacy reasons IP discovery will only be active
if no other addresses would be announced (as kind of a fallback).
You can set `--announce-addr-discovered=true` to explicitly activate it.
Your node will then update discovered IP addresses even if it also announces e.g. a TOR address.

Alternatively, you can [setup a TOR hidden service](TOR.md) for your node that
will also work well behind NAT firewalls.
Expand Down
17 changes: 11 additions & 6 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,19 @@ doc/index.rst: $(MANPAGES:=.md)
)

# For CI to (very roughly!) check that we only deprecated fields, or labelled added ones

# So GitHub renamed master to main. This is painful.
# When running on GitHub (CI=true), we need to fetch origin/master
schema-added-check:
@if ! git describe master >/dev/null 2>&1; then MASTER=main; else MASTER=master; fi; if git diff $$MASTER doc/schemas | grep -q '^+.*{' && ! git diff master doc/schemas | grep -q '^+.*"added"'; then echo 'New schema fields must have "added": "vNEXTVERSION"' >&2; exit 1; fi

# So GitHub renamed master to main. This is painful.
@if ! test -z $$CI; then git fetch origin master; fi; \
if git diff origin/master -- doc/schemas | grep -q '^+.*{' && ! git diff origin/master -- doc/schemas | grep -q '^+.*"added"'; then \
git diff origin/master -- doc/schemas; \
echo 'New schema fields must have "added": "vNEXTVERSION"' >&2; exit 1; \
fi
schema-removed-check:
@if ! git describe master >/dev/null 2>&1; then MASTER=main; else MASTER=master; fi; if git diff $$MASTER doc/schemas | grep -q '^-.*{' && ! git diff master doc/schemas | grep -q '^-.*"deprecated": "'; then echo 'Schema fields must be deprecated, with version, not removed' >&2; exit 1; fi
@if ! test -z $$CI; then git fetch origin master; fi; \
if git diff origin/master -- doc/schemas | grep -q '^-.*{' && ! git diff origin/master -- doc/schemas | grep -q '^-.*"deprecated"'; then \
git diff origin/master -- doc/schemas ; \
echo 'Schema fields must be "deprecated", with version, not removed' >&2; exit 1; \
fi

schema-diff-check: schema-added-check schema-removed-check

Expand Down
9 changes: 7 additions & 2 deletions doc/TOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ network between you and the Internet, as long as you can use Tor you can
be connected to.

Note: Core Lightning also support IPv4/6 address discovery behind NAT routers.
For this to work you need to forward the default TCP port 9735 to your node.
If your node detects an new public address, it can update its announcement.
For this to work you need to forward the TCP port 9735 on your NAT router to your node.
In this case you don't need TOR to punch through your firewall.
IP discovery is only active if no other addresses are announced.

Note: Per default and for privacy reasons IP discovery will only be active
if no other addresses would be announced (as kind of a fallback).
You can set `--announce-addr-discovered=true` to explicitly activate it.
Your node will then update discovered IP addresses even if it also announces e.g. a TOR address.
This usually has the benefit of quicker and more stable connections but does not
offer additional privacy.

Expand Down
5 changes: 3 additions & 2 deletions doc/lightning-listconfigs.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ On success, an object is returned, containing:
- **autolisten** (boolean, optional): `autolisten` field from config or cmdline, or default
- **proxy** (string, optional): `proxy` field from config or cmdline, or default
- **disable-dns** (boolean, optional): `true` if `disable-dns` was set in config or cmdline
- **disable-ip-discovery** (boolean, optional): `true` if `disable-ip-discovery` was set in config or cmdline
- **disable-ip-discovery** (boolean, optional): `true` if `disable-ip-discovery` was set in config or cmdline **deprecated, removal in v23.11**
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved
- **announce-addr-discovered** (string, optional): `true`/`false`/`auto` depending on how `announce-addr-discovered` was set in config or cmdline *(added v23.02)*
- **encrypted-hsm** (boolean, optional): `true` if `encrypted-hsm` was set in config or cmdline
- **rpc-file-mode** (string, optional): `rpc-file-mode` field from config or cmdline, or default
- **log-level** (string, optional): `log-level` field from config or cmdline, or default
Expand Down Expand Up @@ -220,4 +221,4 @@ RESOURCES

Main web site: <https://github.com/ElementsProject/lightning>

[comment]: # ( SHA256STAMP:bc7c3374ba6609553f431deae62c1e5525e136086b39fffb6c674a58365c0740)
[comment]: # ( SHA256STAMP:9953b3545acb82bed816b86a65ba51ff4b043d3848c4a3ae460aa68db1a4b542)
11 changes: 5 additions & 6 deletions doc/lightningd-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,12 @@ RPC call lightning-setchannel(7).
channels. If you want to change the `htlc_maximum_msat` for existing channels,
use the RPC call lightning-setchannel(7).

* **disable-ip-discovery**
* **announce-addr-discovered**=*BOOL*

Turn off public IP discovery to send `node_announcement` updates that contain
the discovered IP with TCP port 9735 as announced address. If unset and you
open TCP port 9735 on your router towords your node, your node will remain
connectable on changing IP addresses. Note: Will always be disabled if you use
'always-use-proxy'.
Explicitly control the usage of discovered public IPs in `node_announcement` updates.
Default: 'auto' - Only if we don't have anything else to announce.
Note: You also need to open TCP port 9735 on your router towords your node.
Note: Will always be disabled if you use 'always-use-proxy'.

### Lightning channel and HTLC options

Expand Down
8 changes: 7 additions & 1 deletion doc/schemas/listconfigs.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@
},
"disable-ip-discovery": {
"type": "boolean",
"description": "`true` if `disable-ip-discovery` was set in config or cmdline"
"description": "`true` if `disable-ip-discovery` was set in config or cmdline",
"deprecated": "v23.02"
},
"announce-addr-discovered": {
"type": "string",
"description": "`true`/`false`/`auto` depending on how `announce-addr-discovered` was set in config or cmdline",
"added": "v23.02"
},
"encrypted-hsm": {
"type": "boolean",
Expand Down
7 changes: 5 additions & 2 deletions gossipd/gossip_generation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "config.h"
#include <ccan/asort/asort.h>
#include <ccan/cast/cast.h>
#include <ccan/ccan/opt/opt.h>
#include <ccan/mem/mem.h>
#include <common/daemon_conn.h>
#include <common/features.h>
Expand Down Expand Up @@ -44,8 +45,10 @@ static u8 *create_node_announcement(const tal_t *ctx, struct daemon *daemon,
tal_arr_expand(&was, daemon->announceable[i]);

/* Add discovered IPs v4/v6 verified by peer `remote_addr` feature. */
/* Only do that if we don't have addresses announced. */
if (count_announceable == 0) {
/* Only do that if we don't have any addresses announced or
* `config.ip_discovery` is explicitly enabled. */
if ((daemon->ip_discovery == OPT_AUTOBOOL_AUTO && count_announceable == 0) ||
daemon->ip_discovery == OPT_AUTOBOOL_TRUE) {
cdecker marked this conversation as resolved.
Show resolved Hide resolved
if (daemon->discovered_ip_v4 != NULL &&
!wireaddr_arr_contains(was, daemon->discovered_ip_v4))
tal_arr_expand(&was, *daemon->discovered_ip_v4);
Expand Down
12 changes: 9 additions & 3 deletions gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ static void handle_local_private_channel(struct daemon *daemon, const u8 *msg)
static void handle_discovered_ip(struct daemon *daemon, const u8 *msg)
{
struct wireaddr discovered_ip;
size_t count_announceable;

if (!fromwire_gossipd_discovered_ip(msg, &discovered_ip))
master_badmsg(WIRE_GOSSIPD_DISCOVERED_IP, msg);
Expand Down Expand Up @@ -380,8 +381,11 @@ static void handle_discovered_ip(struct daemon *daemon, const u8 *msg)
return;

update_node_annoucement:
status_debug("Update our node_announcement for discovered address: %s",
fmt_wireaddr(tmpctx, &discovered_ip));
count_announceable = tal_count(daemon->announceable);
if ((daemon->ip_discovery == OPT_AUTOBOOL_AUTO && count_announceable == 0) ||
daemon->ip_discovery == OPT_AUTOBOOL_TRUE)
status_debug("Update our node_announcement for discovered address: %s",
fmt_wireaddr(tmpctx, &discovered_ip));
maybe_send_own_node_announce(daemon, false);
}

Expand Down Expand Up @@ -727,7 +731,8 @@ static void gossip_init(struct daemon *daemon, const u8 *msg)
&daemon->announceable,
&dev_gossip_time,
&dev_fast_gossip,
&dev_fast_gossip_prune)) {
&dev_fast_gossip_prune,
&daemon->ip_discovery)) {
master_badmsg(WIRE_GOSSIPD_INIT, msg);
}

Expand Down Expand Up @@ -1096,6 +1101,7 @@ int main(int argc, char *argv[])
daemon->rates = NULL;
daemon->discovered_ip_v4 = NULL;
daemon->discovered_ip_v6 = NULL;
daemon->ip_discovery = OPT_AUTOBOOL_AUTO;
list_head_init(&daemon->deferred_updates);

/* Tell the ecdh() function how to talk to hsmd */
Expand Down
3 changes: 3 additions & 0 deletions gossipd/gossipd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef LIGHTNING_GOSSIPD_GOSSIPD_H
#define LIGHTNING_GOSSIPD_GOSSIPD_H
#include "config.h"
#include <ccan/ccan/opt/opt.h>
#include <ccan/timer/timer.h>
#include <common/node_id.h>
#include <lightningd/options.h>
#include <wire/peer_wire.h>

/* We talk to `hsmd` to sign our gossip messages with the node key */
Expand Down Expand Up @@ -51,6 +53,7 @@ struct daemon {
/* verified remote_addr as reported by recent peers */
struct wireaddr *discovered_ip_v4;
struct wireaddr *discovered_ip_v6;
enum opt_autobool ip_discovery;

/* Timer until we can send an updated node_announcement */
struct oneshot *node_announce_timer;
Expand Down
1 change: 1 addition & 0 deletions gossipd/gossipd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ msgdata,gossipd_init,announceable,wireaddr,num_announceable
msgdata,gossipd_init,dev_gossip_time,?u32,
msgdata,gossipd_init,dev_fast_gossip,bool,
msgdata,gossipd_init,dev_fast_gossip_prune,bool,
msgdata,gossipd_init,ip_discovery,u32,
m-schmoock marked this conversation as resolved.
Show resolved Hide resolved

msgtype,gossipd_init_reply,3100

Expand Down
3 changes: 2 additions & 1 deletion lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ void gossip_init(struct lightningd *ld, int connectd_fd)
ld->announceable,
IFDEV(ld->dev_gossip_time ? &ld->dev_gossip_time: NULL, NULL),
IFDEV(ld->dev_fast_gossip, false),
IFDEV(ld->dev_fast_gossip_prune, false));
IFDEV(ld->dev_fast_gossip_prune, false),
ld->config.ip_discovery);

subd_req(ld->gossip, ld->gossip, take(msg), -1, 0,
gossipd_init_done, NULL);
Expand Down
1 change: 0 additions & 1 deletion lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
#include <lightningd/io_loop_with_timers.h>
#include <lightningd/lightningd.h>
#include <lightningd/onchain_control.h>
#include <lightningd/options.h>
#include <lightningd/plugin.h>
#include <lightningd/subd.h>
#include <sys/resource.h>
Expand Down
6 changes: 4 additions & 2 deletions lightningd/lightningd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef LIGHTNING_LIGHTNINGD_LIGHTNINGD_H
#define LIGHTNING_LIGHTNINGD_LIGHTNINGD_H
#include "config.h"
#include <ccan/ccan/opt/opt.h>
#include <lightningd/htlc_end.h>
#include <lightningd/htlc_set.h>
#include <lightningd/options.h>
#include <lightningd/peer_control.h>
#include <signal.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -57,8 +59,8 @@ struct config {
/* Are we allowed to use DNS lookup for peers. */
bool use_dns;

/* Turn off IP address announcement discovered via peer `remote_addr` */
bool disable_ip_discovery;
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
enum opt_autobool ip_discovery;

/* Minimal amount of effective funding_satoshis for accepting channels */
u64 min_capacity_sat;
Expand Down
58 changes: 51 additions & 7 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,41 @@ static char *opt_set_s32(const char *arg, s32 *u)
return NULL;
}

char *opt_set_autobool_arg(const char *arg, enum opt_autobool *b)
{
if (!strcasecmp(arg, "yes") ||
!strcasecmp(arg, "true")) {
*b = OPT_AUTOBOOL_TRUE;
return NULL;
}
if (!strcasecmp(arg, "no") ||
!strcasecmp(arg, "false")) {
*b = OPT_AUTOBOOL_FALSE;
return NULL;
}
if (!strcasecmp(arg, "auto") ||
!strcasecmp(arg, "default")) {
*b = OPT_AUTOBOOL_AUTO;
return NULL;
}
return opt_invalid_argument(arg);
}

void opt_show_autobool(char buf[OPT_SHOW_LEN], const enum opt_autobool *b)
{
switch (*b) {
case OPT_AUTOBOOL_TRUE:
strncpy(buf, "true", OPT_SHOW_LEN);
break;
case OPT_AUTOBOOL_FALSE:
strncpy(buf, "false", OPT_SHOW_LEN);
break;
case OPT_AUTOBOOL_AUTO:
default:
strncpy(buf, "auto", OPT_SHOW_LEN);
}
}

static char *opt_set_mode(const char *arg, mode_t *m)
{
char *endp;
Expand Down Expand Up @@ -808,8 +843,8 @@ static const struct config testnet_config = {

.use_dns = true,

/* Turn off IP address announcement discovered via peer `remote_addr` */
.disable_ip_discovery = false,
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
.ip_discovery = OPT_AUTOBOOL_AUTO,

/* Sets min_effective_htlc_capacity - at 1000$/BTC this is 10ct */
.min_capacity_sat = 10000,
Expand Down Expand Up @@ -874,8 +909,8 @@ static const struct config mainnet_config = {

.use_dns = true,

/* Turn off IP address announcement discovered via peer `remote_addr` */
.disable_ip_discovery = false,
/* Excplicitly turns 'on' or 'off' IP discovery feature. */
.ip_discovery = OPT_AUTOBOOL_AUTO,

/* Sets min_effective_htlc_capacity - at 1000$/BTC this is 10ct */
.min_capacity_sat = 10000,
Expand Down Expand Up @@ -1045,6 +1080,13 @@ static char *opt_set_db_upgrade(const char *arg, struct lightningd *ld)
return opt_set_bool_arg(arg, ld->db_upgrade_ok);
}

static char *opt_disable_ip_discovery(struct lightningd *ld)
{
log_broken(ld->log, "--disable-ip-discovery has been deprecated, use --announce-addr-discovered=false");
ld->config.ip_discovery = OPT_AUTOBOOL_FALSE;
return NULL;
}

static void register_opts(struct lightningd *ld)
{
/* This happens before plugins started */
Expand Down Expand Up @@ -1176,9 +1218,11 @@ static void register_opts(struct lightningd *ld)
opt_register_arg("--announce-addr", opt_add_announce_addr, NULL,
ld,
"Set an IP address (v4 or v6) or .onion v3 to announce, but not listen on");
opt_register_noarg("--disable-ip-discovery", opt_set_bool,
&ld->config.disable_ip_discovery,
"Turn off announcement of discovered public IPs");

opt_register_noarg("--disable-ip-discovery", opt_disable_ip_discovery, ld, opt_hidden);
opt_register_arg("--announce-addr-discovered", opt_set_autobool_arg, opt_show_autobool,
&ld->config.ip_discovery,
"Explicitly turns IP discovery 'on' or 'off'.");

opt_register_noarg("--offline", opt_set_offline, ld,
"Start in offline-mode (do not automatically reconnect and do not accept incoming connections)");
Expand Down
9 changes: 9 additions & 0 deletions lightningd/options.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef LIGHTNING_LIGHTNINGD_OPTIONS_H
#define LIGHTNING_LIGHTNINGD_OPTIONS_H
#include "config.h"
#include <ccan/ccan/opt/opt.h>

struct lightningd;

Expand All @@ -13,4 +14,12 @@ void handle_opts(struct lightningd *ld, int argc, char *argv[]);
/* Derive default color and alias from the pubkey. */
void setup_color_and_alias(struct lightningd *ld);

enum opt_autobool {
OPT_AUTOBOOL_FALSE = 0,
OPT_AUTOBOOL_TRUE = 1,
OPT_AUTOBOOL_AUTO = 2,
};
char *opt_set_autobool_arg(const char *arg, enum opt_autobool *b);
void opt_show_autobool(char buf[OPT_SHOW_LEN], const enum opt_autobool *b);

#endif /* LIGHTNING_LIGHTNINGD_OPTIONS_H */
11 changes: 6 additions & 5 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,8 @@ static void update_remote_addr(struct lightningd *ld,
u16 public_port;

/* failsafe to prevent privacy leakage. */
if (ld->always_use_proxy || ld->config.disable_ip_discovery)
if (ld->always_use_proxy ||
ld->config.ip_discovery == OPT_AUTOBOOL_FALSE)
return;

/* Peers will have likey reported our dynamic outbound TCP port.
Expand Down Expand Up @@ -2382,10 +2383,10 @@ static struct command_result *json_getinfo(struct command *cmd,
for (size_t i = 0; i < count_announceable; i++)
json_add_address(response, NULL, cmd->ld->announceable+i);

/* Currently, IP discovery will only be announced by gossipd,
* if we don't already have usable addresses.
* See `create_node_announcement` in `gossip_generation.c`. */
if (count_announceable == 0) {
/* Add discovered IPs if we announce them.
* Also see `create_node_announcement` in `gossip_generation.c`. */
if ((cmd->ld->config.ip_discovery == OPT_AUTOBOOL_AUTO && count_announceable == 0) ||
cmd->ld->config.ip_discovery == OPT_AUTOBOOL_TRUE) {
if (cmd->ld->discovered_ip_v4 != NULL &&
!wireaddr_arr_contains(
cmd->ld->announceable,
Expand Down
Loading