unet-cli: strip initial newline in usage message
[project/unetd.git] / wg.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
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_user_ops;
41 extern const struct wg_ops wg_linux_ops;
42
43 int wg_init_network(struct network *net);
44 void wg_cleanup_network(struct network *net);
45
46 #define wg_init_local(net, ...) (net)->wg.ops->init_local(net, ##__VA_ARGS__)
47 #define wg_peer_update(net, ...) (net)->wg.ops->peer_update(net, ##__VA_ARGS__)
48 #define wg_peer_connect(net, ...) (net)->wg.ops->peer_connect(net, ##__VA_ARGS__)
49 #define wg_peer_refresh(net) (net)->wg.ops->peer_refresh(net)
50
51 /* internal */
52 struct network_peer *wg_peer_update_start(struct network *net, const uint8_t *key);
53 void wg_peer_update_done(struct network *net, struct network_peer *peer);
54 void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer,
55 uint64_t now, uint64_t sec);
56 void wg_peer_set_rx_bytes(struct network *net, struct network_peer *peer,
57 uint64_t bytes);
58 void wg_peer_set_endpoint(struct network *net, struct network_peer *peer,
59 void *data, size_t len);
60
61 #endif