ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
wireguard.cpp
Go to the documentation of this file.
1#include "wireguard.h"
2#ifdef USE_WIREGUARD
3#include <cinttypes>
4#include <ctime>
5
7#include "esphome/core/log.h"
8#include "esphome/core/time.h"
11
12#include <esp_wireguard.h>
13#include <esp_wireguard_err.h>
14
16
17static const char *const TAG = "wireguard";
18
19/*
20 * Cannot use `static const char*` for LOGMSG_PEER_STATUS on esp8266 platform
21 * because log messages in `Wireguard::update()` method fail.
22 */
23#define LOGMSG_PEER_STATUS "Remote peer is %s (latest handshake %s)"
24
25static const char *const LOGMSG_ONLINE = "online";
26static const char *const LOGMSG_OFFLINE = "offline";
27
29 this->wg_config_.address = this->address_;
30 this->wg_config_.private_key = this->private_key_;
31 this->wg_config_.endpoint = this->peer_endpoint_;
32 this->wg_config_.public_key = this->peer_public_key_;
33 this->wg_config_.port = this->peer_port_;
34 this->wg_config_.netmask = this->netmask_;
35 this->wg_config_.persistent_keepalive = this->keepalive_;
36
37 if (this->preshared_key_ != nullptr)
38 this->wg_config_.preshared_key = this->preshared_key_;
39
41
42 {
43 LwIPLock lock;
44 this->wg_initialized_ = esp_wireguard_init(&(this->wg_config_), &(this->wg_ctx_));
45 }
46
47 if (this->wg_initialized_ == ESP_OK) {
48 ESP_LOGI(TAG, "Initialized");
50 this->srctime_->add_on_time_sync_callback([this]() { this->start_connection_(); });
51 this->defer([this]() { this->start_connection_(); }); // defer to avoid blocking setup
52
53#ifdef USE_TEXT_SENSOR
54 if (this->address_sensor_ != nullptr) {
56 }
57#endif
58 } else {
59 ESP_LOGE(TAG, "Cannot initialize: error code %d", this->wg_initialized_);
60 this->mark_failed();
61 }
62}
63
65 if (!this->enabled_) {
66 return;
67 }
68
69 if ((this->wg_initialized_ == ESP_OK) && (this->wg_connected_ == ESP_OK) && (!network::is_connected())) {
70 ESP_LOGV(TAG, "Local network connection has been lost, stopping");
71 this->stop_connection_();
72 }
73}
74
76 bool peer_up = this->is_peer_up();
77 time_t lhs = this->get_latest_handshake();
78 bool lhs_updated = (lhs > this->latest_saved_handshake_);
79
80 ESP_LOGV(TAG, "enabled=%d, connected=%d, peer_up=%d, handshake: current=%.0f latest=%.0f updated=%d",
81 (int) this->enabled_, (int) (this->wg_connected_ == ESP_OK), (int) peer_up, (double) lhs,
82 (double) this->latest_saved_handshake_, (int) lhs_updated);
83
84 if (lhs_updated) {
85 this->latest_saved_handshake_ = lhs;
86 }
87
88 std::string latest_handshake =
89 (this->latest_saved_handshake_ > 0)
90 ? ESPTime::from_epoch_local(this->latest_saved_handshake_).strftime("%Y-%m-%d %H:%M:%S %Z")
91 : "timestamp not available";
92
93 if (peer_up) {
94 if (this->wg_peer_offline_time_ != 0) {
95 ESP_LOGI(TAG, LOGMSG_PEER_STATUS, LOGMSG_ONLINE, latest_handshake.c_str());
96 this->wg_peer_offline_time_ = 0;
97 } else {
98 ESP_LOGD(TAG, LOGMSG_PEER_STATUS, LOGMSG_ONLINE, latest_handshake.c_str());
99 }
100 } else {
101 if (this->wg_peer_offline_time_ == 0) {
102 ESP_LOGW(TAG, LOGMSG_PEER_STATUS, LOGMSG_OFFLINE, latest_handshake.c_str());
104 } else if (this->enabled_) {
105 ESP_LOGD(TAG, LOGMSG_PEER_STATUS, LOGMSG_OFFLINE, latest_handshake.c_str());
106 this->start_connection_();
107 }
108
109 // check reboot timeout every time the peer is down
110 if (this->enabled_ && this->reboot_timeout_ > 0) {
111 if (millis() - this->wg_peer_offline_time_ > this->reboot_timeout_) {
112 ESP_LOGE(TAG, "Remote peer is unreachable; rebooting");
113 App.reboot();
114 }
115 }
116 }
117
118#ifdef USE_BINARY_SENSOR
119 if (this->status_sensor_ != nullptr) {
120 this->status_sensor_->publish_state(peer_up);
121 }
122#endif
123
124#ifdef USE_SENSOR
125 if (this->handshake_sensor_ != nullptr && lhs_updated) {
126 this->handshake_sensor_->publish_state((double) this->latest_saved_handshake_);
127 }
128#endif
129}
130
132 char private_key_masked[MASK_KEY_BUFFER_SIZE];
133 char preshared_key_masked[MASK_KEY_BUFFER_SIZE];
134 mask_key_to(private_key_masked, sizeof(private_key_masked), this->private_key_);
135 mask_key_to(preshared_key_masked, sizeof(preshared_key_masked), this->preshared_key_);
136 // clang-format off
137 ESP_LOGCONFIG(
138 TAG,
139 "WireGuard:\n"
140 " Address: %s\n"
141 " Netmask: %s\n"
142 " Private Key: " LOG_SECRET("%s") "\n"
143 " Peer Endpoint: " LOG_SECRET("%s") "\n"
144 " Peer Port: " LOG_SECRET("%d") "\n"
145 " Peer Public Key: " LOG_SECRET("%s") "\n"
146 " Peer Pre-shared Key: " LOG_SECRET("%s"),
147 this->address_, this->netmask_, private_key_masked,
148 this->peer_endpoint_, this->peer_port_, this->peer_public_key_,
149 (this->preshared_key_ != nullptr ? preshared_key_masked : "NOT IN USE"));
150 // clang-format on
151 ESP_LOGCONFIG(TAG, " Peer Allowed IPs:");
152 for (const AllowedIP &allowed_ip : this->allowed_ips_) {
153 ESP_LOGCONFIG(TAG, " - %s/%s", allowed_ip.ip, allowed_ip.netmask);
154 }
155 ESP_LOGCONFIG(TAG, " Peer Persistent Keepalive: %d%s", this->keepalive_,
156 (this->keepalive_ > 0 ? "s" : " (DISABLED)"));
157 ESP_LOGCONFIG(TAG, " Reboot Timeout: %" PRIu32 "%s", (this->reboot_timeout_ / 1000),
158 (this->reboot_timeout_ != 0 ? "s" : " (DISABLED)"));
159 // be careful: if proceed_allowed_ is true, require connection is false
160 ESP_LOGCONFIG(TAG, " Require Connection to Proceed: %s", (this->proceed_allowed_ ? "NO" : "YES"));
161 LOG_UPDATE_INTERVAL(this);
162}
163
165
166bool Wireguard::can_proceed() { return (this->proceed_allowed_ || this->is_peer_up() || !this->enabled_); }
167
169 return (this->wg_initialized_ == ESP_OK) && (this->wg_connected_ == ESP_OK) &&
170 (esp_wireguardif_peer_is_up(&(this->wg_ctx_)) == ESP_OK);
171}
172
174 time_t result;
175 if (esp_wireguard_latest_handshake(&(this->wg_ctx_), &result) != ESP_OK) {
176 result = 0;
177 }
178 return result;
179}
180
181void Wireguard::set_keepalive(const uint16_t seconds) { this->keepalive_ = seconds; }
182void Wireguard::set_reboot_timeout(const uint32_t seconds) { this->reboot_timeout_ = seconds; }
183void Wireguard::set_srctime(time::RealTimeClock *srctime) { this->srctime_ = srctime; }
184
185#ifdef USE_BINARY_SENSOR
188#endif
189
190#ifdef USE_SENSOR
192#endif
193
194#ifdef USE_TEXT_SENSOR
196#endif
197
199
201 this->enabled_ = true;
202 ESP_LOGI(TAG, "Enabled");
203 this->publish_enabled_state();
204}
205
207 this->enabled_ = false;
208 this->defer([this]() { this->stop_connection_(); }); // defer to avoid blocking running loop
209 ESP_LOGI(TAG, "Disabled");
210 this->publish_enabled_state();
211}
212
214#ifdef USE_BINARY_SENSOR
215 if (this->enabled_sensor_ != nullptr) {
217 }
218#endif
219}
220
221bool Wireguard::is_enabled() { return this->enabled_; }
222
224 if (!this->enabled_) {
225 ESP_LOGV(TAG, "Disabled, cannot start connection");
226 return;
227 }
228
229 if (this->wg_initialized_ != ESP_OK) {
230 ESP_LOGE(TAG, "Cannot start: error code %d", this->wg_initialized_);
231 return;
232 }
233
234 if (!network::is_connected()) {
235 ESP_LOGD(TAG, "Waiting for local network connection to be available");
236 return;
237 }
238
239 if (!this->srctime_->now().is_valid()) {
240 ESP_LOGD(TAG, "Waiting for system time to be synchronized");
241 return;
242 }
243
244 if (this->wg_connected_ == ESP_OK) {
245 ESP_LOGV(TAG, "Connection already started");
246 return;
247 }
248
249 ESP_LOGD(TAG, "Starting connection");
250 {
251 LwIPLock lock;
252 this->wg_connected_ = esp_wireguard_connect(&(this->wg_ctx_));
253 }
254
255 if (this->wg_connected_ == ESP_OK) {
256 ESP_LOGI(TAG, "Connection started");
257 } else if (this->wg_connected_ == ESP_ERR_RETRY) {
258 ESP_LOGD(TAG, "Waiting for endpoint IP address to be available");
259 return;
260 } else {
261 ESP_LOGW(TAG, "Cannot start connection, error code %d", this->wg_connected_);
262 return;
263 }
264
265 ESP_LOGD(TAG, "Configuring allowed IPs list");
266 bool allowed_ips_ok = true;
267 for (const AllowedIP &ip : this->allowed_ips_) {
268 allowed_ips_ok &= (esp_wireguard_add_allowed_ip(&(this->wg_ctx_), ip.ip, ip.netmask) == ESP_OK);
269 }
270
271 if (allowed_ips_ok) {
272 ESP_LOGD(TAG, "Allowed IPs list configured correctly");
273 } else {
274 ESP_LOGE(TAG, "Cannot configure allowed IPs list, aborting");
275 this->stop_connection_();
276 this->mark_failed();
277 }
278}
279
281 if (this->wg_initialized_ == ESP_OK && this->wg_connected_ == ESP_OK) {
282 ESP_LOGD(TAG, "Stopping connection");
283 {
284 LwIPLock lock;
285 esp_wireguard_disconnect(&(this->wg_ctx_));
286 }
287 this->wg_connected_ = ESP_FAIL;
288 }
289}
290
291void mask_key_to(char *buffer, size_t len, const char *key) {
292 // Format: "XXXXX[...]=\0" = MASK_KEY_BUFFER_SIZE chars minimum
293 if (len < MASK_KEY_BUFFER_SIZE || key == nullptr) {
294 if (len > 0)
295 buffer[0] = '\0';
296 return;
297 }
298 // Copy first 5 characters of the key
299 size_t i = 0;
300 for (; i < 5 && key[i] != '\0'; ++i) {
301 buffer[i] = key[i];
302 }
303 // Append "[...]="
304 const char *suffix = "[...]=";
305 for (size_t j = 0; suffix[j] != '\0' && (i + j) < len - 1; ++j) {
306 buffer[i + j] = suffix[j];
307 }
308 buffer[i + 6] = '\0';
309}
310
311} // namespace esphome::wireguard
312#endif
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:560
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:2123
Base class for all binary_sensor-type classes.
void publish_state(bool new_state)
Publish a new state to the front-end.
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void publish_state(const std::string &state)
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
ESPTime now()
Get the time in the currently defined timezone.
void add_on_time_sync_callback(F &&callback)
binary_sensor::BinarySensor * enabled_sensor_
Definition wireguard.h:119
FixedVector< AllowedIP > allowed_ips_
Definition wireguard.h:109
void set_keepalive(uint16_t seconds)
bool enabled_
When false the wireguard link will not be established.
Definition wireguard.h:134
binary_sensor::BinarySensor * status_sensor_
Definition wireguard.h:118
void set_status_sensor(binary_sensor::BinarySensor *sensor)
void set_srctime(time::RealTimeClock *srctime)
void publish_enabled_state()
Publish the enabled state if the enabled binary sensor is configured.
time_t get_latest_handshake() const
sensor::Sensor * handshake_sensor_
Definition wireguard.h:123
time::RealTimeClock * srctime_
Definition wireguard.h:115
bool proceed_allowed_
Set to false to block the setup step until peer is connected.
Definition wireguard.h:131
void set_reboot_timeout(uint32_t seconds)
void disable_auto_proceed()
Block the setup step until peer is connected.
void set_address_sensor(text_sensor::TextSensor *sensor)
text_sensor::TextSensor * address_sensor_
Definition wireguard.h:127
uint32_t wg_peer_offline_time_
The last time the remote peer become offline.
Definition wireguard.h:143
void disable()
Stop any running connection and disable the WireGuard component.
void set_enabled_sensor(binary_sensor::BinarySensor *sensor)
void set_handshake_sensor(sensor::Sensor *sensor)
bool is_enabled()
Return if the WireGuard component is or is not enabled.
void enable()
Enable the WireGuard component.
time_t latest_saved_handshake_
The latest saved handshake.
Definition wireguard.h:151
wireguard_config_t wg_config_
Definition wireguard.h:136
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:27
void mask_key_to(char *buffer, size_t len, const char *key)
Strip most part of the key only for secure printing.
std::string size_t len
Definition helpers.h:1045
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:125
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
Definition time.cpp:18
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
Allowed IP entry for WireGuard peer configuration.
Definition wireguard.h:29