e23feb7866fe5bc7d47f967ede986c534d172524
[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 <vnet/ip/ip.h>
21
22 #include <wireguard/wireguard_cookie.h>
23 #include <wireguard/wireguard_timer.h>
24 #include <wireguard/wireguard_key.h>
25 #include <wireguard/wireguard_messages.h>
26 #include <wireguard/wireguard_if.h>
27
28 typedef struct ip4_udp_header_t_
29 {
30   ip4_header_t ip4;
31   udp_header_t udp;
32 } __clib_packed ip4_udp_header_t;
33
34 u8 *format_ip4_udp_header (u8 * s, va_list * va);
35
36 typedef struct wg_peer_endpoint_t_
37 {
38   ip46_address_t addr;
39   u16 port;
40 } wg_peer_endpoint_t;
41
42 typedef struct wg_peer
43 {
44   noise_remote_t remote;
45   cookie_maker_t cookie_maker;
46
47   u32 input_thread_index;
48   u32 output_thread_index;
49
50   /* Peer addresses */
51   wg_peer_endpoint_t dst;
52   wg_peer_endpoint_t src;
53   u32 table_id;
54   adj_index_t *adj_indices;
55
56   /* rewrite built from address information */
57   u8 *rewrite;
58
59   /* Vector of allowed-ips */
60   fib_prefix_t *allowed_ips;
61
62   /* The WG interface this peer is attached to */
63   u32 wg_sw_if_index;
64
65   /* Timers */
66   tw_timer_wheel_16t_2w_512sl_t *timer_wheel;
67   u32 timers[WG_N_TIMERS];
68   u8 timers_dispatched[WG_N_TIMERS];
69   u32 timer_handshake_attempts;
70   u16 persistent_keepalive_interval;
71
72   /* Timestamps */
73   f64 last_sent_handshake;
74   f64 last_sent_packet;
75   f64 last_received_packet;
76   f64 session_derived;
77   f64 rehandshake_started;
78
79   /* Variable intervals */
80   u32 new_handshake_interval_tick;
81   u32 rehandshake_interval_tick;
82
83   bool timer_need_another_keepalive;
84
85   bool is_dead;
86 } wg_peer_t;
87
88 typedef struct wg_peer_table_bind_ctx_t_
89 {
90   ip_address_family_t af;
91   u32 new_fib_index;
92   u32 old_fib_index;
93 } wg_peer_table_bind_ctx_t;
94
95 int wg_peer_add (u32 tun_sw_if_index,
96                  const u8 public_key_64[NOISE_PUBLIC_KEY_LEN],
97                  u32 table_id,
98                  const ip46_address_t * endpoint,
99                  const fib_prefix_t * allowed_ips,
100                  u16 port, u16 persistent_keepalive, index_t * peer_index);
101 int wg_peer_remove (u32 peer_index);
102
103 typedef walk_rc_t (*wg_peer_walk_cb_t) (index_t peeri, void *arg);
104 index_t wg_peer_walk (wg_peer_walk_cb_t fn, void *data);
105
106 u8 *format_wg_peer (u8 * s, va_list * va);
107
108 walk_rc_t wg_peer_if_admin_state_change (index_t peeri, void *data);
109 walk_rc_t wg_peer_if_delete (index_t peeri, void *data);
110 walk_rc_t wg_peer_if_adj_change (index_t peeri, void *data);
111 adj_walk_rc_t wg_peer_adj_walk (adj_index_t ai, void *data);
112
113 /*
114  * Expoed for the data-plane
115  */
116 extern index_t *wg_peer_by_adj_index;
117 extern wg_peer_t *wg_peer_pool;
118
119 static inline wg_peer_t *
120 wg_peer_get (index_t peeri)
121 {
122   return (pool_elt_at_index (wg_peer_pool, peeri));
123 }
124
125 static inline index_t
126 wg_peer_get_by_adj_index (index_t ai)
127 {
128   return (wg_peer_by_adj_index[ai]);
129 }
130
131 /*
132  * Makes choice for thread_id should be assigned.
133 */
134 static inline u32
135 wg_peer_assign_thread (u32 thread_id)
136 {
137   return ((thread_id) ? thread_id
138           : (vlib_num_workers ()?
139              ((unix_time_now_nsec () % vlib_num_workers ()) +
140               1) : thread_id));
141 }
142
143 static_always_inline bool
144 fib_prefix_is_cover_addr_4 (const fib_prefix_t *p1, const ip4_address_t *ip4)
145 {
146   switch (p1->fp_proto)
147     {
148     case FIB_PROTOCOL_IP4:
149       return (ip4_destination_matches_route (&ip4_main, &p1->fp_addr.ip4, ip4,
150                                              p1->fp_len) != 0);
151     case FIB_PROTOCOL_IP6:
152       return (false);
153     case FIB_PROTOCOL_MPLS:
154       break;
155     }
156   return (false);
157 }
158
159 #endif // __included_wg_peer_h__
160
161 /*
162  * fd.io coding-style-patch-verification: ON
163  *
164  * Local Variables:
165  * eval: (c-set-style "gnu")
166  * End:
167  */