utils: fix memory leak in network_get_endpoint()
[project/unetd.git] / wg.h
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
4 */
5 #ifndef __UNETD_WG_H
6 #define __UNETD_WG_H
7
8 #define WG_KEY_LEN 32
9 #define WG_KEY_LEN_HEX (WG_KEY_LEN * 2 + 1)
10
11 enum wg_update_cmd {
12 WG_PEER_CREATE,
13 WG_PEER_UPDATE,
14 WG_PEER_DELETE,
15 };
16
17 struct network;
18 struct network_peer;
19 union network_endpoint;
20
21 struct wg_ops {
22 const char *name;
23
24 bool (*check)(struct network *net);
25
26 int (*init)(struct network *net);
27 void (*cleanup)(struct network *net);
28 int (*init_local)(struct network *net, struct network_peer *peer);
29 int (*peer_refresh)(struct network *net);
30 int (*peer_update)(struct network *net, struct network_peer *peer,
31 enum wg_update_cmd cmd);
32 int (*peer_connect)(struct network *net, struct network_peer *peer,
33 union network_endpoint *ep);
34 };
35
36 struct wg {
37 const struct wg_ops *ops;
38 };
39
40 extern const struct wg_ops wg_dummy_ops;
41 extern const struct wg_ops wg_user_ops;
42 extern const struct wg_ops wg_linux_ops;
43
44 int wg_init_network(struct network *net);
45 void wg_cleanup_network(struct network *net);
46
47 #define wg_init_local(net, ...) (net)->wg.ops->init_local(net, ##__VA_ARGS__)
48 #define wg_peer_update(net, ...) (net)->wg.ops->peer_update(net, ##__VA_ARGS__)
49 #define wg_peer_connect(net, ...) (net)->wg.ops->peer_connect(net, ##__VA_ARGS__)
50 #define wg_peer_refresh(net) (net)->wg.ops->peer_refresh(net)
51
52 /* internal */
53 struct network_peer *wg_peer_update_start(struct network *net, const uint8_t *key);
54 void wg_peer_update_done(struct network *net, struct network_peer *peer);
55 void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer,
56 uint64_t now, uint64_t sec);
57 void wg_peer_set_rx_bytes(struct network *net, struct network_peer *peer,
58 uint64_t bytes);
59 void wg_peer_set_endpoint(struct network *net, struct network_peer *peer,
60 void *data, size_t len);
61
62 #endif