Refer to scheduler via hydra and not charon.
[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 <hydra.h>
24 #include <daemon.h>
25 #include <network/socket.h>
26 #include <network/packet.h>
27 #include <processing/jobs/job.h>
28 #include <processing/jobs/process_message_job.h>
29 #include <processing/jobs/callback_job.h>
30 #include <crypto/hashers/hasher.h>
31
32 /** lifetime of a cookie, in seconds */
33 #define COOKIE_LIFETIME 10
34 /** how many times to reuse the secret */
35 #define COOKIE_REUSE 10000
36 /** default value for private_receiver_t.cookie_threshold */
37 #define COOKIE_THRESHOLD_DEFAULT 10
38 /** default value for private_receiver_t.block_threshold */
39 #define BLOCK_THRESHOLD_DEFAULT 5
40 /** length of the secret to use for cookie calculation */
41 #define SECRET_LENGTH 16
42
43 typedef struct private_receiver_t private_receiver_t;
44
45 /**
46 * Private data of a receiver_t object.
47 */
48 struct private_receiver_t {
49 /**
50 * Public part of a receiver_t object.
51 */
52 receiver_t public;
53
54 /**
55 * Threads job receiving packets
56 */
57 callback_job_t *job;
58
59 /**
60 * current secret to use for cookie calculation
61 */
62 char secret[SECRET_LENGTH];
63
64 /**
65 * previous secret used to verify older cookies
66 */
67 char secret_old[SECRET_LENGTH];
68
69 /**
70 * how many times we have used "secret" so far
71 */
72 u_int32_t secret_used;
73
74 /**
75 * time we did the cookie switch
76 */
77 u_int32_t secret_switch;
78
79 /**
80 * time offset to use, hides our system time
81 */
82 u_int32_t secret_offset;
83
84 /**
85 * the RNG to use for secret generation
86 */
87 rng_t *rng;
88
89 /**
90 * hasher to use for cookie calculation
91 */
92 hasher_t *hasher;
93
94 /**
95 * require cookies after this many half open IKE_SAs
96 */
97 u_int32_t cookie_threshold;
98
99 /**
100 * how many half open IKE_SAs per peer before blocking
101 */
102 u_int32_t block_threshold;
103
104 /**
105 * Delay for receiving incoming packets, to simulate larger RTT
106 */
107 int receive_delay;
108
109 /**
110 * Specific message type to delay, 0 for any
111 */
112 int receive_delay_type;
113
114 /**
115 * Delay request messages?
116 */
117 bool receive_delay_request;
118
119 /**
120 * Delay response messages?
121 */
122 bool receive_delay_response;
123 };
124
125 /**
126 * send a notify back to the sender
127 */
128 static void send_notify(message_t *request, notify_type_t type, chunk_t data)
129 {
130 if (request->get_request(request) &&
131 request->get_exchange_type(request) == IKE_SA_INIT)
132 {
133 message_t *response;
134 host_t *src, *dst;
135 packet_t *packet;
136 ike_sa_id_t *ike_sa_id;
137
138 response = message_create();
139 dst = request->get_source(request);
140 src = request->get_destination(request);
141 response->set_source(response, src->clone(src));
142 response->set_destination(response, dst->clone(dst));
143 response->set_exchange_type(response, request->get_exchange_type(request));
144 response->set_request(response, FALSE);
145 response->set_message_id(response, 0);
146 ike_sa_id = request->get_ike_sa_id(request);
147 ike_sa_id->switch_initiator(ike_sa_id);
148 response->set_ike_sa_id(response, ike_sa_id);
149 response->add_notify(response, FALSE, type, data);
150 if (response->generate(response, NULL, &packet) == SUCCESS)
151 {
152 charon->sender->send(charon->sender, packet);
153 response->destroy(response);
154 }
155 }
156 }
157
158 /**
159 * build a cookie
160 */
161 static chunk_t cookie_build(private_receiver_t *this, message_t *message,
162 u_int32_t t, chunk_t secret)
163 {
164 u_int64_t spi = message->get_initiator_spi(message);
165 host_t *ip = message->get_source(message);
166 chunk_t input, hash;
167
168 /* COOKIE = t | sha1( IPi | SPIi | t | secret ) */
169 input = chunk_cata("cccc", ip->get_address(ip), chunk_from_thing(spi),
170 chunk_from_thing(t), secret);
171 hash = chunk_alloca(this->hasher->get_hash_size(this->hasher));
172 this->hasher->get_hash(this->hasher, input, hash.ptr);
173 return chunk_cat("cc", chunk_from_thing(t), hash);
174 }
175
176 /**
177 * verify a received cookie
178 */
179 static bool cookie_verify(private_receiver_t *this, message_t *message,
180 chunk_t cookie)
181 {
182 u_int32_t t, now;
183 chunk_t reference;
184 chunk_t secret;
185
186 now = time_monotonic(NULL);
187 t = *(u_int32_t*)cookie.ptr;
188
189 if (cookie.len != sizeof(u_int32_t) +
190 this->hasher->get_hash_size(this->hasher) ||
191 t < now - this->secret_offset - COOKIE_LIFETIME)
192 {
193 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
194 return FALSE;
195 }
196
197 /* check if cookie is derived from old_secret */
198 if (t + this->secret_offset > this->secret_switch)
199 {
200 secret = chunk_from_thing(this->secret);
201 }
202 else
203 {
204 secret = chunk_from_thing(this->secret_old);
205 }
206
207 /* compare own calculation against received */
208 reference = cookie_build(this, message, t, secret);
209 if (chunk_equals(reference, cookie))
210 {
211 chunk_free(&reference);
212 return TRUE;
213 }
214 chunk_free(&reference);
215 return FALSE;
216 }
217
218 /**
219 * check if cookies are required, and if so, a valid cookie is included
220 */
221 static bool cookie_required(private_receiver_t *this, message_t *message)
222 {
223 bool failed = FALSE;
224
225 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
226 NULL) >= this->cookie_threshold)
227 {
228 /* check for a cookie. We don't use our parser here and do it
229 * quick and dirty for performance reasons.
230 * we assume the cookie is the first payload (which is a MUST), and
231 * the cookie's SPI length is zero. */
232 packet_t *packet = message->get_packet(message);
233 chunk_t data = packet->get_data(packet);
234 if (data.len <
235 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH +
236 sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher) ||
237 *(data.ptr + 16) != NOTIFY ||
238 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
239 {
240 /* no cookie found */
241 failed = TRUE;
242 }
243 else
244 {
245 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
246 data.len = sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher);
247 if (!cookie_verify(this, message, data))
248 {
249 DBG2(DBG_NET, "found cookie, but content invalid");
250 failed = TRUE;
251 }
252 }
253 packet->destroy(packet);
254 }
255 return failed;
256 }
257
258 /**
259 * check if peer has to many half open IKE_SAs
260 */
261 static bool peer_too_aggressive(private_receiver_t *this, message_t *message)
262 {
263 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
264 message->get_source(message)) >= this->block_threshold)
265 {
266 return TRUE;
267 }
268 return FALSE;
269 }
270
271 /**
272 * Job callback to receive packets
273 */
274 static job_requeue_t receive_packets(private_receiver_t *this)
275 {
276 packet_t *packet;
277 message_t *message;
278
279 /* read in a packet */
280 if (charon->socket->receive(charon->socket, &packet) != SUCCESS)
281 {
282 DBG2(DBG_NET, "receiving from socket failed!");
283 return JOB_REQUEUE_FAIR;
284 }
285
286 /* parse message header */
287 message = message_create_from_packet(packet);
288 if (message->parse_header(message) != SUCCESS)
289 {
290 DBG1(DBG_NET, "received invalid IKE header from %H - ignored",
291 packet->get_source(packet));
292 message->destroy(message);
293 return JOB_REQUEUE_DIRECT;
294 }
295
296 /* check IKE major version */
297 if (message->get_major_version(message) != IKE_MAJOR_VERSION)
298 {
299 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, "
300 "sending INVALID_MAJOR_VERSION", message->get_major_version(message),
301 message->get_minor_version(message), packet->get_source(packet));
302 send_notify(message, INVALID_MAJOR_VERSION, chunk_empty);
303 message->destroy(message);
304 return JOB_REQUEUE_DIRECT;
305 }
306
307 if (message->get_request(message) &&
308 message->get_exchange_type(message) == IKE_SA_INIT)
309 {
310 /* check for cookies */
311 if (this->cookie_threshold && cookie_required(this, message))
312 {
313 u_int32_t now = time_monotonic(NULL);
314 chunk_t cookie = cookie_build(this, message, now - this->secret_offset,
315 chunk_from_thing(this->secret));
316
317 DBG2(DBG_NET, "received packet from: %#H to %#H",
318 message->get_source(message),
319 message->get_destination(message));
320 DBG2(DBG_NET, "sending COOKIE notify to %H",
321 message->get_source(message));
322 send_notify(message, COOKIE, cookie);
323 chunk_free(&cookie);
324 if (++this->secret_used > COOKIE_REUSE)
325 {
326 /* create new cookie */
327 DBG1(DBG_NET, "generating new cookie secret after %d uses",
328 this->secret_used);
329 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
330 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
331 this->secret_switch = now;
332 this->secret_used = 0;
333 }
334 message->destroy(message);
335 return JOB_REQUEUE_DIRECT;
336 }
337
338 /* check if peer has not too many IKE_SAs half open */
339 if (this->block_threshold && peer_too_aggressive(this, message))
340 {
341 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
342 "peer too aggressive", message->get_source(message));
343 message->destroy(message);
344 return JOB_REQUEUE_DIRECT;
345 }
346 }
347 if (this->receive_delay)
348 {
349 if (this->receive_delay_type == 0 ||
350 this->receive_delay_type == message->get_exchange_type(message))
351 {
352 if ((message->get_request(message) && this->receive_delay_request) ||
353 (!message->get_request(message) && this->receive_delay_response))
354 {
355 DBG1(DBG_NET, "using receive delay: %dms",
356 this->receive_delay);
357 hydra->scheduler->schedule_job_ms(hydra->scheduler,
358 (job_t*)process_message_job_create(message),
359 this->receive_delay);
360 return JOB_REQUEUE_DIRECT;
361 }
362 }
363 }
364 hydra->processor->queue_job(hydra->processor,
365 (job_t*)process_message_job_create(message));
366 return JOB_REQUEUE_DIRECT;
367 }
368
369 METHOD(receiver_t, destroy, void,
370 private_receiver_t *this)
371 {
372 this->job->cancel(this->job);
373 this->rng->destroy(this->rng);
374 this->hasher->destroy(this->hasher);
375 free(this);
376 }
377
378 /*
379 * Described in header.
380 */
381 receiver_t *receiver_create()
382 {
383 private_receiver_t *this;
384 u_int32_t now = time_monotonic(NULL);
385
386 INIT(this,
387 .public = {
388 .destroy = _destroy,
389 },
390 .secret_switch = now,
391 .secret_offset = random() % now,
392 );
393
394 if (lib->settings->get_bool(lib->settings, "charon.dos_protection", TRUE))
395 {
396 this->cookie_threshold = lib->settings->get_int(lib->settings,
397 "charon.cookie_threshold", COOKIE_THRESHOLD_DEFAULT);
398 this->block_threshold = lib->settings->get_int(lib->settings,
399 "charon.block_threshold", BLOCK_THRESHOLD_DEFAULT);
400 }
401 this->receive_delay = lib->settings->get_int(lib->settings,
402 "charon.receive_delay", 0);
403 this->receive_delay_type = lib->settings->get_int(lib->settings,
404 "charon.receive_delay_type", 0),
405 this->receive_delay_request = lib->settings->get_bool(lib->settings,
406 "charon.receive_delay_request", TRUE),
407 this->receive_delay_response = lib->settings->get_int(lib->settings,
408 "charon.receive_delay_response", TRUE),
409
410 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
411 if (this->hasher == NULL)
412 {
413 DBG1(DBG_NET, "creating cookie hasher failed, no hashers supported");
414 free(this);
415 return NULL;
416 }
417 this->rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
418 if (this->rng == NULL)
419 {
420 DBG1(DBG_NET, "creating cookie RNG failed, no RNG supported");
421 this->hasher->destroy(this->hasher);
422 free(this);
423 return NULL;
424 }
425 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
426 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
427
428 this->job = callback_job_create((callback_job_cb_t)receive_packets,
429 this, NULL, NULL);
430 hydra->processor->queue_job(hydra->processor, (job_t*)this->job);
431
432 return &this->public;
433 }
434