aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/wireguard/wireguard_timer.c
blob: e4d4030bb1852a96e2cca5f0edb50e1c36d84dfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 2020 Doc.ai and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <wireguard/wireguard.h>
#include <wireguard/wireguard_send.h>
#include <wireguard/wireguard_timer.h>

static u32
get_random_u32_max (u32 max)
{
  vlib_main_t *vm = vlib_get_main ();
  u32 seed = (u32) (vlib_time_now (vm) * 1e6);
  return random_u32 (&seed) % max;
}

static void
stop_timer (wg_peer_t * peer, u32 timer_id)
{
  if (peer->timers[timer_id] != ~0)
    {
      tw_timer_stop_16t_2w_512sl (&peer->timer_wheel, peer->timers[timer_id]);
      peer->timers[timer_id] = ~0;
    }
}

static void
start_or_update_timer (wg_peer_t * peer, u32 timer_id, u32 interval)
{
  if (peer->timers[timer_id] == ~0)
    {
      wg_main_t *wmp = &wg_main;
      peer->timers[timer_id] =
	tw_timer_start_16t_2w_512sl (&peer->timer_wheel, peer - wmp->peers,
				     timer_id, interval);
    }
  else
    {
      tw_timer_update_16t_2w_512sl (&peer->timer_wheel,
				    peer->timers[timer_id], interval);
    }
}

static void
wg_expired_retransmit_handshake (vlib_main_t * vm, wg_peer_t * peer)
{

  if (peer->timer_handshake_attempts > MAX_TIMER_HANDSHAKES)
    {
      stop_timer (peer, WG_TIMER_SEND_KEEPALIVE);

      /* We set a timer for destroying any residue that might be left
       * of a partial exchange.
       */

      if (peer->timers[WG_TIMER_KEY_ZEROING] == ~0)
	{
	  wg_main_t *wmp = &wg_main;

	  peer->timers[WG_TIMER_KEY_ZEROING] =
	    tw_timer_start_16t_2w_512sl (&peer->timer_wheel,
					 peer - wmp->peers,
					 WG_TIMER_KEY_ZEROING,
					 REJECT_AFTER_TIME * 3 * WHZ);
	}
    }
  else
    {
      ++peer->timer_handshake_attempts;
      wg_send_handshake (vm, peer, true);
    }
}

static void
wg_expired_send_keepalive (vlib_main_t * vm, wg_peer_t * peer)
{
  wg_send_keepalive (vm, peer);

  if (peer->timer_need_another_keepalive)
    {
      peer->timer_need_another_keepalive = false;
      start_or_update_timer (peer, WG_TIMER_SEND_KEEPALIVE,
			     KEEPALIVE_TIMEOUT * WHZ);
    }
}

static void
wg_expired_send_persistent_keepalive (vlib_main_t * vm, wg_peer_t * peer)
{
  if (peer->persistent_keepalive_interval)
    {
      wg_send_keepalive (vm, peer);
    }
}

static void
wg_expired_new_handshake (vlib_main_t * vm, wg_peer_t * peer)
{
  wg_send_handshake (vm, peer, false);
}

static void
wg_expired_zero_key_material (vlib_main_t * vm, wg_peer_t * peer)
{
  if (!peer->is_dead)
    {
      noise_remote_clear (vm, &peer->remote);
    }
}


void
wg_timers_any_authenticated_packet_traversal (wg_peer_t * peer)
{
  if (peer->persistent_keepalive_interval)
    {
      start_or_update_timer (peer, WG_TIMER_PERSISTENT_KEEPALIVE,
			     peer->persistent_keepalive_interval * WHZ);
    }
}

void
wg_timers_any_authenticated_packet_sent (wg_peer_t * peer)
{
  stop_timer (peer, WG_TIMER_SEND_KEEPALIVE);
}

void
wg_timers_handshake_initiated (wg_peer_t * peer)
{
  u32 interval =
    REKEY_TIMEOUT * WHZ + get_random_u32_max (REKEY_TIMEOUT_JITTER);
  start_or_update_timer (peer, WG_TIMER_RETRANSMIT_HANDSHAKE, interval);
}

void
wg_timers_session_derived (wg_peer_t * peer)
{
  start_or_update_timer (peer, WG_TIMER_KEY_ZEROING,
			 REJECT_AFTER_TIME * 3 * WHZ);
}

/* Should be called after an authenticated data packet is sent. */
void
wg_timers_data_sent (wg_peer_t * peer)
{
  u32 interval = (KEEPALIVE_TIMEOUT + REKEY_TIMEOUT) * WHZ +
    get_random_u32_max (REKEY_TIMEOUT_JITTER);

  if (peer->timers[WG_TIMER_NEW_HANDSHAKE] == ~0)
    {
      wg_main_t *wmp = &wg_main;
      peer->timers[WG_TIMER_NEW_HANDSHAKE] =
	tw_timer_start_16t_2w_512sl (&peer->timer_wheel, peer - wmp->peers,
				     WG_TIMER_NEW_HANDSHAKE, interval);
    }
}

