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