wireguard: initial implementation of wireguard protocol
[vpp.git] / src / plugins / wireguard / wireguard_peer.h
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __included_wg_peer_h__
17 #define __included_wg_peer_h__
18
19 #include <vnet/ip/ip.h>
20
21 #include <wireguard/wireguard_cookie.h>
22 #include <wireguard/wireguard_timer.h>
23 #include <wireguard/wireguard_key.h>
24 #include <wireguard/wireguard_messages.h>
25 #include <wireguard/wireguard_if.h>
26
27 typedef struct ip4_udp_header_t_
28 {
29   ip4_header_t ip4;
30   udp_header_t udp;
31 } __clib_packed ip4_udp_header_t;
32
33 u8 *format_ip4_udp_header (u8 * s, va_list * va);
34
35 typedef struct wg_peer_allowed_ip_t_
36 {
37   fib_prefix_t prefix;
38   fib_node_index_t fib_entry_index;
39 } wg_peer_allowed_ip_t;
40
41 typedef struct wg_peer_endpoint_t_
42 {
43   ip46_address_t addr;
44   u16 port;
45 } wg_peer_endpoint_t;
46
47 typedef struct wg_peer
48 {
49   noise_remote_t remote;
50   cookie_maker_t cookie_maker;
51
52   /* Peer addresses */
53   wg_peer_endpoint_t dst;
54   wg_peer_endpoint_t src;
55   u32 table_id;
56   adj_index_t adj_index;
57
58   /* rewrite built from address information */
59   u8 *rewrite;
60
61   /* Vector of allowed-ips */
62   wg_peer_allowed_ip_t *allowed_ips;
63
64   /* The WG interface this peer is attached to */
65   u32 wg_sw_if_index;
66
67   /* Timers */
68   tw_timer_wheel_16t_2w_512sl_t timer_wheel;
69   u32 timers[WG_N_TIMERS];
70   u32 timer_handshake_attempts;
71   u16 persistent_keepalive_interval;
72   f64 last_sent_handshake;
73   bool timer_need_another_keepalive;
74
75   bool is_dead;
76 } wg_peer_t;
77
78 typedef struct wg_peer_table_bind_ctx_t_
79 {
80   ip_address_family_t af;
81   u32 new_fib_index;
82   u32 old_fib_index;
83 } wg_peer_table_bind_ctx_t;
84
85 int wg_peer_add (u32 tun_sw_if_index,
86                  const u8 public_key_64[NOISE_PUBLIC_KEY_LEN],
87                  u32 table_id,
88                  const ip46_address_t * endpoint,
89                  const fib_prefix_t * allowed_ips,
90                  u16 port, u16 persistent_keepalive, index_t * peer_index);
91 int wg_peer_remove (u32 peer_index);
92
93 typedef walk_rc_t (*wg_peer_walk_cb_t) (index_t peeri, void *arg);
94 void wg_peer_walk (wg_peer_walk_cb_t fn, void *data);
95
96 u8 *format_wg_peer (u8 * s, va_list * va);
97 wg_peer_t *wg_peer_get (index_t peeri);
98
99 walk_rc_t wg_peer_if_admin_state_change (wg_if_t * wgi, index_t peeri,
100                                          void *data);
101 walk_rc_t wg_peer_if_table_change (wg_if_t * wgi, index_t peeri, void *data);
102
103 /*
104  * Expoed for the data-plane
105  */
106 extern index_t *wg_peer_by_adj_index;
107
108 static inline wg_peer_t *
109 wg_peer_get_by_adj_index (index_t ai)
110 {
111   return wg_peer_get (wg_peer_by_adj_index[ai]);
112 }
113
114 #endif // __included_wg_peer_h__
115
116 /*
117  * fd.io coding-style-patch-verification: ON
118  *
119  * Local Variables:
120  * eval: (c-set-style "gnu")
121  * End:
122  */