/* Should be called after an authenticated data packet is received. */
void
wg_timers_data_received (wg_peer_t * peer)
{
  if (peer->timers[WG_TIMER_SEND_KEEPALIVE] == ~0)
    {
      wg_main_t *wmp = &wg_main;
      peer->timers[WG_TIMER_SEND_KEEPALIVE] =
	tw_timer_start_16t_2w_512sl (&peer->timer_wheel, peer - wmp->peers,
				     WG_TIMER_SEND_KEEPALIVE,
				     KEEPALIVE_TIMEOUT * WHZ);
    }
  else
    {
      peer->timer_need_another_keepalive = true;
    }
}

/* Should be called after a handshake response message is received and processed
 * or when getting key confirmation via the first data message.
 */
void
wg_timers_handshake_complete (wg_peer_t * peer)
{
  stop_timer (peer, WG_TIMER_RETRANSMIT_HANDSHAKE);

  peer->timer_handshake_attempts = 0;
}

void
wg_timers_any_authenticated_packet_received (wg_peer_t * peer)
{
  stop_timer (peer, WG_TIMER_NEW_HANDSHAKE);
}

static vlib_node_registration_t wg_timer_mngr_node;

static void
expired_timer_callback (u32 * expired_timers)
{
  int i;
  u32 timer_id;
  u32 pool_index;

  wg_main_t *wmp = &wg_main;
  vlib_main_t *vm = wmp->vlib_main;

  wg_peer_t *peer;

  /* Need to invalidate all of them because one can restart other */
  for (i = 0; i < vec_len (expired_timers); i++)
    {
      pool_index = expired_timers[i] & 0x0FFFFFFF;
      timer_id = expired_timers[i] >> 28;

      peer = pool_elt_at_index (wmp->peers, pool_index);
      peer->timers[timer_id] = ~0;
    }

  for (i = 0; i < vec_len (expired_timers); i++)
    {
      pool_index = expired_timers[i] & 0x0FFFFFFF;
      timer_id = expired_timers[i] >> 28;

      peer = pool_elt_at_index (wmp->peers, pool_index);
      switch (timer_id)
	{
	case WG_TIMER_RETRANSMIT_HANDSHAKE:
	  wg_expired_retransmit_handshake (vm, peer);
	  break;
	case WG_TIMER_PERSISTENT_KEEPALIVE:
	  wg_expired_send_persistent_keepalive (vm, peer);
	  break;
	case WG_TIMER_SEND_KEEPALIVE:
	  wg_expired_send_keepalive (vm, peer);
	  break;
	case WG_TIMER_NEW_HANDSHAKE:
	  wg_expired_new_handshake (vm, peer);
	  break;
	case WG_TIMER_KEY_ZEROING:
	  wg_expired_zero_key_material (vm, peer);
	  break;
	default:
	  break;
	}
    }
}

void
wg_timers_init (wg_peer_t * peer, f64 now)
{
  for (int i = 0; i < WG_N_TIMERS; i++)
    {
      peer->timers[i] = ~0;
    }
  tw_timer_wheel_16t_2w_512sl_t *tw = &peer->timer_wheel;
  tw_timer_wheel_init_16t_2w_512sl (tw,
				    expired_timer_callback,
				    WG_TICK /* timer period in s */ , ~0);
  tw->last_run_time = now;
  peer->adj_index = INDEX_INVALID;
}

static uword
wg_timer_mngr_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
		  vlib_frame_t * f)
{
  wg_main_t *wmp = &wg_main;
  wg_peer_t *peers;
  wg_peer_t *peer;

  while (1)
    {
      vlib_process_wait_for_event_or_clock (vm, WG_TICK);
      vlib_process_get_events (vm, NULL);

      peers = wmp->peers;
      /* *INDENT-OFF* */
      pool_foreach (peer, peers,
      ({
        tw_timer_expire_timers_16t_2w_512sl
        (&peer->timer_wheel, vlib_time_now (vm));
      }));
      /* *INDENT-ON* */
    }

  return 0;
}

void
wg_timers_stop (wg_peer_t * peer)
{
  stop_timer (peer, WG_TIMER_RETRANSMIT_HANDSHAKE);
  stop_timer (peer, WG_TIMER_PERSISTENT_KEEPALIVE);
  stop_timer (peer, WG_TIMER_SEND_KEEPALIVE);
  stop_timer (peer, WG_TIMER_NEW_HANDSHAKE);
  stop_timer (peer, WG_TIMER_KEY_ZEROING);
}

/* *INDENT-OFF* */
VLIB_REGISTER_NODE (wg_timer_mngr_node, static) = {
    .function = wg_timer_mngr_fn,
    .type = VLIB_NODE_TYPE_PROCESS,
    .name =
    "wg-timer-manager",
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */