Imported Upstream connman version 1.38
[platform/upstream/connman.git] / vpn / plugins / wireguard.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  */
5
6 #ifndef WIREGUARD_H
7 #define WIREGUARD_H
8
9 #include <net/if.h>
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <time.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15
16 typedef uint8_t wg_key[32];
17 typedef char wg_key_b64_string[((sizeof(wg_key) + 2) / 3) * 4 + 1];
18
19 /* Cross platform __kernel_timespec */
20 struct timespec64 {
21         int64_t tv_sec;
22         int64_t tv_nsec;
23 };
24
25 typedef struct wg_allowedip {
26         uint16_t family;
27         union {
28                 struct in_addr ip4;
29                 struct in6_addr ip6;
30         };
31         uint8_t cidr;
32         struct wg_allowedip *next_allowedip;
33 } wg_allowedip;
34
35 enum wg_peer_flags {
36         WGPEER_REMOVE_ME = 1U << 0,
37         WGPEER_REPLACE_ALLOWEDIPS = 1U << 1,
38         WGPEER_HAS_PUBLIC_KEY = 1U << 2,
39         WGPEER_HAS_PRESHARED_KEY = 1U << 3,
40         WGPEER_HAS_PERSISTENT_KEEPALIVE_INTERVAL = 1U << 4
41 };
42
43 typedef struct wg_peer {
44         enum wg_peer_flags flags;
45
46         wg_key public_key;
47         wg_key preshared_key;
48
49         union {
50                 struct sockaddr addr;
51                 struct sockaddr_in addr4;
52                 struct sockaddr_in6 addr6;
53         } endpoint;
54
55         struct timespec64 last_handshake_time;
56         uint64_t rx_bytes, tx_bytes;
57         uint16_t persistent_keepalive_interval;
58
59         struct wg_allowedip *first_allowedip, *last_allowedip;
60         struct wg_peer *next_peer;
61 } wg_peer;
62
63 enum wg_device_flags {
64         WGDEVICE_REPLACE_PEERS = 1U << 0,
65         WGDEVICE_HAS_PRIVATE_KEY = 1U << 1,
66         WGDEVICE_HAS_PUBLIC_KEY = 1U << 2,
67         WGDEVICE_HAS_LISTEN_PORT = 1U << 3,
68         WGDEVICE_HAS_FWMARK = 1U << 4
69 };
70
71 typedef struct wg_device {
72         char name[IFNAMSIZ];
73         uint32_t ifindex;
74
75         enum wg_device_flags flags;
76
77         wg_key public_key;
78         wg_key private_key;
79
80         uint32_t fwmark;
81         uint16_t listen_port;
82
83         struct wg_peer *first_peer, *last_peer;
84 } wg_device;
85
86 #define wg_for_each_device_name(__names, __name, __len) for ((__name) = (__names), (__len) = 0; ((__len) = strlen(__name)); (__name) += (__len) + 1)
87 #define wg_for_each_peer(__dev, __peer) for ((__peer) = (__dev)->first_peer; (__peer); (__peer) = (__peer)->next_peer)
88 #define wg_for_each_allowedip(__peer, __allowedip) for ((__allowedip) = (__peer)->first_allowedip; (__allowedip); (__allowedip) = (__allowedip)->next_allowedip)
89
90 int wg_set_device(wg_device *dev);
91 int wg_get_device(wg_device **dev, const char *device_name);
92 int wg_add_device(const char *device_name);
93 int wg_del_device(const char *device_name);
94 void wg_free_device(wg_device *dev);
95 char *wg_list_device_names(void); /* first\0second\0third\0forth\0last\0\0 */
96 void wg_key_to_base64(wg_key_b64_string base64, const wg_key key);
97 int wg_key_from_base64(wg_key key, const wg_key_b64_string base64);
98 bool wg_key_is_zero(const wg_key key);
99 void wg_generate_public_key(wg_key public_key, const wg_key private_key);
100 void wg_generate_private_key(wg_key private_key);
101 void wg_generate_preshared_key(wg_key preshared_key);
102
103 #endif