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
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>.
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
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>
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
48 typedef struct private_receiver_t private_receiver_t
;
51 * Private data of a receiver_t object.
53 struct private_receiver_t
{
55 * Public part of a receiver_t object.
60 * Registered callback for ESP packets
68 * Mutex for ESP callback
70 mutex_t
*esp_cb_mutex
;
73 * current secret to use for cookie calculation
75 char secret
[SECRET_LENGTH
];
78 * previous secret used to verify older cookies
80 char secret_old
[SECRET_LENGTH
];
83 * how many times we have used "secret" so far
85 u_int32_t secret_used
;
88 * time we did the cookie switch
90 u_int32_t secret_switch
;
93 * time offset to use, hides our system time
95 u_int32_t secret_offset
;
98 * the RNG to use for secret generation
103 * hasher to use for cookie calculation
108 * require cookies after this many half open IKE_SAs
110 u_int32_t cookie_threshold
;
113 * timestamp of last cookie requested
118 * how many half open IKE_SAs per peer before blocking
120 u_int32_t block_threshold
;
123 * Drop IKE_SA_INIT requests if processor job load exceeds this limit
125 u_int init_limit_job_load
;
128 * Drop IKE_SA_INIT requests if half open IKE_SA count exceeds this limit
130 u_int init_limit_half_open
;
133 * Delay for receiving incoming packets, to simulate larger RTT
138 * Specific message type to delay, 0 for any
140 int receive_delay_type
;
143 * Delay request messages?
145 bool receive_delay_request
;
148 * Delay response messages?
150 bool receive_delay_response
;
153 * Endpoint is allowed to act as an initiator only
160 * send a notify back to the sender
162 static void send_notify(message_t
*request
, int major
, exchange_type_t exchange
,
163 notify_type_t type
, chunk_t data
)
165 ike_sa_id_t
*ike_sa_id
;
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
)
179 response
->set_request(response
, FALSE
);
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
)
187 charon
->sender
->send(charon
->sender
, packet
);
189 response
->destroy(response
);
195 static bool cookie_build(private_receiver_t
*this, message_t
*message
,
196 u_int32_t t
, chunk_t secret
, chunk_t
*cookie
)
198 u_int64_t spi
= message
->get_initiator_spi(message
);
199 host_t
*ip
= message
->get_source(message
);
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
))
210 *cookie
= chunk_cat("cc", chunk_from_thing(t
), hash
);
215 * verify a received cookie
217 static bool cookie_verify(private_receiver_t
*this, message_t
*message
,
224 now
= time_monotonic(NULL
);
225 t
= *(u_int32_t
*)cookie
.ptr
;
227 if (cookie
.len
!= sizeof(u_int32_t
) +
228 this->hasher
->get_hash_size(this->hasher
) ||
229 t
< now
- this->secret_offset
- COOKIE_LIFETIME
)
231 DBG2(DBG_NET
, "received cookie lifetime expired, rejecting");
235 /* check if cookie is derived from old_secret */
236 if (t
+ this->secret_offset
> this->secret_switch
)
238 secret
= chunk_from_thing(this->secret
);
242 secret
= chunk_from_thing(this->secret_old
);
245 /* compare own calculation against received */
246 if (!cookie_build(this, message
, t
, secret
, &reference
))
250 if (chunk_equals(reference
, cookie
))
252 chunk_free(&reference
);
255 chunk_free(&reference
);
260 * Check if a valid cookie found
262 static bool check_cookie(private_receiver_t
*this, message_t
*message
)
266 /* check for a cookie. We don't use our parser here and do it
267 * quick and dirty for performance reasons.
268 * we assume the cookie is the first payload (which is a MUST), and
269 * the cookie's SPI length is zero. */
270 data
= message
->get_packet_data(message
);
272 IKE_HEADER_LENGTH
+ NOTIFY_PAYLOAD_HEADER_LENGTH
+
273 sizeof(u_int32_t
) + this->hasher
->get_hash_size(this->hasher
) ||
274 *(data
.ptr
+ 16) != PLV2_NOTIFY
||
275 *(u_int16_t
*)(data
.ptr
+ IKE_HEADER_LENGTH
+ 6) != htons(COOKIE
))
277 /* no cookie found */
280 data
.ptr
+= IKE_HEADER_LENGTH
+ NOTIFY_PAYLOAD_HEADER_LENGTH
;
281 data
.len
= sizeof(u_int32_t
) + this->hasher
->get_hash_size(this->hasher
);
282 if (!cookie_verify(this, message
, data
))
284 DBG2(DBG_NET
, "found cookie, but content invalid");
291 * Check if we currently require cookies
293 static bool cookie_required(private_receiver_t
*this,
294 u_int half_open
, u_int32_t now
)
296 if (this->cookie_threshold
&& half_open
>= this->cookie_threshold
)
298 this->last_cookie
= now
;
301 if (this->last_cookie
&& now
< this->last_cookie
+ COOKIE_CALMDOWN_DELAY
)
303 /* We don't disable cookies unless we haven't seen IKE_SA_INITs
304 * for COOKIE_CALMDOWN_DELAY seconds. This avoids jittering between
305 * cookie on / cookie off states, which is problematic. Consider the
306 * following: A legitimiate initiator sends a IKE_SA_INIT while we
307 * are under a DoS attack. If we toggle our cookie behavior,
308 * multiple retransmits of this IKE_SA_INIT might get answered with
309 * and without cookies. The initiator goes on and retries with
310 * a cookie, but it can't know if the completing IKE_SA_INIT response
311 * is to its IKE_SA_INIT request with or without cookies. This is
312 * problematic, as the cookie is part of AUTH payload data.
314 this->last_cookie
= now
;
321 * Check if we should drop IKE_SA_INIT because of cookie/overload checking
323 static bool drop_ike_sa_init(private_receiver_t
*this, message_t
*message
)
328 now
= time_monotonic(NULL
);
329 half_open
= charon
->ike_sa_manager
->get_half_open_count(
330 charon
->ike_sa_manager
, NULL
);
332 /* check for cookies in IKEv2 */
333 if (message
->get_major_version(message
) == IKEV2_MAJOR_VERSION
&&
334 cookie_required(this, half_open
, now
) && !check_cookie(this, message
))
338 DBG2(DBG_NET
, "received packet from: %#H to %#H",
339 message
->get_source(message
),
340 message
->get_destination(message
));
341 if (!cookie_build(this, message
, now
- this->secret_offset
,
342 chunk_from_thing(this->secret
), &cookie
))
346 DBG2(DBG_NET
, "sending COOKIE notify to %H",
347 message
->get_source(message
));
348 send_notify(message
, IKEV2_MAJOR_VERSION
, IKE_SA_INIT
, COOKIE
, cookie
);
350 if (++this->secret_used
> COOKIE_REUSE
)
352 char secret
[SECRET_LENGTH
];
354 DBG1(DBG_NET
, "generating new cookie secret after %d uses",
356 if (this->rng
->get_bytes(this->rng
, SECRET_LENGTH
, secret
))
358 memcpy(this->secret_old
, this->secret
, SECRET_LENGTH
);
359 memcpy(this->secret
, secret
, SECRET_LENGTH
);
360 memwipe(secret
, SECRET_LENGTH
);
361 this->secret_switch
= now
;
362 this->secret_used
= 0;
366 DBG1(DBG_NET
, "failed to allocated cookie secret, keeping old");
372 /* check if peer has too many IKE_SAs half open */
373 if (this->block_threshold
&&
374 charon
->ike_sa_manager
->get_half_open_count(charon
->ike_sa_manager
,
375 message
->get_source(message
)) >= this->block_threshold
)
377 DBG1(DBG_NET
, "ignoring IKE_SA setup from %H, "
378 "peer too aggressive", message
->get_source(message
));
382 /* check if global half open IKE_SA limit reached */
383 if (this->init_limit_half_open
&&
384 half_open
>= this->init_limit_half_open
)
386 DBG1(DBG_NET
, "ignoring IKE_SA setup from %H, half open IKE_SA "
387 "count of %d exceeds limit of %d", message
->get_source(message
),
388 half_open
, this->init_limit_half_open
);
392 /* check if job load acceptable */
393 if (this->init_limit_job_load
)
397 for (i
= 0; i
< JOB_PRIO_MAX
; i
++)
399 jobs
+= lib
->processor
->get_job_load(lib
->processor
, i
);
401 if (jobs
> this->init_limit_job_load
)
403 DBG1(DBG_NET
, "ignoring IKE_SA setup from %H, job load of %d "
404 "exceeds limit of %d", message
->get_source(message
),
405 jobs
, this->init_limit_job_load
);
413 * Job callback to receive packets
415 static job_requeue_t
receive_packets(private_receiver_t
*this)
422 bool supported
= TRUE
;
423 chunk_t data
, marker
= chunk_from_chars(0x00, 0x00, 0x00, 0x00);
425 /* read in a packet */
426 status
= charon
->socket
->receive(charon
->socket
, &packet
);
427 if (status
== NOT_SUPPORTED
)
429 return JOB_REQUEUE_NONE
;
431 else if (status
!= SUCCESS
)
433 DBG2(DBG_NET
, "receiving from socket failed!");
434 return JOB_REQUEUE_FAIR
;
437 data
= packet
->get_data(packet
);
438 if (data
.len
== 1 && data
.ptr
[0] == 0xFF)
439 { /* silently drop NAT-T keepalives */
440 packet
->destroy(packet
);
441 return JOB_REQUEUE_DIRECT
;
443 else if (data
.len
< marker
.len
)
444 { /* drop packets that are too small */
445 DBG3(DBG_NET
, "received packet is too short (%d bytes)", data
.len
);
446 packet
->destroy(packet
);
447 return JOB_REQUEUE_DIRECT
;
450 dst
= packet
->get_destination(packet
);
451 src
= packet
->get_source(packet
);
452 if (!hydra
->kernel_interface
->all_interfaces_usable(hydra
->kernel_interface
)
453 && !hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
456 DBG3(DBG_NET
, "received packet from %#H to %#H on ignored interface",
458 packet
->destroy(packet
);
459 return JOB_REQUEUE_DIRECT
;
462 /* if neither source nor destination port is 500 we assume an IKE packet
463 * with Non-ESP marker or an ESP packet */
464 if (dst
->get_port(dst
) != IKEV2_UDP_PORT
&&
465 src
->get_port(src
) != IKEV2_UDP_PORT
)
467 if (memeq(data
.ptr
, marker
.ptr
, marker
.len
))
468 { /* remove Non-ESP marker */
469 packet
->skip_bytes(packet
, marker
.len
);
472 { /* this seems to be an ESP packet */
473 this->esp_cb_mutex
->lock(this->esp_cb_mutex
);
476 this->esp_cb
.cb(this->esp_cb
.data
, packet
);
480 packet
->destroy(packet
);
482 this->esp_cb_mutex
->unlock(this->esp_cb_mutex
);
483 return JOB_REQUEUE_DIRECT
;
487 /* parse message header */
488 message
= message_create_from_packet(packet
);
489 if (message
->parse_header(message
) != SUCCESS
)
491 DBG1(DBG_NET
, "received invalid IKE header from %H - ignored",
492 packet
->get_source(packet
));
493 charon
->bus
->alert(charon
->bus
, ALERT_PARSE_ERROR_HEADER
, message
);
494 message
->destroy(message
);
495 return JOB_REQUEUE_DIRECT
;
498 /* check IKE major version */
499 switch (message
->get_major_version(message
))
501 case IKEV2_MAJOR_VERSION
:
503 if (message
->get_exchange_type(message
) == IKE_SA_INIT
&&
504 message
->get_request(message
))
506 send_notify(message
, IKEV1_MAJOR_VERSION
, INFORMATIONAL_V1
,
507 INVALID_MAJOR_VERSION
, chunk_empty
);
510 #endif /* USE_IKEV2 */
512 case IKEV1_MAJOR_VERSION
:
514 if (message
->get_exchange_type(message
) == ID_PROT
||
515 message
->get_exchange_type(message
) == AGGRESSIVE
)
517 send_notify(message
, IKEV2_MAJOR_VERSION
, INFORMATIONAL
,
518 INVALID_MAJOR_VERSION
, chunk_empty
);
521 #endif /* USE_IKEV1 */
525 send_notify(message
, IKEV2_MAJOR_VERSION
, INFORMATIONAL
,
526 INVALID_MAJOR_VERSION
, chunk_empty
);
527 #elif defined(USE_IKEV1)
528 send_notify(message
, IKEV1_MAJOR_VERSION
, INFORMATIONAL_V1
,
529 INVALID_MAJOR_VERSION
, chunk_empty
);
530 #endif /* USE_IKEV1 */
536 DBG1(DBG_NET
, "received unsupported IKE version %d.%d from %H, sending "
537 "INVALID_MAJOR_VERSION", message
->get_major_version(message
),
538 message
->get_minor_version(message
), packet
->get_source(packet
));
539 message
->destroy(message
);
540 return JOB_REQUEUE_DIRECT
;
542 if (message
->get_request(message
) &&
543 message
->get_exchange_type(message
) == IKE_SA_INIT
)
545 if (this->initiator_only
|| drop_ike_sa_init(this, message
))
547 message
->destroy(message
);
548 return JOB_REQUEUE_DIRECT
;
551 if (message
->get_exchange_type(message
) == ID_PROT
||
552 message
->get_exchange_type(message
) == AGGRESSIVE
)
554 id
= message
->get_ike_sa_id(message
);
555 if (id
->get_responder_spi(id
) == 0 &&
556 (this->initiator_only
|| drop_ike_sa_init(this, message
)))
558 message
->destroy(message
);
559 return JOB_REQUEUE_DIRECT
;
563 if (this->receive_delay
)
565 if (this->receive_delay_type
== 0 ||
566 this->receive_delay_type
== message
->get_exchange_type(message
))
568 if ((message
->get_request(message
) && this->receive_delay_request
) ||
569 (!message
->get_request(message
) && this->receive_delay_response
))
571 DBG1(DBG_NET
, "using receive delay: %dms",
572 this->receive_delay
);
573 lib
->scheduler
->schedule_job_ms(lib
->scheduler
,
574 (job_t
*)process_message_job_create(message
),
575 this->receive_delay
);
576 return JOB_REQUEUE_DIRECT
;
580 lib
->processor
->queue_job(lib
->processor
,
581 (job_t
*)process_message_job_create(message
));
582 return JOB_REQUEUE_DIRECT
;
585 METHOD(receiver_t
, add_esp_cb
, void,
586 private_receiver_t
*this, receiver_esp_cb_t callback
, void *data
)
588 this->esp_cb_mutex
->lock(this->esp_cb_mutex
);
589 this->esp_cb
.cb
= callback
;
590 this->esp_cb
.data
= data
;
591 this->esp_cb_mutex
->unlock(this->esp_cb_mutex
);
594 METHOD(receiver_t
, del_esp_cb
, void,
595 private_receiver_t
*this, receiver_esp_cb_t callback
)
597 this->esp_cb_mutex
->lock(this->esp_cb_mutex
);
598 if (this->esp_cb
.cb
== callback
)
600 this->esp_cb
.cb
= NULL
;
601 this->esp_cb
.data
= NULL
;
603 this->esp_cb_mutex
->unlock(this->esp_cb_mutex
);
606 METHOD(receiver_t
, destroy
, void,
607 private_receiver_t
*this)
609 this->rng
->destroy(this->rng
);
610 this->hasher
->destroy(this->hasher
);
611 this->esp_cb_mutex
->destroy(this->esp_cb_mutex
);
616 * Described in header.
618 receiver_t
*receiver_create()
620 private_receiver_t
*this;
621 u_int32_t now
= time_monotonic(NULL
);
625 .add_esp_cb
= _add_esp_cb
,
626 .del_esp_cb
= _del_esp_cb
,
629 .esp_cb_mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
630 .secret_switch
= now
,
631 .secret_offset
= random() % now
,
634 if (lib
->settings
->get_bool(lib
->settings
,
635 "%s.dos_protection", TRUE
, lib
->ns
))
637 this->cookie_threshold
= lib
->settings
->get_int(lib
->settings
,
638 "%s.cookie_threshold", COOKIE_THRESHOLD_DEFAULT
, lib
->ns
);
639 this->block_threshold
= lib
->settings
->get_int(lib
->settings
,
640 "%s.block_threshold", BLOCK_THRESHOLD_DEFAULT
, lib
->ns
);
642 this->init_limit_job_load
= lib
->settings
->get_int(lib
->settings
,
643 "%s.init_limit_job_load", 0, lib
->ns
);
644 this->init_limit_half_open
= lib
->settings
->get_int(lib
->settings
,
645 "%s.init_limit_half_open", 0, lib
->ns
);
646 this->receive_delay
= lib
->settings
->get_int(lib
->settings
,
647 "%s.receive_delay", 0, lib
->ns
);
648 this->receive_delay_type
= lib
->settings
->get_int(lib
->settings
,
649 "%s.receive_delay_type", 0, lib
->ns
),
650 this->receive_delay_request
= lib
->settings
->get_bool(lib
->settings
,
651 "%s.receive_delay_request", TRUE
, lib
->ns
),
652 this->receive_delay_response
= lib
->settings
->get_bool(lib
->settings
,
653 "%s.receive_delay_response", TRUE
, lib
->ns
),
654 this->initiator_only
= lib
->settings
->get_bool(lib
->settings
,
655 "%s.initiator_only", FALSE
, lib
->ns
),
657 this->hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
660 DBG1(DBG_NET
, "creating cookie hasher failed, no hashers supported");
664 this->rng
= lib
->crypto
->create_rng(lib
->crypto
, RNG_STRONG
);
667 DBG1(DBG_NET
, "creating cookie RNG failed, no RNG supported");
668 this->hasher
->destroy(this->hasher
);
672 if (!this->rng
->get_bytes(this->rng
, SECRET_LENGTH
, this->secret
))
674 DBG1(DBG_NET
, "creating cookie secret failed");
678 memcpy(this->secret_old
, this->secret
, SECRET_LENGTH
);
680 lib
->processor
->queue_job(lib
->processor
,
681 (job_t
*)callback_job_create_with_prio((callback_job_cb_t
)receive_packets
,
682 this, NULL
, (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
684 return &this->public;