simplified capability dropping
[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 charon->drop_capabilities(charon, TRUE);
258
259 while (TRUE)
260 {
261 /* read in a packet */
262 if (charon->socket->receive(charon->socket, &packet) != SUCCESS)
263 {
264 DBG2(DBG_NET, "receiving from socket failed!");
265 /* try again after a delay */
266 sleep(1);
267 continue;
268 }
269
270 /* parse message header */
271 message = message_create_from_packet(packet);
272 if (message->parse_header(message) != SUCCESS)
273 {
274 DBG1(DBG_NET, "received invalid IKE header from %H, ignored",
275 packet->get_source(packet));
276 message->destroy(message);
277 continue;
278 }
279
280 /* check IKE major version */
281 if (message->get_major_version(message) != IKE_MAJOR_VERSION)
282 {
283 DBG1(DBG_NET, "received unsupported IKE version %d.%d from %H, "
284 "sending INVALID_MAJOR_VERSION", message->get_major_version(message),
285 message->get_minor_version(message), packet->get_source(packet));
286 send_notify(message, INVALID_MAJOR_VERSION, chunk_empty);
287 message->destroy(message);
288 continue;
289 }
290
291 if (message->get_request(message) &&
292 message->get_exchange_type(message) == IKE_SA_INIT)
293 {
294 /* check for cookies */
295 if (cookie_required(this, message))
296 {
297 u_int32_t now = time(NULL);
298 chunk_t cookie = cookie_build(this, message, now - this->secret_offset,
299 chunk_from_thing(this->secret));
300
301 DBG2(DBG_NET, "received packet from: %#H to %#H",
302 message->get_source(message),
303 message->get_destination(message));
304 DBG2(DBG_NET, "sending COOKIE notify to %H",
305 message->get_source(message));
306 send_notify(message, COOKIE, cookie);
307 chunk_free(&cookie);
308 if (++this->secret_used > COOKIE_REUSE)
309 {
310 /* create new cookie */
311 DBG1(DBG_NET, "generating new cookie secret after %d uses",
312 this->secret_used);
313 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
314 this->randomizer->get_pseudo_random_bytes(this->randomizer,
315 SECRET_LENGTH, this->secret);
316 this->secret_switch = now;
317 this->secret_used = 0;
318 }
319 message->destroy(message);
320 continue;
321 }
322
323 /* check if peer has not too many IKE_SAs half open */
324 if (peer_to_aggressive(this, message))
325 {
326 DBG1(DBG_NET, "ignoring IKE_SA setup from %H, "
327 "peer to aggressive", message->get_source(message));
328 message->destroy(message);
329 continue;
330 }
331 }
332 job = (job_t *)process_message_job_create(message);
333 charon->job_queue->add(charon->job_queue, job);
334 }
335 }
336
337 /**
338 * Implementation of receiver_t.destroy.
339 */
340 static void destroy(private_receiver_t *this)
341 {
342 pthread_cancel(this->assigned_thread);
343 pthread_join(this->assigned_thread, NULL);
344 this->randomizer->destroy(this->randomizer);
345 this->hasher->destroy(this->hasher);
346 free(this);
347 }
348
349 /*
350 * Described in header.
351 */
352 receiver_t *receiver_create()
353 {
354 private_receiver_t *this = malloc_thing(private_receiver_t);
355 u_int32_t now = time(NULL);
356
357 this->public.destroy = (void(*)(receiver_t*)) destroy;
358
359 this->randomizer = randomizer_create();
360 this->hasher = hasher_create(HASH_SHA1);
361 this->secret_switch = now;
362 this->secret_offset = random() % now;
363 this->secret_used = 0;
364 this->randomizer->get_pseudo_random_bytes(this->randomizer, SECRET_LENGTH,
365 this->secret);
366 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
367
368 if (pthread_create(&this->assigned_thread, NULL,
369 (void*)receive_packets, this) != 0)
370 {
371 free(this);
372 charon->kill(charon, "unable to create receiver thread");
373 }
374
375 return &this->public;
376 }