Skip to content

Commit c71c299

Browse files
committed
net: wg: Make getting current time extensible
Allow user to provide a function that will need to get the current time from a RTC or SNTP or similar. Wireguard handshake replay prevention needs a monotonic time so the application should get it from somewhere. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent d830ac1 commit c71c299

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

include/zephyr/net/wireguard.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ int wireguard_peer_remove(int peer_id);
201201
*/
202202
int wireguard_peer_keepalive(int peer_id);
203203

204+
/**
205+
* @brief Get current time in seconds and nanoseconds from Unix epoch
206+
*
207+
* @details This function is used to get the current time in seconds
208+
* and nanoseconds. The time is used to calculate the timestamp
209+
* in the Wireguard handshake. User can override this function
210+
* to provide the current time. The default implementation uses
211+
* k_uptime_get() to get the current time.
212+
*
213+
* @param seconds Pointer to store the current time in seconds.
214+
* @param nanoseconds Pointer to store the current time in nanoseconds.
215+
*
216+
* @return 0 on success, a negative errno otherwise.
217+
*/
218+
int wireguard_get_current_time(uint64_t *seconds, uint32_t *nanoseconds);
219+
204220
#ifdef __cplusplus
205221
}
206222
#endif

subsys/net/lib/wireguard/wg.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,3 +2209,16 @@ static int wg_stats_get(uint32_t mgmt_request, struct net_if *iface,
22092209
NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_STATS_GET_VPN, wg_stats_get);
22102210

22112211
#endif /* CONFIG_NET_STATISTICS_USER_API && CONFIG_NET_STATISTICS_VPN */
2212+
2213+
static int get_current_time(uint64_t *seconds, uint32_t *nanoseconds)
2214+
{
2215+
uint64_t millis = k_uptime_get();
2216+
2217+
*seconds = millis / MSEC_PER_SEC;
2218+
*nanoseconds = (millis % MSEC_PER_SEC) * NSEC_PER_MSEC;
2219+
2220+
return 0;
2221+
}
2222+
2223+
/* Declare a default function but allow the user to override it */
2224+
__weak FUNC_ALIAS(get_current_time, wireguard_get_current_time, int);

subsys/net/lib/wireguard/wg_crypto.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,21 @@ void wg_tai64n_now(uint8_t *output)
4646
* 64 bit seconds from 1970 = 8 bytes
4747
* 32 bit nano seconds from current second
4848
*/
49-
uint64_t millis = k_ticks_to_ms_floor64(sys_clock_tick_get());
49+
uint64_t seconds;
50+
uint32_t nanoseconds;
51+
int ret;
52+
53+
ret = wireguard_get_current_time(&seconds, &nanoseconds);
54+
if (ret < 0) {
55+
NET_DBG("Failed to get current time");
56+
return;
57+
}
5058

51-
/* Split into seconds offset + nanos */
52-
uint64_t seconds = 0x400000000000000aULL + (millis / 1000);
53-
uint32_t nanos = (millis % 1000) * 1000;
59+
/* Seconds in TAI64N format */
60+
seconds += 0x400000000000000aULL;
5461

5562
sys_put_be64(seconds, output);
56-
sys_put_be32(nanos, output + 8U);
63+
sys_put_be32(nanoseconds, output + 8U);
5764
}
5865

5966
static void wg_mac(uint8_t *dst, const void *message, size_t len,

0 commit comments

Comments
 (0)