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