FD.io VPP  v21.01.1
Vector Packet Processing
wireguard_timer.c
Go to the documentation of this file.
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 #include <vlibmemory/api.h>
17 #include <wireguard/wireguard.h>
20 
21 static u32
23 {
25  u32 seed = (u32) (vlib_time_now (vm) * 1e6);
26  return random_u32 (&seed) % max;
27 }
28 
29 static void
31 {
32  if (peer->timers[timer_id] != ~0)
33  {
34  tw_timer_stop_16t_2w_512sl (peer->timer_wheel, peer->timers[timer_id]);
35  peer->timers[timer_id] = ~0;
36  }
37 }
38 
39 static void
40 start_timer (wg_peer_t * peer, u32 timer_id, u32 interval_ticks)
41 {
42  ASSERT (vlib_get_thread_index () == 0);
43 
44  if (peer->timers[timer_id] == ~0)
45  {
46  peer->timers[timer_id] =
47  tw_timer_start_16t_2w_512sl (peer->timer_wheel, peer - wg_peer_pool,
48  timer_id, interval_ticks);
49  }
50 }
51 
52 typedef struct
53 {
57 
59 
60 static void *
62 {
63  wg_timers_args *a = arg;
65 
66  start_timer (peer, a->timer_id, a->interval_ticks);
67  return 0;
68 }
69 
70 static void
71 start_timer_from_mt (u32 peer_idx, u32 timer_id, u32 interval_ticks)
72 {
73  wg_timers_args a = {
74  .peer_idx = peer_idx,
75  .timer_id = timer_id,
76  .interval_ticks = interval_ticks,
77  };
78 
80 }
81 
82 static inline u32
83 timer_ticks_left (vlib_main_t * vm, f64 init_time_sec, u32 interval_ticks)
84 {
85  static const int32_t rounding = (int32_t) (WHZ / 2);
86  int32_t ticks_remain;
87 
88  ticks_remain = (init_time_sec - vlib_time_now (vm)) * WHZ + interval_ticks;
89  return (ticks_remain > rounding) ? (u32) ticks_remain : 0;
90 }
91 
92 static void
94 {
95  if (peer->rehandshake_started == ~0)
96  return;
97 
98  u32 ticks = timer_ticks_left (vm, peer->rehandshake_started,
100  if (ticks)
101  {
102  start_timer (peer, WG_TIMER_RETRANSMIT_HANDSHAKE, ticks);
103  return;
104  }
105 
107  {
108  stop_timer (peer, WG_TIMER_SEND_KEEPALIVE);
109 
110  /* We set a timer for destroying any residue that might be left
111  * of a partial exchange.
112  */
113  start_timer (peer, WG_TIMER_KEY_ZEROING, REJECT_AFTER_TIME * 3 * WHZ);
114 
115  }
116  else
117  {
118  ++peer->timer_handshake_attempts;
119  wg_send_handshake (vm, peer, true);
120  }
121 }
122 
123 static void
125 {
126  if (peer->last_sent_packet < peer->last_received_packet)
127  {
128  u32 ticks = timer_ticks_left (vm, peer->last_received_packet,
130  if (ticks)
131  {
132  start_timer (peer, WG_TIMER_SEND_KEEPALIVE, ticks);
133  return;
134  }
135 
136  wg_send_keepalive (vm, peer);
138  {
139  peer->timer_need_another_keepalive = false;
140  start_timer (peer, WG_TIMER_SEND_KEEPALIVE,
141  KEEPALIVE_TIMEOUT * WHZ);
142  }
143  }
144 }
145 
146 static void
148 {
150  {
151  f64 latest_time = peer->last_sent_packet > peer->last_received_packet
152  ? peer->last_sent_packet : peer->last_received_packet;
153 
154  u32 ticks = timer_ticks_left (vm, latest_time,
156  WHZ);
157  if (ticks)
158  {
159  start_timer (peer, WG_TIMER_PERSISTENT_KEEPALIVE, ticks);
160  return;
161  }
162 
163  wg_send_keepalive (vm, peer);
164  }
165 }
166 
167 static void
169 {
170  u32 ticks = timer_ticks_left (vm, peer->last_sent_packet,
172  if (ticks)
173  {
174  start_timer (peer, WG_TIMER_NEW_HANDSHAKE, ticks);
175  return;
176  }
177 
178  wg_send_handshake (vm, peer, false);
179 }
180 
181 static void
183 {
184  u32 ticks =
186  if (ticks)
187  {
188  start_timer (peer, WG_TIMER_KEY_ZEROING, ticks);
189  return;
190  }
191 
192  if (!peer->is_dead)
193  {
194  noise_remote_clear (vm, &peer->remote);
195  }
196 }
197 
198 void
200 {
202  {
204  WG_TIMER_PERSISTENT_KEEPALIVE,
206  }
207 }
208 
209 void
211 {
213 }
214 
215 void
217 {
221 
222  start_timer_from_mt (peer - wg_peer_pool, WG_TIMER_RETRANSMIT_HANDSHAKE,
224 }
225 
226 void
228 {
230 
231  start_timer_from_mt (peer - wg_peer_pool, WG_TIMER_KEY_ZEROING,
232  REJECT_AFTER_TIME * 3 * WHZ);
233 }
234 
235 /* Should be called after an authenticated data packet is sent. */
236 void
238 {
242 
243  start_timer_from_mt (peer - wg_peer_pool, WG_TIMER_NEW_HANDSHAKE,
245 }
246 
247 /* Should be called after an authenticated data packet is received. */
248 void
250 {
251  if (peer->timers[WG_TIMER_SEND_KEEPALIVE] == ~0)
252  {
253  start_timer_from_mt (peer - wg_peer_pool, WG_TIMER_SEND_KEEPALIVE,
255  }
256  else
257  peer->timer_need_another_keepalive = true;
258 }
259 
260 /* Should be called after a handshake response message is received and processed
261  * or when getting key confirmation via the first data message.
262  */
263 void
265 {
266  peer->rehandshake_started = ~0;
267  peer->timer_handshake_attempts = 0;
268 }
269 
270 void
272 {
274 }
275 
277 
278 static void
279 expired_timer_callback (u32 * expired_timers)
280 {
281  int i;
282  u32 timer_id;
283  u32 pool_index;
284 
285  wg_main_t *wmp = &wg_main;
286  vlib_main_t *vm = wmp->vlib_main;
287 
288  wg_peer_t *peer;
289 
290  /* Need to invalidate all of them because one can restart other */
291  for (i = 0; i < vec_len (expired_timers); i++)
292  {
293  pool_index = expired_timers[i] & 0x0FFFFFFF;
294  timer_id = expired_timers[i] >> 28;
295 
296  peer = wg_peer_get (pool_index);
297  peer->timers[timer_id] = ~0;
298  }
299 
300  for (i = 0; i < vec_len (expired_timers); i++)
301  {
302  pool_index = expired_timers[i] & 0x0FFFFFFF;
303  timer_id = expired_timers[i] >> 28;
304 
305  peer = wg_peer_get (pool_index);
306  switch (timer_id)
307  {
308  case WG_TIMER_RETRANSMIT_HANDSHAKE:
310  break;
311  case WG_TIMER_PERSISTENT_KEEPALIVE:
313  break;
314  case WG_TIMER_SEND_KEEPALIVE:
315  wg_expired_send_keepalive (vm, peer);
316  break;
317  case WG_TIMER_NEW_HANDSHAKE:
318  wg_expired_new_handshake (vm, peer);
319  break;
320  case WG_TIMER_KEY_ZEROING:
321  wg_expired_zero_key_material (vm, peer);
322  break;
323  default:
324  break;
325  }
326  }
327 }
328 
329 void
331 {
332  wg_main_t *wmp = &wg_main;
333  tw_timer_wheel_16t_2w_512sl_t *tw = &wmp->timer_wheel;
334  tw_timer_wheel_init_16t_2w_512sl (tw,
336  WG_TICK /* timer period in s */ , ~0);
337 }
338 
339 static uword
341  vlib_frame_t * f)
342 {
343  wg_main_t *wmp = &wg_main;
344  uword event_type = 0;
345 
346  /* Park the process until the feature is configured */
347  while (1)
348  {
350  event_type = vlib_process_get_events (vm, 0);
351  if (event_type == WG_START_EVENT)
352  {
353  break;
354  }
355  else
356  {
357  clib_warning ("Unknown event type %d", event_type);
358  }
359  }
360  /*
361  * Reset the timer wheel time so it won't try to
362  * expire Avogadro's number of time slots.
363  */
364  wmp->timer_wheel.last_run_time = vlib_time_now (vm);
365 
366  while (1)
367  {
369  vlib_process_get_events (vm, NULL);
370 
371  tw_timer_expire_timers_16t_2w_512sl (&wmp->timer_wheel,
372  vlib_time_now (vm));
373  }
374 
375  return 0;
376 }
377 
378 void
380 {
381  ASSERT (vlib_get_thread_index () == 0);
382  if (peer->timer_wheel)
383  {
384  stop_timer (peer, WG_TIMER_RETRANSMIT_HANDSHAKE);
385  stop_timer (peer, WG_TIMER_PERSISTENT_KEEPALIVE);
386  stop_timer (peer, WG_TIMER_SEND_KEEPALIVE);
387  stop_timer (peer, WG_TIMER_NEW_HANDSHAKE);
388  stop_timer (peer, WG_TIMER_KEY_ZEROING);
389  }
390 }
391 
392 /* *INDENT-OFF* */
394  .function = wg_timer_mngr_fn,
395  .type = VLIB_NODE_TYPE_PROCESS,
396  .name =
397  "wg-timer-manager",
398 };
399 /* *INDENT-ON* */
400 
401 void
403 {
404  if (wmp->feature_init)
405  return;
407  WG_START_EVENT, 0);
408  wmp->feature_init = 1;
409 }
410 
411 
412 
413 /*
414  * fd.io coding-style-patch-verification: ON
415  *
416  * Local Variables:
417  * eval: (c-set-style "gnu")
418  * End:
419  */
#define REJECT_AFTER_TIME
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:751
static void wg_expired_zero_key_material(vlib_main_t *vm, wg_peer_t *peer)
u16 persistent_keepalive_interval
a
Definition: bitmap.h:544
f64 rehandshake_started
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:656
static void stop_timer(wg_peer_t *peer, u32 timer_id)
void wg_feature_init(wg_main_t *wmp)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
tw_timer_wheel_16t_2w_512sl_t * timer_wheel
static void wg_expired_send_keepalive(vlib_main_t *vm, wg_peer_t *peer)
static u32 timer_ticks_left(vlib_main_t *vm, f64 init_time_sec, u32 interval_ticks)
void wg_timers_any_authenticated_packet_traversal(wg_peer_t *peer)
vlib_main_t * vm
Definition: in2out_ed.c:1580
u32 timers[WG_N_TIMERS]
f64 session_derived
#define WG_START_EVENT
Definition: wireguard.h:51
unsigned char u8
Definition: types.h:56
void wg_timers_stop(wg_peer_t *peer)
double f64
Definition: types.h:142
static void wg_expired_send_persistent_keepalive(vlib_main_t *vm, wg_peer_t *peer)
f64 last_sent_packet
noise_remote_t remote
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:579
static void start_timer(wg_peer_t *peer, u32 timer_id, u32 interval_ticks)
void vl_api_rpc_call_main_thread(void *fp, u8 *data, u32 data_length)
Definition: vlib_api.c:619
void noise_remote_clear(vlib_main_t *vm, noise_remote_t *r)
unsigned int u32
Definition: types.h:88
tw_timer_wheel_16t_2w_512sl_t timer_wheel
Definition: wireguard.h:46
static uword wg_timer_mngr_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
void wg_timers_any_authenticated_packet_received(wg_peer_t *peer)
void wg_timers_data_sent(wg_peer_t *peer)
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1015
#define WHZ
WG tick frequency.
static vlib_node_registration_t wg_timer_mngr_node
(constructor) VLIB_REGISTER_NODE (wg_timer_mngr_node)
u8 feature_init
Definition: wireguard.h:44
vl_api_address_t peer
Definition: teib.api:28
void wg_timers_data_received(wg_peer_t *peer)
#define WG_TICK
WG tick period (s)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:170
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:219
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
static u32 get_random_u32_max(u32 max)
u32 new_handshake_interval_tick
void wg_timers_any_authenticated_packet_sent(wg_peer_t *peer)
#define clib_warning(format, args...)
Definition: error.h:59
wg_peer_t * wg_peer_pool
void wg_timer_wheel_init()
bool is_dead
#define ASSERT(truth)
wg_main_t wg_main
Definition: wireguard.c:26
bool timer_need_another_keepalive
static void wg_expired_retransmit_handshake(vlib_main_t *vm, wg_peer_t *peer)
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
u32 rehandshake_interval_tick
struct _vlib_node_registration vlib_node_registration_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static void wg_expired_new_handshake(vlib_main_t *vm, wg_peer_t *peer)
u64 uword
Definition: types.h:112
vlib_main_t * vlib_main
Definition: wireguard.h:34
void wg_timers_session_derived(wg_peer_t *peer)
f64 last_received_packet
static void start_timer_from_mt(u32 peer_idx, u32 timer_id, u32 interval_ticks)
static void * start_timer_thread_fn(void *arg)
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
bool wg_send_keepalive(vlib_main_t *vm, wg_peer_t *peer)
void wg_timers_handshake_initiated(wg_peer_t *peer)
static wg_peer_t * wg_peer_get(index_t peeri)
u32 timer_handshake_attempts
static void expired_timer_callback(u32 *expired_timers)
bool wg_send_handshake(vlib_main_t *vm, wg_peer_t *peer, bool is_retry)
void wg_timers_handshake_complete(wg_peer_t *peer)