wireguard: add events for peer
[vpp.git] / src / plugins / wireguard / wireguard_peer.h
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __included_wg_peer_h__
18 #define __included_wg_peer_h__
19
20 #include <vlibapi/api_helper_macros.h>
21
22 #include <vnet/ip/ip.h>
23
24 #include <wireguard/wireguard_cookie.h>
25 #include <wireguard/wireguard_timer.h>
26 #include <wireguard/wireguard_key.h>
27 #include <wireguard/wireguard_messages.h>
28 #include <wireguard/wireguard_if.h>
29
30 typedef struct ip4_udp_header_t_
31 {
32   ip4_header_t ip4;
33   udp_header_t udp;
34 } __clib_packed ip4_udp_header_t;
35
36 typedef struct ip6_udp_header_t_
37 {
38   ip6_header_t ip6;
39   udp_header_t udp;
40 } __clib_packed ip6_udp_header_t;
41
42 u8 *format_ip4_udp_header (u8 * s, va_list * va);
43 u8 *format_ip6_udp_header (u8 *s, va_list *va);
44
45 typedef struct wg_peer_endpoint_t_
46 {
47   ip46_address_t addr;
48   u16 port;
49 } wg_peer_endpoint_t;
50
51 typedef enum
52 {
53   WG_PEER_STATUS_DEAD = 0x1,
54   WG_PEER_ESTABLISHED = 0x2,
55 } wg_peer_flags;
56
57 typedef struct wg_peer
58 {
59   noise_remote_t remote;
60   cookie_maker_t cookie_maker;
61
62   u32 input_thread_index;
63   u32 output_thread_index;
64
65   /* Peer addresses */
66   wg_peer_endpoint_t dst;
67   wg_peer_endpoint_t src;
68   u32 table_id;
69   adj_index_t *adj_indices;
70
71   /* rewrite built from address information */
72   u8 *rewrite;
73
74   /* Vector of allowed-ips */
75   fib_prefix_t *allowed_ips;
76
77   /* The WG interface this peer is attached to */
78   u32 wg_sw_if_index;
79
80   /* API client registered for events */
81   vpe_client_registration_t *api_clients;
82   uword *api_client_by_client_index;
83   wg_peer_flags flags;
84
85   /* Timers */
86   tw_timer_wheel_16t_2w_512sl_t *timer_wheel;
87   u32 timers[WG_N_TIMERS];
88   u8 timers_dispatched[WG_N_TIMERS];
89   u32 timer_handshake_attempts;
90   u16 persistent_keepalive_interval;
91
92   /* Timestamps */
93   f64 last_sent_handshake;
94   f64 last_sent_packet;
95   f64 last_received_packet;
96   f64 session_derived;
97   f64 rehandshake_started;
98
99   /* Variable intervals */
100   u32 new_handshake_interval_tick;
101   u32 rehandshake_interval_tick;
102
103   bool timer_need_another_keepalive;
104 } wg_peer_t;
105
106 typedef struct wg_peer_table_bind_ctx_t_
107 {
108   ip_address_family_t af;
109   u32 new_fib_index;
110   u32 old_fib_index;
111 } wg_peer_table_bind_ctx_t;
112
113 int wg_peer_add (u32 tun_sw_if_index,
114                  const u8 public_key_64[NOISE_PUBLIC_KEY_LEN],
115                  u32 table_id,
116                  const ip46_address_t * endpoint,
117                  const fib_prefix_t * allowed_ips,
118                  u16 port, u16 persistent_keepalive, index_t * peer_index);
119 int wg_peer_remove (u32 peer_index);
120
121 typedef walk_rc_t (*wg_peer_walk_cb_t) (index_t peeri, void *arg);
122 index_t wg_peer_walk (wg_peer_walk_cb_t fn, void *data);
123
124 u8 *format_wg_peer (u8 * s, va_list * va);
125
126 walk_rc_t wg_peer_if_admin_state_change (index_t peeri, void *data);
127 walk_rc_t wg_peer_if_delete (index_t peeri, void *data);
128 walk_rc_t wg_peer_if_adj_change (index_t peeri, void *data);
129 adj_walk_rc_t wg_peer_adj_walk (adj_index_t ai, void *data);
130
131 void wg_api_peer_event (index_t peeri, wg_peer_flags flags);
132 void wg_peer_update_flags (index_t peeri, wg_peer_flags flag, bool add_del);
133
134 static inline bool
135 wg_peer_is_dead (wg_peer_t *peer)
136 {
137   return peer && peer->flags & WG_PEER_STATUS_DEAD;
138 }
139
140 /*
141  * Expoed for the data-plane
142  */
143 extern index_t *wg_peer_by_adj_index;
144 extern wg_peer_t *wg_peer_pool;
145
146 static inline wg_peer_t *
147 wg_peer_get (index_t peeri)
148 {
149   return (pool_elt_at_index (wg_peer_pool, peeri));
150 }
151
152 static inline index_t
153 wg_peer_get_by_adj_index (index_t ai)
154 {
155   return (wg_peer_by_adj_index[ai]);
156 }
157
158 /*
159  * Makes choice for thread_id should be assigned.
160 */
161 static inline u32
162 wg_peer_assign_thread (u32 thread_id)
163 {
164   return ((thread_id) ? thread_id
165           : (vlib_num_workers ()?
166              ((unix_time_now_nsec () % vlib_num_workers ()) +
167               1) : thread_id));
168 }
169
170 static_always_inline bool
171 fib_prefix_is_cover_addr_46 (const fib_prefix_t *p1, const ip46_address_t *ip)
172 {
173   switch (p1->fp_proto)
174     {
175     case FIB_PROTOCOL_IP4:
176       return (ip4_destination_matches_route (&ip4_main, &p1->fp_addr.ip4,
177                                              &ip->ip4, p1->fp_len) != 0);
178     case FIB_PROTOCOL_IP6:
179       return (ip6_destination_matches_route (&ip6_main, &p1->fp_addr.ip6,
180                                              &ip->ip6, p1->fp_len) != 0);
181     case FIB_PROTOCOL_MPLS:
182       break;
183     }
184   return (false);
185 }
186
187 #endif // __included_wg_peer_h__
188
189 /*
190  * fd.io coding-style-patch-verification: ON
191  *
192  * Local Variables:
193  * eval: (c-set-style "gnu")
194  * End:
195  */