fixed casting
[strongswan.git] / src / charon / network / receiver.c
1 /**
2 * @file receiver.c
3 *
4 * @brief Implementation of receiver_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stdlib.h>
25 #include <pthread.h>
26
27 #include "receiver.h"
28
29 #include <daemon.h>
30 #include <network/socket.h>
31 #include <network/packet.h>
32 #include <processing/job_queue.h>
33 #include <processing/jobs/job.h>
34 #include <processing/jobs/process_message_job.h>
35
36 /** length of the full cookie, including time (u_int32_t + SHA1()) */
37 #define COOKIE_LENGTH 24
38 /** lifetime of a cookie, in seconds */
39 #define COOKIE_LIFETIME 10
40 /** how many times to reuse the secret */
41 #define COOKIE_REUSE 10000
42 /** require cookies after half open IKE_SAs */
43 #define COOKIE_TRESHOLD 10
44 /** how many half open IKE_SAs per peer before blocking */
45 #define BLOCK_TRESHOLD 5
46 /** length of the secret to use for cookie calculation */
47 #define SECRET_LENGTH 16
48
49 typedef struct private_receiver_t private_receiver_t;
50
51 /**
52 * Private data of a receiver_t object.
53 */
54 struct private_receiver_t {
55 /**
56 * Public part of a receiver_t object.
57 */
58 receiver_t public;
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 randomizer to use for secret generation
92 */
93 randomizer_t *randomizer;
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 = chunk_alloca(this->hasher->get_hash_size(this->hasher));
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 this->hasher->get_hash(this->hasher, input, hash.ptr);
148 return chunk_cat("cc", chunk_from_thing(t), hash);
149 }
150
151 /**
152 * verify a received cookie
153 */
154 static bool cookie_verify(private_receiver_t *this, message_t *message,
155 chunk_t cookie)
156 {
157 u_int32_t t, now;
158 chunk_t reference;
159 chunk_t secret;
160
161 now = time(NULL);
162 t = *(u_int32_t*)cookie.ptr;
163
164 if (cookie.len != COOKIE_LENGTH ||
165 t < now - this->secret_offset - COOKIE_LIFETIME)
166 {
167 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
168 return FALSE;
169 }
170
171 /* check if cookie is derived from old_secret */
172 if (t + this->secret_offset > this->secret_switch)
173 {
174 secret = chunk_from_thing(this->secret);
175 }
176 else
177 {
178 secret = chunk_from_thing(this->secret_old);
179 }
180
181 /* compare own calculation against received */
182 reference = cookie_build(this, message, t, secret);
183 if (chunk_equals(reference, cookie))
184 {
185 chunk_free(&reference);
186 return TRUE;
187 }
188 chunk_free(&reference);
189 return FALSE;
190 }
191
192 /**
193 * check if cookies are required, and if so, a valid cookie is included
194 */
195 static bool cookie_required(private_receiver_t *this, message_t *message)
196 {
197 bool failed = FALSE;
198
199 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
200 NULL) >= COOKIE_TRESHOLD)
201 {
202 /* check for a cookie. We don't use our parser here and do it
203 * quick and dirty for performance reasons.
204 * we assume to cookie is the first payload (which is a MUST), and
205 * the cookies SPI length is zero. */
206 packet_t *packet = message->get_packet(message);
207 chunk_t data = packet->get_data(packet);
208 if (data.len <
209 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH + COOKIE_LENGTH ||
210 *(data.ptr + 16) != NOTIFY ||
211 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
212 {
213 /* no cookie found */
214 failed = TRUE;
215 }
216 else
217 {
218 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
219 data.len = COOKIE_LENGTH;
220 if (!cookie_verify(this, message, data))
221 {
222 DBG2(DBG_NET, "found cookie, but content invalid");
223 failed = TRUE;
224 }
225 }
226 packet->destroy(packet);
227 }
228 return failed;
229 }
230
231 /**
232 * check if peer has to many half open IKE_SAs
233 */
234 static bool peer_to_aggressive(private_receiver_t *this, message_t *message)
235 {
236 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
237 message->get_source(message)) >= BLOCK_TRESHOLD)
238 {
239 return TRUE;
240 }
241 return FALSE;
242 }
243
244 /**
245 * Implementation of receiver_t.receive_packets.
246 */
247 static void receive_packets(private_receiver_t *this)
248 {
249 packet_t *packet;
250 message_t *message;
251 job_t *job;
252
253 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
254 DBG1(DBG_NET, "receiver thread running, thread_ID: %06u",
255 (int)pthread_self());
256
257 while (TRUE)
258 {
259 /* read in a packet */
260 if (charon->socket->receive(charon->socket, &packet) != SUCCESS)
261 {
262 DBG1(DBG_NET, "receiving from socket failed!");
263 continue;
264 }
265
266 /* parse message header */
267 message = message_create_from_packet(packet);
268 if (message->parse_header(message) != SUCCESS)
269 {
270 DBG1(DBG_NET, "received invalid IKE header from %H, ignored",
271 packet->get_source(packet));
272 message->destroy(message);
273 continue;
274 }
275
276 /* check IKE major version */
277 if (message->get_major_version(message) != IKE_MAJOR_VERSION)
278 {
279 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, "
280 "sending INVALID_MAJOR_VERSION", message->get_major_version(message),
281 message->get_minor_version(message), packet->get_source(packet));
282 send_notify(message, INVALID_MAJOR_VERSION, chunk_empty);
283 message->destroy(message);
284 continue;
285 }
286
287 if (message->get_request(message) &&
288 message->get_exchange_type(message) == IKE_SA_INIT)
289 {
290 /* check for cookies */
291 if (cookie_required(this, message))
292 {
293 u_int32_t now = time(NULL);
294 chunk_t cookie = cookie_build(this, message, now - this->secret_offset,
295 chunk_from_thing(this->secret));
296
297 DBG2(DBG_NET, "received packet from: %#H to %#H",
298 message->get_source(message),
299 message->get_destination(message));
300 DBG2(DBG_NET, "sending COOKIE notify to %H",
301 message->get_source(message));
302 send_notify(message, COOKIE, cookie);
303 chunk_free(&cookie);
304 if (++this->secret_used > COOKIE_REUSE)
305 {
306 /* create new cookie */
307 DBG1(DBG_NET, "generating new cookie secret after %d uses",
308 this->secret_used);
309 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
310 this->randomizer->get_pseudo_random_bytes(this->randomizer,
311 SECRET_LENGTH, this->secret);
312 this->secret_switch = now;
313 this->secret_used = 0;
314 }
315 message->destroy(message);
316 continue;
317 }
318
319 /* check if peer has not too many IKE_SAs half open */
320 if (peer_to_aggressive(this, message))
321 {
322 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
323 "peer to aggressive", message->get_source(message));
324 message->destroy(message);
325 continue;
326 }
327 }
328 job = (job_t *)process_message_job_create(message);
329 charon->job_queue->add(charon->job_queue, job);
330 }
331 }
332
333 /**
334 * Implementation of receiver_t.destroy.
335 */
336 static void destroy(private_receiver_t *this)
337 {
338 pthread_cancel(this->assigned_thread);
339 pthread_join(this->assigned_thread, NULL);
340 this->randomizer->destroy(this->randomizer);
341 this->hasher->destroy(this->hasher);
342 free(this);
343 }
344
345 /*
346 * Described in header.
347 */
348 receiver_t *receiver_create()
349 {
350 private_receiver_t *this = malloc_thing(private_receiver_t);
351 u_int32_t now = time(NULL);
352
353 this->public.destroy = (void(*)(receiver_t*)) destroy;
354
355 this->randomizer = randomizer_create();
356 this->hasher = hasher_create(HASH_SHA1);
357 this->secret_switch = now;
358 this->secret_offset = random() % now;
359 this->secret_used = 0;
360 this->randomizer->get_pseudo_random_bytes(this->randomizer, SECRET_LENGTH,
361 this->secret);
362 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
363
364 if (pthread_create(&this->assigned_thread, NULL,
365 (void*)receive_packets, this) != 0)
366 {
367 free(this);
368 charon->kill(charon, "unable to create receiver thread");
369 }
370
371 return &this->public;
372 }