Added charon.initiator_only option which causes charon to ignore IKE initiation reque...
[strongswan.git] / src / libcharon / network / receiver.c
1 /*
2 * Copyright (C) 2008-2012 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 <hydra.h>
24 #include <daemon.h>
25 #include <network/socket.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 #include <threading/mutex.h>
31 #include <networking/packet.h>
32
33 /** lifetime of a cookie, in seconds */
34 #define COOKIE_LIFETIME 10
35 /** time we wait before disabling cookies */
36 #define COOKIE_CALMDOWN_DELAY 10
37 /** how many times to reuse the secret */
38 #define COOKIE_REUSE 10000
39 /** default value for private_receiver_t.cookie_threshold */
40 #define COOKIE_THRESHOLD_DEFAULT 10
41 /** default value for private_receiver_t.block_threshold */
42 #define BLOCK_THRESHOLD_DEFAULT 5
43 /** length of the secret to use for cookie calculation */
44 #define SECRET_LENGTH 16
45 /** Length of a notify payload header */
46 #define NOTIFY_PAYLOAD_HEADER_LENGTH 8
47
48 typedef struct private_receiver_t private_receiver_t;
49
50 /**
51 * Private data of a receiver_t object.
52 */
53 struct private_receiver_t {
54 /**
55 * Public part of a receiver_t object.
56 */
57 receiver_t public;
58
59 /**
60 * Registered callback for ESP packets
61 */
62 struct {
63 receiver_esp_cb_t cb;
64 void *data;
65 } esp_cb;
66
67 /**
68 * Mutex for ESP callback
69 */
70 mutex_t *esp_cb_mutex;
71
72 /**
73 * current secret to use for cookie calculation
74 */
75 char secret[SECRET_LENGTH];
76
77 /**
78 * previous secret used to verify older cookies
79 */
80 char secret_old[SECRET_LENGTH];
81
82 /**
83 * how many times we have used "secret" so far
84 */
85 u_int32_t secret_used;
86
87 /**
88 * time we did the cookie switch
89 */
90 u_int32_t secret_switch;
91
92 /**
93 * time offset to use, hides our system time
94 */
95 u_int32_t secret_offset;
96
97 /**
98 * the RNG to use for secret generation
99 */
100 rng_t *rng;
101
102 /**
103 * hasher to use for cookie calculation
104 */
105 hasher_t *hasher;
106
107 /**
108 * require cookies after this many half open IKE_SAs
109 */
110 u_int32_t cookie_threshold;
111
112 /**
113 * timestamp of last cookie requested
114 */
115 time_t last_cookie;
116
117 /**
118 * how many half open IKE_SAs per peer before blocking
119 */
120 u_int32_t block_threshold;
121
122 /**
123 * Drop IKE_SA_INIT requests if processor job load exceeds this limit
124 */
125 u_int init_limit_job_load;
126
127 /**
128 * Drop IKE_SA_INIT requests if half open IKE_SA count exceeds this limit
129 */
130 u_int init_limit_half_open;
131
132 /**
133 * Delay for receiving incoming packets, to simulate larger RTT
134 */
135 int receive_delay;
136
137 /**
138 * Specific message type to delay, 0 for any
139 */
140 int receive_delay_type;
141
142 /**
143 * Delay request messages?
144 */
145 bool receive_delay_request;
146
147 /**
148 * Delay response messages?
149 */
150 bool receive_delay_response;
151
152 /**
153 * Endpoint is allowed to act as an initiator only
154 */
155 bool initiator_only;
156
157 };
158
159 /**
160 * send a notify back to the sender
161 */
162 static void send_notify(message_t *request, int major, exchange_type_t exchange,
163 notify_type_t type, chunk_t data)
164 {
165 ike_sa_id_t *ike_sa_id;
166 message_t *response;
167 host_t *src, *dst;
168 packet_t *packet;
169
170 response = message_create(major, 0);
171 response->set_exchange_type(response, exchange);
172 response->add_notify(response, FALSE, type, data);
173 dst = request->get_source(request);
174 src = request->get_destination(request);
175 response->set_source(response, src->clone(src));
176 response->set_destination(response, dst->clone(dst));
177 if (major == IKEV2_MAJOR_VERSION)
178 {
179 response->set_request(response, FALSE);
180 }
181 response->set_message_id(response, 0);
182 ike_sa_id = request->get_ike_sa_id(request);
183 ike_sa_id->switch_initiator(ike_sa_id);
184 response->set_ike_sa_id(response, ike_sa_id);
185 if (response->generate(response, NULL, &packet) == SUCCESS)
186 {
187 charon->sender->send(charon->sender, packet);
188 }
189 response->destroy(response);
190 }
191
192 /**
193 * build a cookie
194 */
195 static bool cookie_build(private_receiver_t *this, message_t *message,
196 u_int32_t t, chunk_t secret, chunk_t *cookie)
197 {
198 u_int64_t spi = message->get_initiator_spi(message);
199 host_t *ip = message->get_source(message);
200 chunk_t input, hash;
201
202 /* COOKIE = t | sha1( IPi | SPIi | t | secret ) */
203 input = chunk_cata("cccc", ip->get_address(ip), chunk_from_thing(spi),
204 chunk_from_thing(t), secret);
205 hash = chunk_alloca(this->hasher->get_hash_size(this->hasher));
206 if (!this->hasher->get_hash(this->hasher, input, hash.ptr))
207 {
208 return FALSE;
209 }
210 *cookie = chunk_cat("cc", chunk_from_thing(t), hash);
211 return TRUE;
212 }
213
214 /**
215 * verify a received cookie
216 */
217 static bool cookie_verify(private_receiver_t *this, message_t *message,
218 chunk_t cookie)
219 {
220 u_int32_t t, now;
221 chunk_t reference;
222 chunk_t secret;
223
224 now = time_monotonic(NULL);
225 t = *(u_int32_t*)cookie.ptr;
226
227 if (cookie.len != sizeof(u_int32_t) +
228 this->hasher->get_hash_size(this->hasher) ||
229 t < now - this->secret_offset - COOKIE_LIFETIME)
230 {
231 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
232 return FALSE;
233 }
234
235 /* check if cookie is derived from old_secret */
236 if (t + this->secret_offset > this->secret_switch)
237 {
238 secret = chunk_from_thing(this->secret);
239 }
240 else
241 {
242 secret = chunk_from_thing(this->secret_old);
243 }
244
245 /* compare own calculation against received */
246 if (!cookie_build(this, message, t, secret, &reference))
247 {
248 return FALSE;
249 }
250 if (chunk_equals(reference, cookie))
251 {
252 chunk_free(&reference);
253 return TRUE;
254 }
255 chunk_free(&reference);
256 return FALSE;
257 }
258
259 /**
260 * Check if a valid cookie found
261 */
262 static bool check_cookie(private_receiver_t *this, message_t *message)
263 {
264 packet_t *packet;
265 chunk_t data;
266
267 /* check for a cookie. We don't use our parser here and do it
268 * quick and dirty for performance reasons.
269 * we assume the cookie is the first payload (which is a MUST), and
270 * the cookie's SPI length is zero. */
271 packet = message->get_packet(message);
272 data = packet->get_data(packet);
273 if (data.len <
274 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH +
275 sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher) ||
276 *(data.ptr + 16) != NOTIFY ||
277 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
278 {
279 /* no cookie found */
280 packet->destroy(packet);
281 return FALSE;
282 }
283 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
284 data.len = sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher);
285 if (!cookie_verify(this, message, data))
286 {
287 DBG2(DBG_NET, "found cookie, but content invalid");
288 packet->destroy(packet);
289 return FALSE;
290 }
291 return TRUE;
292 }
293
294 /**
295 * Check if we currently require cookies
296 */
297 static bool cookie_required(private_receiver_t *this,
298 u_int half_open, u_int32_t now)
299 {
300 if (this->cookie_threshold && half_open >= this->cookie_threshold)
301 {
302 this->last_cookie = now;
303 return TRUE;
304 }
305 if (this->last_cookie && now < this->last_cookie + COOKIE_CALMDOWN_DELAY)
306 {
307 /* We don't disable cookies unless we haven't seen IKE_SA_INITs
308 * for COOKIE_CALMDOWN_DELAY seconds. This avoids jittering between
309 * cookie on / cookie off states, which is problematic. Consider the
310 * following: A legitimiate initiator sends a IKE_SA_INIT while we
311 * are under a DoS attack. If we toggle our cookie behavior,
312 * multiple retransmits of this IKE_SA_INIT might get answered with
313 * and without cookies. The initiator goes on and retries with
314 * a cookie, but it can't know if the completing IKE_SA_INIT response
315 * is to its IKE_SA_INIT request with or without cookies. This is
316 * problematic, as the cookie is part of AUTH payload data.
317 */
318 this->last_cookie = now;
319 return TRUE;
320 }
321 return FALSE;
322 }
323
324 /**
325 * Check if we should drop IKE_SA_INIT because of cookie/overload checking
326 */
327 static bool drop_ike_sa_init(private_receiver_t *this, message_t *message)
328 {
329 u_int half_open;
330 u_int32_t now;
331
332 now = time_monotonic(NULL);
333 half_open = charon->ike_sa_manager->get_half_open_count(
334 charon->ike_sa_manager, NULL);
335
336 /* check for cookies in IKEv2 */
337 if (message->get_major_version(message) == IKEV2_MAJOR_VERSION &&
338 cookie_required(this, half_open, now) && !check_cookie(this, message))
339 {
340 chunk_t cookie;
341
342 DBG2(DBG_NET, "received packet from: %#H to %#H",
343 message->get_source(message),
344 message->get_destination(message));
345 if (!cookie_build(this, message, now - this->secret_offset,
346 chunk_from_thing(this->secret), &cookie))
347 {
348 return TRUE;
349 }
350 DBG2(DBG_NET, "sending COOKIE notify to %H",
351 message->get_source(message));
352 send_notify(message, IKEV2_MAJOR_VERSION, IKE_SA_INIT, COOKIE, cookie);
353 chunk_free(&cookie);
354 if (++this->secret_used > COOKIE_REUSE)
355 {
356 char secret[SECRET_LENGTH];
357
358 DBG1(DBG_NET, "generating new cookie secret after %d uses",
359 this->secret_used);
360 if (this->rng->get_bytes(this->rng, SECRET_LENGTH, secret))
361 {
362 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
363 memcpy(this->secret, secret, SECRET_LENGTH);
364 memwipe(secret, SECRET_LENGTH);
365 this->secret_switch = now;
366 this->secret_used = 0;
367 }
368 else
369 {
370 DBG1(DBG_NET, "failed to allocated cookie secret, keeping old");
371 }
372 }
373 return TRUE;
374 }
375
376 /* check if peer has too many IKE_SAs half open */
377 if (this->block_threshold &&
378 charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
379 message->get_source(message)) >= this->block_threshold)
380 {
381 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
382 "peer too aggressive", message->get_source(message));
383 return TRUE;
384 }
385
386 /* check if global half open IKE_SA limit reached */
387 if (this->init_limit_half_open &&
388 half_open >= this->init_limit_half_open)
389 {
390 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, half open IKE_SA "
391 "count of %d exceeds limit of %d", message->get_source(message),
392 half_open, this->init_limit_half_open);
393 return TRUE;
394 }
395
396 /* check if job load acceptable */
397 if (this->init_limit_job_load)
398 {
399 u_int jobs = 0, i;
400
401 for (i = 0; i < JOB_PRIO_MAX; i++)
402 {
403 jobs += lib->processor->get_job_load(lib->processor, i);
404 }
405 if (jobs > this->init_limit_job_load)
406 {
407 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, job load of %d "
408 "exceeds limit of %d", message->get_source(message),
409 jobs, this->init_limit_job_load);
410 return TRUE;
411 }
412 }
413 return FALSE;
414 }
415
416 /**
417 * Job callback to receive packets
418 */
419 static job_requeue_t receive_packets(private_receiver_t *this)
420 {
421 ike_sa_id_t *id;
422 packet_t *packet;
423 message_t *message;
424 host_t *src, *dst;
425 status_t status;
426 bool supported = TRUE;
427 chunk_t data, marker = chunk_from_chars(0x00, 0x00, 0x00, 0x00);
428
429 /* read in a packet */
430 status = charon->socket->receive(charon->socket, &packet);
431 if (status == NOT_SUPPORTED)
432 {
433 return JOB_REQUEUE_NONE;
434 }
435 else if (status != SUCCESS)
436 {
437 DBG2(DBG_NET, "receiving from socket failed!");
438 return JOB_REQUEUE_FAIR;
439 }
440
441 data = packet->get_data(packet);
442 if (data.len == 1 && data.ptr[0] == 0xFF)
443 { /* silently drop NAT-T keepalives */
444 packet->destroy(packet);
445 return JOB_REQUEUE_DIRECT;
446 }
447 else if (data.len < marker.len)
448 { /* drop packets that are too small */
449 DBG3(DBG_NET, "received packet is too short (%d bytes)", data.len);
450 packet->destroy(packet);
451 return JOB_REQUEUE_DIRECT;
452 }
453
454 dst = packet->get_destination(packet);
455 src = packet->get_source(packet);
456 if (!hydra->kernel_interface->all_interfaces_usable(hydra->kernel_interface)
457 && !hydra->kernel_interface->get_interface(hydra->kernel_interface,
458 dst, NULL))
459 {
460 DBG3(DBG_NET, "received packet from %#H to %#H on ignored interface",
461 src, dst);
462 packet->destroy(packet);
463 return JOB_REQUEUE_DIRECT;
464 }
465
466 /* if neither source nor destination port is 500 we assume an IKE packet
467 * with Non-ESP marker or an ESP packet */
468 if (dst->get_port(dst) != IKEV2_UDP_PORT &&
469 src->get_port(src) != IKEV2_UDP_PORT)
470 {
471 if (memeq(data.ptr, marker.ptr, marker.len))
472 { /* remove Non-ESP marker */
473 packet->skip_bytes(packet, marker.len);
474 }
475 else
476 { /* this seems to be an ESP packet */
477 this->esp_cb_mutex->lock(this->esp_cb_mutex);
478 if (this->esp_cb.cb)
479 {
480 this->esp_cb.cb(this->esp_cb.data, packet);
481 }
482 else
483 {
484 packet->destroy(packet);
485 }
486 this->esp_cb_mutex->unlock(this->esp_cb_mutex);
487 return JOB_REQUEUE_DIRECT;
488 }
489 }
490
491 /* parse message header */
492 message = message_create_from_packet(packet);
493 if (message->parse_header(message) != SUCCESS)
494 {
495 DBG1(DBG_NET, "received invalid IKE header from %H - ignored",
496 packet->get_source(packet));
497 charon->bus->alert(charon->bus, ALERT_PARSE_ERROR_HEADER, message);
498 message->destroy(message);
499 return JOB_REQUEUE_DIRECT;
500 }
501
502 /* check IKE major version */
503 switch (message->get_major_version(message))
504 {
505 case IKEV2_MAJOR_VERSION:
506 #ifndef USE_IKEV2
507 if (message->get_exchange_type(message) == IKE_SA_INIT &&
508 message->get_request(message))
509 {
510 send_notify(message, IKEV1_MAJOR_VERSION, INFORMATIONAL_V1,
511 INVALID_MAJOR_VERSION, chunk_empty);
512 supported = FALSE;
513 }
514 #endif /* USE_IKEV2 */
515 break;
516 case IKEV1_MAJOR_VERSION:
517 #ifndef USE_IKEV1
518 if (message->get_exchange_type(message) == ID_PROT ||
519 message->get_exchange_type(message) == AGGRESSIVE)
520 {
521 send_notify(message, IKEV2_MAJOR_VERSION, INFORMATIONAL,
522 INVALID_MAJOR_VERSION, chunk_empty);
523 supported = FALSE;
524 }
525 #endif /* USE_IKEV1 */
526 break;
527 default:
528 #ifdef USE_IKEV2
529 send_notify(message, IKEV2_MAJOR_VERSION, INFORMATIONAL,
530 INVALID_MAJOR_VERSION, chunk_empty);
531 #endif /* USE_IKEV2 */
532 #ifdef USE_IKEV1
533 send_notify(message, IKEV1_MAJOR_VERSION, INFORMATIONAL_V1,
534 INVALID_MAJOR_VERSION, chunk_empty);
535 #endif /* USE_IKEV1 */
536 supported = FALSE;
537 break;
538 }
539 if (!supported)
540 {
541 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, sending "
542 "INVALID_MAJOR_VERSION", message->get_major_version(message),
543 message->get_minor_version(message), packet->get_source(packet));
544 message->destroy(message);
545 return JOB_REQUEUE_DIRECT;
546 }
547 if (message->get_request(message) &&
548 message->get_exchange_type(message) == IKE_SA_INIT)
549 {
550 if (this->initiator_only || drop_ike_sa_init(this, message))
551 {
552 message->destroy(message);
553 return JOB_REQUEUE_DIRECT;
554 }
555 }
556 if (message->get_exchange_type(message) == ID_PROT ||
557 message->get_exchange_type(message) == AGGRESSIVE)
558 {
559 id = message->get_ike_sa_id(message);
560 if (id->get_responder_spi(id) == 0 &&
561 (this->initiator_only || drop_ike_sa_init(this, message)))
562 {
563 message->destroy(message);
564 return JOB_REQUEUE_DIRECT;
565 }
566 }
567
568 if (this->receive_delay)
569 {
570 if (this->receive_delay_type == 0 ||
571 this->receive_delay_type == message->get_exchange_type(message))
572 {
573 if ((message->get_request(message) && this->receive_delay_request) ||
574 (!message->get_request(message) && this->receive_delay_response))
575 {
576 DBG1(DBG_NET, "using receive delay: %dms",
577 this->receive_delay);
578 lib->scheduler->schedule_job_ms(lib->scheduler,
579 (job_t*)process_message_job_create(message),
580 this->receive_delay);
581 return JOB_REQUEUE_DIRECT;
582 }
583 }
584 }
585 lib->processor->queue_job(lib->processor,
586 (job_t*)process_message_job_create(message));
587 return JOB_REQUEUE_DIRECT;
588 }
589
590 METHOD(receiver_t, add_esp_cb, void,
591 private_receiver_t *this, receiver_esp_cb_t callback, void *data)
592 {
593 this->esp_cb_mutex->lock(this->esp_cb_mutex);
594 this->esp_cb.cb = callback;
595 this->esp_cb.data = data;
596 this->esp_cb_mutex->unlock(this->esp_cb_mutex);
597 }
598
599 METHOD(receiver_t, del_esp_cb, void,
600 private_receiver_t *this, receiver_esp_cb_t callback)
601 {
602 this->esp_cb_mutex->lock(this->esp_cb_mutex);
603 if (this->esp_cb.cb == callback)
604 {
605 this->esp_cb.cb = NULL;
606 this->esp_cb.data = NULL;
607 }
608 this->esp_cb_mutex->unlock(this->esp_cb_mutex);
609 }
610
611 METHOD(receiver_t, destroy, void,
612 private_receiver_t *this)
613 {
614 this->rng->destroy(this->rng);
615 this->hasher->destroy(this->hasher);
616 this->esp_cb_mutex->destroy(this->esp_cb_mutex);
617 free(this);
618 }
619
620 /*
621 * Described in header.
622 */
623 receiver_t *receiver_create()
624 {
625 private_receiver_t *this;
626 u_int32_t now = time_monotonic(NULL);
627
628 INIT(this,
629 .public = {
630 .add_esp_cb = _add_esp_cb,
631 .del_esp_cb = _del_esp_cb,
632 .destroy = _destroy,
633 },
634 .esp_cb_mutex = mutex_create(MUTEX_TYPE_DEFAULT),
635 .secret_switch = now,
636 .secret_offset = random() % now,
637 );
638
639 if (lib->settings->get_bool(lib->settings,
640 "%s.dos_protection", TRUE, charon->name))
641 {
642 this->cookie_threshold = lib->settings->get_int(lib->settings,
643 "%s.cookie_threshold", COOKIE_THRESHOLD_DEFAULT, charon->name);
644 this->block_threshold = lib->settings->get_int(lib->settings,
645 "%s.block_threshold", BLOCK_THRESHOLD_DEFAULT, charon->name);
646 }
647 this->init_limit_job_load = lib->settings->get_int(lib->settings,
648 "%s.init_limit_job_load", 0, charon->name);
649 this->init_limit_half_open = lib->settings->get_int(lib->settings,
650 "%s.init_limit_half_open", 0, charon->name);
651 this->receive_delay = lib->settings->get_int(lib->settings,
652 "%s.receive_delay", 0, charon->name);
653 this->receive_delay_type = lib->settings->get_int(lib->settings,
654 "%s.receive_delay_type", 0, charon->name),
655 this->receive_delay_request = lib->settings->get_bool(lib->settings,
656 "%s.receive_delay_request", TRUE, charon->name),
657 this->receive_delay_response = lib->settings->get_bool(lib->settings,
658 "%s.receive_delay_response", TRUE, charon->name),
659 this->initiator_only = lib->settings->get_bool(lib->settings,
660 "%s.initiator_only", FALSE, charon->name),
661
662 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
663 if (!this->hasher)
664 {
665 DBG1(DBG_NET, "creating cookie hasher failed, no hashers supported");
666 free(this);
667 return NULL;
668 }
669 this->rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
670 if (!this->rng)
671 {
672 DBG1(DBG_NET, "creating cookie RNG failed, no RNG supported");
673 this->hasher->destroy(this->hasher);
674 free(this);
675 return NULL;
676 }
677 if (!this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret))
678 {
679 DBG1(DBG_NET, "creating cookie secret failed");
680 destroy(this);
681 return NULL;
682 }
683 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
684
685 lib->processor->queue_job(lib->processor,
686 (job_t*)callback_job_create_with_prio((callback_job_cb_t)receive_packets,
687 this, NULL, (callback_job_cancel_t)return_false, JOB_PRIO_CRITICAL));
688
689 return &this->public;
690 }
691