fixed typo
[strongswan.git] / src / charon / network / receiver.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <pthread.h>
22
23 #include "receiver.h"
24
25 #include <daemon.h>
26 #include <network/socket.h>
27 #include <network/packet.h>
28 #include <processing/jobs/job.h>
29 #include <processing/jobs/process_message_job.h>
30 #include <processing/jobs/callback_job.h>
31 #include <crypto/hashers/hasher.h>
32
33 /** lifetime of a cookie, in seconds */
34 #define COOKIE_LIFETIME 10
35 /** how many times to reuse the secret */
36 #define COOKIE_REUSE 10000
37 /** require cookies after half open IKE_SAs */
38 #define COOKIE_TRESHOLD 10
39 /** how many half open IKE_SAs per peer before blocking */
40 #define BLOCK_TRESHOLD 5
41 /** length of the secret to use for cookie calculation */
42 #define SECRET_LENGTH 16
43
44 typedef struct private_receiver_t private_receiver_t;
45
46 /**
47 * Private data of a receiver_t object.
48 */
49 struct private_receiver_t {
50 /**
51 * Public part of a receiver_t object.
52 */
53 receiver_t public;
54
55 /**
56 * Threads job receiving packets
57 */
58 callback_job_t *job;
59
60 /**
61 * Assigned thread.
62 */
63 pthread_t assigned_thread;
64
65 /**
66 * current secret to use for cookie calculation
67 */
68 char secret[SECRET_LENGTH];
69
70 /**
71 * previous secret used to verify older cookies
72 */
73 char secret_old[SECRET_LENGTH];
74
75 /**
76 * how many times we have used "secret" so far
77 */
78 u_int32_t secret_used;
79
80 /**
81 * time we did the cookie switch
82 */
83 u_int32_t secret_switch;
84
85 /**
86 * time offset to use, hides our system time
87 */
88 u_int32_t secret_offset;
89
90 /**
91 * the RNG to use for secret generation
92 */
93 rng_t *rng;
94
95 /**
96 * hasher to use for cookie calculation
97 */
98 hasher_t *hasher;
99 };
100
101 /**
102 * send a notify back to the sender
103 */
104 static void send_notify(message_t *request, notify_type_t type, chunk_t data)
105 {
106 if (request->get_request(request) &&
107 request->get_exchange_type(request) == IKE_SA_INIT)
108 {
109 message_t *response;
110 host_t *src, *dst;
111 packet_t *packet;
112 ike_sa_id_t *ike_sa_id;
113
114 response = message_create();
115 dst = request->get_source(request);
116 src = request->get_destination(request);
117 response->set_source(response, src->clone(src));
118 response->set_destination(response, dst->clone(dst));
119 response->set_exchange_type(response, request->get_exchange_type(request));
120 response->set_request(response, FALSE);
121 response->set_message_id(response, 0);
122 ike_sa_id = request->get_ike_sa_id(request);
123 ike_sa_id->switch_initiator(ike_sa_id);
124 response->set_ike_sa_id(response, ike_sa_id);
125 response->add_notify(response, FALSE, type, data);
126 if (response->generate(response, NULL, NULL, &packet) == SUCCESS)
127 {
128 charon->sender->send(charon->sender, packet);
129 response->destroy(response);
130 }
131 }
132 }
133
134 /**
135 * build a cookie
136 */
137 static chunk_t cookie_build(private_receiver_t *this, message_t *message,
138 u_int32_t t, chunk_t secret)
139 {
140 u_int64_t spi = message->get_initiator_spi(message);
141 host_t *ip = message->get_source(message);
142 chunk_t input, hash;
143
144 /* COOKIE = t | sha1( IPi | SPIi | t | secret ) */
145 input = chunk_cata("cccc", ip->get_address(ip), chunk_from_thing(spi),
146 chunk_from_thing(t), secret);
147 hash = chunk_alloca(this->hasher->get_hash_size(this->hasher));
148 this->hasher->get_hash(this->hasher, input, hash.ptr);
149 return chunk_cat("cc", chunk_from_thing(t), hash);
150 }
151
152 /**
153 * verify a received cookie
154 */
155 static bool cookie_verify(private_receiver_t *this, message_t *message,
156 chunk_t cookie)
157 {
158 u_int32_t t, now;
159 chunk_t reference;
160 chunk_t secret;
161
162 now = time(NULL);
163 t = *(u_int32_t*)cookie.ptr;
164
165 if (cookie.len != sizeof(u_int32_t) +
166 this->hasher->get_hash_size(this->hasher) ||
167 t < now - this->secret_offset - COOKIE_LIFETIME)
168 {
169 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
170 return FALSE;
171 }
172
173 /* check if cookie is derived from old_secret */
174 if (t + this->secret_offset > this->secret_switch)
175 {
176 secret = chunk_from_thing(this->secret);
177 }
178 else
179 {
180 secret = chunk_from_thing(this->secret_old);
181 }
182
183 /* compare own calculation against received */
184 reference = cookie_build(this, message, t, secret);
185 if (chunk_equals(reference, cookie))
186 {
187 chunk_free(&reference);
188 return TRUE;
189 }
190 chunk_free(&reference);
191 return FALSE;
192 }
193
194 /**
195 * check if cookies are required, and if so, a valid cookie is included
196 */
197 static bool cookie_required(private_receiver_t *this, message_t *message)
198 {
199 bool failed = FALSE;
200
201 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
202 NULL) >= COOKIE_TRESHOLD)
203 {
204 /* check for a cookie. We don't use our parser here and do it
205 * quick and dirty for performance reasons.
206 * we assume to cookie is the first payload (which is a MUST), and
207 * the cookies SPI length is zero. */
208 packet_t *packet = message->get_packet(message);
209 chunk_t data = packet->get_data(packet);
210 if (data.len <
211 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH +
212 sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher) ||
213 *(data.ptr + 16) != NOTIFY ||
214 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
215 {
216 /* no cookie found */
217 failed = TRUE;
218 }
219 else
220 {
221 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
222 data.len = sizeof(u_int32_t) + this->hasher->get_hash_size(this->hasher);
223 if (!cookie_verify(this, message, data))
224 {
225 DBG2(DBG_NET, "found cookie, but content invalid");
226 failed = TRUE;
227 }
228 }
229 packet->destroy(packet);
230 }
231 return failed;
232 }
233
234 /**
235 * check if peer has to many half open IKE_SAs
236 */
237 static bool peer_to_aggressive(private_receiver_t *this, message_t *message)
238 {
239 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
240 message->get_source(message)) >= BLOCK_TRESHOLD)
241 {
242 return TRUE;
243 }
244 return FALSE;
245 }
246
247 /**
248 * Implementation of receiver_t.receive_packets.
249 */
250 static job_requeue_t receive_packets(private_receiver_t *this)
251 {
252 packet_t *packet;
253 message_t *message;
254 job_t *job;
255
256 /* read in a packet */
257 if (charon->socket->receive(charon->socket, &packet) != SUCCESS)
258 {
259 DBG2(DBG_NET, "receiving from socket failed!");
260 return JOB_REQUEUE_FAIR;
261 }
262
263 /* parse message header */
264 message = message_create_from_packet(packet);
265 if (message->parse_header(message) != SUCCESS)
266 {
267 DBG1(DBG_NET, "received invalid IKE header from %H - ignored",
268 packet->get_source(packet));
269 message->destroy(message);
270 return JOB_REQUEUE_DIRECT;
271 }
272
273 /* check IKE major version */
274 if (message->get_major_version(message) != IKE_MAJOR_VERSION)
275 {
276 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, "
277 "sending INVALID_MAJOR_VERSION", message->get_major_version(message),
278 message->get_minor_version(message), packet->get_source(packet));
279 send_notify(message, INVALID_MAJOR_VERSION, chunk_empty);
280 message->destroy(message);
281 return JOB_REQUEUE_DIRECT;
282 }
283
284 if (message->get_request(message) &&
285 message->get_exchange_type(message) == IKE_SA_INIT)
286 {
287 /* check for cookies */
288 if (cookie_required(this, message))
289 {
290 u_int32_t now = time(NULL);
291 chunk_t cookie = cookie_build(this, message, now - this->secret_offset,
292 chunk_from_thing(this->secret));
293
294 DBG2(DBG_NET, "received packet from: %#H to %#H",
295 message->get_source(message),
296 message->get_destination(message));
297 DBG2(DBG_NET, "sending COOKIE notify to %H",
298 message->get_source(message));
299 send_notify(message, COOKIE, cookie);
300 chunk_free(&cookie);
301 if (++this->secret_used > COOKIE_REUSE)
302 {
303 /* create new cookie */
304 DBG1(DBG_NET, "generating new cookie secret after %d uses",
305 this->secret_used);
306 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
307 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
308 this->secret_switch = now;
309 this->secret_used = 0;
310 }
311 message->destroy(message);
312 return JOB_REQUEUE_DIRECT;
313 }
314
315 /* check if peer has not too many IKE_SAs half open */
316 if (peer_to_aggressive(this, message))
317 {
318 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
319 "peer too aggressive", message->get_source(message));
320 message->destroy(message);
321 return JOB_REQUEUE_DIRECT;
322 }
323 }
324 job = (job_t*)process_message_job_create(message);
325 charon->processor->queue_job(charon->processor, job);
326 return JOB_REQUEUE_DIRECT;
327 }
328
329 /**
330 * Implementation of receiver_t.destroy.
331 */
332 static void destroy(private_receiver_t *this)
333 {
334 this->job->cancel(this->job);
335 this->rng->destroy(this->rng);
336 this->hasher->destroy(this->hasher);
337 free(this);
338 }
339
340 /*
341 * Described in header.
342 */
343 receiver_t *receiver_create()
344 {
345 private_receiver_t *this = malloc_thing(private_receiver_t);
346 u_int32_t now = time(NULL);
347
348 this->public.destroy = (void(*)(receiver_t*)) destroy;
349
350 this->hasher = lib->crypto->create_hasher(lib->crypto, HASH_PREFERRED);
351 if (this->hasher == NULL)
352 {
353 DBG1(DBG_NET, "creating cookie hasher failed, no hashers supported");
354 free(this);
355 return NULL;
356 }
357 this->rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
358 if (this->rng == NULL)
359 {
360 DBG1(DBG_NET, "creating cookie RNG failed, no RNG supported");
361 this->hasher->destroy(this->hasher);
362 free(this);
363 return NULL;
364 }
365 this->secret_switch = now;
366 this->secret_offset = random() % now;
367 this->secret_used = 0;
368 this->rng->get_bytes(this->rng, SECRET_LENGTH, this->secret);
369 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
370
371 this->job = callback_job_create((callback_job_cb_t)receive_packets,
372 this, NULL, NULL);
373 charon->processor->queue_job(charon->processor, (job_t*)this->job);
374
375 return &this->public;
376 }
377