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