child-rekey: Uninstall old outbound SA earlier on initiator/winner
[strongswan.git] / src / libcharon / network / receiver.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20
21 #include "receiver.h"
22
23 #include <daemon.h>
24 #include <network/socket.h>
25 #include <network/packet.h>
26 #include <processing/jobs/job.h>
27 #include <processing/jobs/process_message_job.h>
28 #include <processing/jobs/callback_job.h>
29 #include <crypto/hashers/hasher.h>
30
31 /** lifetime of a cookie, in seconds */
32 #define COOKIE_LIFETIME 10
33 /** time we wait before disabling cookies */
34 #define COOKIE_CALMDOWN_DELAY 10
35 /** how many times to reuse the secret */
36 #define COOKIE_REUSE 10000
37 /** default value for private_receiver_t.cookie_threshold */
38 #define COOKIE_THRESHOLD_DEFAULT 10
39 /** default value for private_receiver_t.block_threshold */
40 #define BLOCK_THRESHOLD_DEFAULT 5
41 /** length of the secret to use for cookie calculation */
42 #define SECRET_LENGTH 16
43 /** Length of a notify payload header */
44 #define NOTIFY_PAYLOAD_HEADER_LENGTH 8
45
46 typedef struct private_receiver_t private_receiver_t;
47
48 /**
49 * Private data of a receiver_t object.
50 */
51 struct private_receiver_t {
52 /**
53 * Public part of a receiver_t object.
54 */
55 receiver_t public;
56
57 /**
58 * current secret to use for cookie calculation
59 */
60 char secret[SECRET_LENGTH];
61
62 /**
63 * previous secret used to verify older cookies
64 */
65 char secret_old[SECRET_LENGTH];
66
67 /**
68 * how many times we have used "secret" so far
69 */
70 u_int32_t secret_used;
71
72 /**
73 * time we did the cookie switch
74 */
75 u_int32_t secret_switch;
76
77 /**
78 * time offset to use, hides our system time
79 */
80 u_int32_t secret_offset;
81
82 /**
83 * the RNG to use for secret generation
84 */
85 rng_t *rng;
86
87 /**
88 * hasher to use for cookie calculation
89 */
90 hasher_t *hasher;
91
92 /**
93 * require cookies after this many half open IKE_SAs
94 */
95 u_int32_t cookie_threshold;
96
97 /**
98 * timestamp of last cookie requested
99 */
100 time_t last_cookie;
101
102 /**
103 * how many half open IKE_SAs per peer before blocking
104 */
105 u_int32_t block_threshold;
106
107 /**
108 * Drop IKE_SA_INIT requests if processor job load exceeds this limit
109 */
110 u_int init_limit_job_load;
111
112 /**
113 * Drop IKE_SA_INIT requests if half open IKE_SA count exceeds this limit
114 */
115 u_int init_limit_half_open;
116
117 /**
118 * Delay for receiving incoming packets, to simulate larger RTT
119 */
120 int receive_delay;
121
122 /**
123 * Specific message type to delay, 0 for any
124 */
125 int receive_delay_type;
126
127 /**
128 * Delay request messages?
129 */
130 bool receive_delay_request;
131
132 /**
133 * Delay response messages?
134 */
135 bool receive_delay_response;
136 };
137
138 /**
139 * send a notify back to the sender
140 */
141 static void send_notify(message_t *request, int major, exchange_type_t exchange,
142 notify_type_t type, chunk_t data)
143 {
144 ike_sa_id_t *ike_sa_id;
145 message_t *response;
146 host_t *src, *dst;
147 packet_t *packet;
148
149 response = message_create(major, 0);
150 response->set_exchange_type(response, exchange);
151 response->add_notify(response, FALSE, type, data);
152 dst = request->get_source(request);
153 src = request->get_destination(request);
154 response->set_source(response, src->clone(src));
155 response->set_destination(response, dst->clone(dst));
156 if (major == IKEV2_MAJOR_VERSION)
157 {
158 response->set_request(response, FALSE);
159 }
160 response->set_message_id(response, 0);
161 ike_sa_id = request->get_ike_sa_id(request);
162 ike_sa_id->switch_initiator(ike_sa_id);
163 response->set_ike_sa_id(response, ike_sa_id);
164 if (response->generate(response, NULL, &packet) == SUCCESS)
165 {
166 charon->sender->send(charon->sender, packet);
167 }
168 response->destroy(response);
169 }
170
171 /**
172 * build a cookie
173 */
174 static chunk_t cookie_build(private_receiver_t *this, message_t *message,
175 u_int32_t t, chunk_t secret)
176 {
177 u_int64_t spi = message->get_initiator_spi(message);
178 host_t *ip = message->get_source(message);
179 chunk_t input, hash;
180
181 /* COOKIE = t | sha1( IPi | SPIi | t | secret ) */
182 input = chunk_cata("cccc", ip->get_address(ip), chunk_from_thing(spi),
183 chunk_from_thing(t), secret);
184 hash = chunk_alloca(this->hasher->get_hash_size(this->hasher));
185 this->hasher->get_hash(this->hasher, input, hash.ptr);
186 return chunk_cat("cc", chunk_from_thing(t), hash);
187 }
188
189 /**
190 * verify a received cookie
191 */
192 static bool cookie_verify(private_receiver_t *this, message_t *message,
193 chunk_t cookie)
194 {
195 u_int32_t t, now;
196 chunk_t reference;
197 chunk_t secret;
198
199 now = time_monotonic(NULL);
200 t = *(u_int32_t*)cookie.ptr;
201
202 if (cookie.len != sizeof(u_int32_t) +
203 this->hasher->get_hash_size(this->hasher) ||
204 t < now - this->secret_offset - COOKIE_LIFETIME)
205 {
206 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
207 return FALSE;
208 }
209
210 /* check if cookie is derived from old_secret */
211 if (t + this->secret_offset > this->secret_switch)
212 {
213 secret = chunk_from_thing(this->secret);
214 }
215 else
216 {
217 secret = chunk_from_thing(this->secret_old);
218 }
219
220 /* compare own calculation against received */
221 reference = cookie_build(this, message, t, secret);
222 if (chunk_equals(reference, cookie))
223 {
224 chunk_free(&reference);
225 return TRUE;
226 }
227 chunk_free(&reference);
228 return FALSE;
229 }
230
231 /**
232 * Check if a valid cookie found
233 */
234 static bool check_cookie(private_receiver_t *this, message_t *message)
235 {
236 packet_t *packet;
237 chunk_t data;
238
239 /* check for a cookie. We don't use our parser here and do it
240 * quick and dirty for performance reasons.
241 * we assume the cookie is the first payload (which is a MUST), and
242 * the cookie's SPI length is zero. */
243 packet = message->get_packet(message);
244 data = packet->get_data(packet);
245 if (data.len <
246 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH +
247 sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher) ||
248 *(data.ptr + 16) != NOTIFY ||
249 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
250 {
251 /* no cookie found */
252 packet->destroy(packet);
253 return FALSE;
254 }
255 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
256 data.len = sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher);
257 if (!cookie_verify(this, message, data))
258 {
259 DBG2(DBG_NET, "found cookie, but content invalid");
260 packet->destroy(packet);
261 return FALSE;
262 }
263 return TRUE;
264 }
265
266 /**
267 * Check if we currently require cookies
268 */
269 static bool cookie_required(private_receiver_t *this,
270 u_int half_open, u_int32_t now)
271 {
272 if (this->cookie_threshold && half_open >= this->cookie_threshold)
273 {
274 this->last_cookie = now;
275 return TRUE;
276 }
277 if (now < this->last_cookie + COOKIE_CALMDOWN_DELAY)
278 {
279 /* We don't disable cookies unless we haven't seen IKE_SA_INITs
280 * for COOKIE_CALMDOWN_DELAY seconds. This avoids jittering between
281 * cookie on / cookie off states, which is problematic. Consider the
282 * following: A legitimiate initiator sends a IKE_SA_INIT while we
283 * are under a DoS attack. If we toggle our cookie behavior,
284 * multiple retransmits of this IKE_SA_INIT might get answered with
285 * and without cookies. The initiator goes on and retries with
286 * a cookie, but it can't know if the completing IKE_SA_INIT response
287 * is to its IKE_SA_INIT request with or without cookies. This is
288 * problematic, as the cookie is part of AUTH payload data.
289 */
290 this->last_cookie = now;
291 return TRUE;
292 }
293 return FALSE;
294 }
295
296 /**
297 * Check if we should drop IKE_SA_INIT because of cookie/overload checking
298 */
299 static bool drop_ike_sa_init(private_receiver_t *this, message_t *message)
300 {
301 u_int half_open;
302 u_int32_t now;
303
304 now = time_monotonic(NULL);
305 half_open = charon->ike_sa_manager->get_half_open_count(
306 charon->ike_sa_manager, NULL);
307
308 /* check for cookies in IKEv2 */
309 if (message->get_major_version(message) == IKEV2_MAJOR_VERSION &&
310 cookie_required(this, half_open, now) && !check_cookie(this, message))
311 {
312 chunk_t cookie;
313
314 cookie = cookie_build(this, message, now - this->secret_offset,
315 chunk_from_thing(this->secret));
316 DBG2(DBG_NET, "received packet from: %#H to %#H",
317 message->get_source(message),
318 message->get_destination(message));
319 DBG2(DBG_NET, "sending COOKIE notify to %H",
320 message->get_source(message));
321 send_notify(message, IKEV2_MAJOR_VERSION, IKE_SA_INIT, COOKIE, cookie);
322 chunk_free(&cookie);
323 if (++this->secret_used > COOKIE_REUSE)
324 {
325 /* create new cookie */
326 DBG1(DBG_NET, "generating new cookie secret after %d uses",
327 this->secret_used);
328 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
329 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
330 this->secret_switch = now;
331 this->secret_used = 0;
332 }
333 return TRUE;
334 }
335
336 /* check if peer has too many IKE_SAs half open */
337 if (this->block_threshold &&
338 charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
339 message->get_source(message)) >= this->block_threshold)
340 {
341 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
342 "peer too aggressive", message->get_source(message));
343 return TRUE;
344 }
345
346 /* check if global half open IKE_SA limit reached */
347 if (this->init_limit_half_open &&
348 half_open >= this->init_limit_half_open)
349 {
350 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, half open IKE_SA "
351 "count of %d exceeds limit of %d", message->get_source(message),
352 half_open, this->init_limit_half_open);
353 return TRUE;
354 }
355
356 /* check if job load acceptable */
357 if (this->init_limit_job_load)
358 {
359 u_int jobs = 0, i;
360
361 for (i = 0; i < JOB_PRIO_MAX; i++)
362 {
363 jobs += lib->processor->get_job_load(lib->processor, i);
364 }
365 if (jobs > this->init_limit_job_load)
366 {
367 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, job load of %d "
368 "exceeds limit of %d", message->get_source(message),
369 jobs, this->init_limit_job_load);
370 return TRUE;
371 }
372 }
373 return FALSE;
374 }
375
376 /**
377 * Job callback to receive packets
378 */
379 static job_requeue_t receive_packets(private_receiver_t *this)
380 {
381 ike_sa_id_t *id;
382 packet_t *packet;
383 message_t *message;
384 status_t status;
385 bool supported = TRUE;
386
387 /* read in a packet */
388 status = charon->socket->receive(charon->socket, &packet);
389 if (status == NOT_SUPPORTED)
390 {
391 return JOB_REQUEUE_NONE;
392 }
393 else if (status != SUCCESS)
394 {
395 DBG2(DBG_NET, "receiving from socket failed!");
396 return JOB_REQUEUE_FAIR;
397 }
398
399 /* parse message header */
400 message = message_create_from_packet(packet);
401 if (message->parse_header(message) != SUCCESS)
402 {
403 DBG1(DBG_NET, "received invalid IKE header from %H - ignored",
404 packet->get_source(packet));
405 message->destroy(message);
406 return JOB_REQUEUE_DIRECT;
407 }
408
409 /* check IKE major version */
410 switch (message->get_major_version(message))
411 {
412 case IKEV2_MAJOR_VERSION:
413 #ifndef USE_IKEV2
414 if (message->get_exchange_type(message) == IKE_SA_INIT &&
415 message->get_request(message))
416 {
417 send_notify(message, IKEV1_MAJOR_VERSION, INFORMATIONAL_V1,
418 INVALID_MAJOR_VERSION, chunk_empty);
419 supported = FALSE;
420 }
421 #endif /* USE_IKEV2 */
422 break;
423 case IKEV1_MAJOR_VERSION:
424 #ifndef USE_IKEV1
425 if (message->get_exchange_type(message) == ID_PROT ||
426 message->get_exchange_type(message) == AGGRESSIVE)
427 {
428 send_notify(message, IKEV2_MAJOR_VERSION, INFORMATIONAL,
429 INVALID_MAJOR_VERSION, chunk_empty);
430 supported = FALSE;
431 }
432 #endif /* USE_IKEV1 */
433 break;
434 default:
435 #ifdef USE_IKEV2
436 send_notify(message, IKEV2_MAJOR_VERSION, INFORMATIONAL,
437 INVALID_MAJOR_VERSION, chunk_empty);
438 #endif /* USE_IKEV2 */
439 #ifdef USE_IKEV1
440 send_notify(message, IKEV1_MAJOR_VERSION, INFORMATIONAL_V1,
441 INVALID_MAJOR_VERSION, chunk_empty);
442 #endif /* USE_IKEV1 */
443 supported = FALSE;
444 break;
445 }
446 if (!supported)
447 {
448 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, sending "
449 "INVALID_MAJOR_VERSION", message->get_major_version(message),
450 message->get_minor_version(message), packet->get_source(packet));
451 message->destroy(message);
452 return JOB_REQUEUE_DIRECT;
453 }
454 if (message->get_request(message) &&
455 message->get_exchange_type(message) == IKE_SA_INIT)
456 {
457 if (drop_ike_sa_init(this, message))
458 {
459 message->destroy(message);
460 return JOB_REQUEUE_DIRECT;
461 }
462 }
463 if (message->get_exchange_type(message) == ID_PROT ||
464 message->get_exchange_type(message) == AGGRESSIVE)
465 {
466 id = message->get_ike_sa_id(message);
467 if (id->get_responder_spi(id) == 0 &&
468 drop_ike_sa_init(this, message))
469 {
470 message->destroy(message);
471 return JOB_REQUEUE_DIRECT;
472 }
473 }
474
475 if (this->receive_delay)
476 {
477 if (this->receive_delay_type == 0 ||
478 this->receive_delay_type == message->get_exchange_type(message))
479 {
480 if ((message->get_request(message) && this->receive_delay_request) ||
481 (!message->get_request(message) && this->receive_delay_response))
482 {
483 DBG1(DBG_NET, "using receive delay: %dms",
484 this->receive_delay);
485 lib->scheduler->schedule_job_ms(lib->scheduler,
486 (job_t*)process_message_job_create(message),
487 this->receive_delay);
488 return JOB_REQUEUE_DIRECT;
489 }
490 }
491 }
492 lib->processor->queue_job(lib->processor,
493 (job_t*)process_message_job_create(message));
494 return JOB_REQUEUE_DIRECT;
495 }
496
497 METHOD(receiver_t, destroy, void,
498 private_receiver_t *this)
499 {
500 this->rng->destroy(this->rng);
501 this->hasher->destroy(this->hasher);
502 free(this);
503 }
504
505 /*
506 * Described in header.
507 */
508 receiver_t *receiver_create()
509 {
510 private_receiver_t *this;
511 u_int32_t now = time_monotonic(NULL);
512
513 INIT(this,
514 .public = {
515 .destroy = _destroy,
516 },
517 .secret_switch = now,
518 .secret_offset = random() % now,
519 );
520
521 if (lib->settings->get_bool(lib->settings,
522 "%s.dos_protection", TRUE, charon->name))
523 {
524 this->cookie_threshold = lib->settings->get_int(lib->settings,
525 "%s.cookie_threshold", COOKIE_THRESHOLD_DEFAULT, charon->name);
526 this->block_threshold = lib->settings->get_int(lib->settings,
527 "%s.block_threshold", BLOCK_THRESHOLD_DEFAULT, charon->name);
528 }
529 this->init_limit_job_load = lib->settings->get_int(lib->settings,
530 "%s.init_limit_job_load", 0, charon->name);
531 this->init_limit_half_open = lib->settings->get_int(lib->settings,
532 "%s.init_limit_half_open", 0, charon->name);
533 this->receive_delay = lib->settings->get_int(lib->settings,
534 "%s.receive_delay", 0, charon->name);
535 this->receive_delay_type = lib->settings->get_int(lib->settings,
536 "%s.receive_delay_type", 0, charon->name),
537 this->receive_delay_request = lib->settings->get_bool(lib->settings,
538 "%s.receive_delay_request", TRUE, charon->name),
539 this->receive_delay_response = lib->settings->get_bool(lib->settings,
540 "%s.receive_delay_response", TRUE, charon->name),
541
542 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
543 if (this->hasher == NULL)
544 {
545 DBG1(DBG_NET, "creating cookie hasher failed, no hashers supported");
546 free(this);
547 return NULL;
548 }
549 this->rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
550 if (this->rng == NULL)
551 {
552 DBG1(DBG_NET, "creating cookie RNG failed, no RNG supported");
553 this->hasher->destroy(this->hasher);
554 free(this);
555 return NULL;
556 }
557 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
558 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
559
560 lib->processor->queue_job(lib->processor,
561 (job_t*)callback_job_create_with_prio((callback_job_cb_t)receive_packets,
562 this, NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
563
564 return &this->public;
565 }
566