show kind of notify contained in messages in log
[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 <unistd.h>
26 #include <pthread.h>
27
28 #include "receiver.h"
29
30 #include <daemon.h>
31 #include <network/socket.h>
32 #include <network/packet.h>
33 #include <processing/jobs/job.h>
34 #include <processing/jobs/process_message_job.h>
35 #include <processing/jobs/callback_job.h>
36
37 /** length of the full cookie, including time (u_int32_t + SHA1()) */
38 #define COOKIE_LENGTH 24
39 /** lifetime of a cookie, in seconds */
40 #define COOKIE_LIFETIME 10
41 /** how many times to reuse the secret */
42 #define COOKIE_REUSE 10000
43 /** require cookies after half open IKE_SAs */
44 #define COOKIE_TRESHOLD 10
45 /** how many half open IKE_SAs per peer before blocking */
46 #define BLOCK_TRESHOLD 5
47 /** length of the secret to use for cookie calculation */
48 #define SECRET_LENGTH 16
49
50 typedef struct private_receiver_t private_receiver_t;
51
52 /**
53 * Private data of a receiver_t object.
54 */
55 struct private_receiver_t {
56 /**
57 * Public part of a receiver_t object.
58 */
59 receiver_t public;
60
61 /**
62 * Threads job receiving packets
63 */
64 callback_job_t *job;
65
66 /**
67 * Assigned thread.
68 */
69 pthread_t assigned_thread;
70
71 /**
72 * current secret to use for cookie calculation
73 */
74 char secret[SECRET_LENGTH];
75
76 /**
77 * previous secret used to verify older cookies
78 */
79 char secret_old[SECRET_LENGTH];
80
81 /**
82 * how many times we have used "secret" so far
83 */
84 u_int32_t secret_used;
85
86 /**
87 * time we did the cookie switch
88 */
89 u_int32_t secret_switch;
90
91 /**
92 * time offset to use, hides our system time
93 */
94 u_int32_t secret_offset;
95
96 /**
97 * the randomizer to use for secret generation
98 */
99 randomizer_t *randomizer;
100
101 /**
102 * hasher to use for cookie calculation
103 */
104 hasher_t *hasher;
105 };
106
107 /**
108 * send a notify back to the sender
109 */
110 static void send_notify(message_t *request, notify_type_t type, chunk_t data)
111 {
112 if (request->get_request(request) &&
113 request->get_exchange_type(request) == IKE_SA_INIT)
114 {
115 message_t *response;
116 host_t *src, *dst;
117 packet_t *packet;
118 ike_sa_id_t *ike_sa_id;
119
120 response = message_create();
121 dst = request->get_source(request);
122 src = request->get_destination(request);
123 response->set_source(response, src->clone(src));
124 response->set_destination(response, dst->clone(dst));
125 response->set_exchange_type(response, request->get_exchange_type(request));
126 response->set_request(response, FALSE);
127 response->set_message_id(response, 0);
128 ike_sa_id = request->get_ike_sa_id(request);
129 ike_sa_id->switch_initiator(ike_sa_id);
130 response->set_ike_sa_id(response, ike_sa_id);
131 response->add_notify(response, FALSE, type, data);
132 if (response->generate(response, NULL, NULL, &packet) == SUCCESS)
133 {
134 charon->sender->send(charon->sender, packet);
135 response->destroy(response);
136 }
137 }
138 }
139
140 /**
141 * build a cookie
142 */
143 static chunk_t cookie_build(private_receiver_t *this, message_t *message,
144 u_int32_t t, chunk_t secret)
145 {
146 u_int64_t spi = message->get_initiator_spi(message);
147 host_t *ip = message->get_source(message);
148 chunk_t input, hash = chunk_alloca(this->hasher->get_hash_size(this->hasher));
149
150 /* COOKIE = t | sha1( IPi | SPIi | t | secret ) */
151 input = chunk_cata("cccc", ip->get_address(ip), chunk_from_thing(spi),
152 chunk_from_thing(t), secret);
153 this->hasher->get_hash(this->hasher, input, hash.ptr);
154 return chunk_cat("cc", chunk_from_thing(t), hash);
155 }
156
157 /**
158 * verify a received cookie
159 */
160 static bool cookie_verify(private_receiver_t *this, message_t *message,
161 chunk_t cookie)
162 {
163 u_int32_t t, now;
164 chunk_t reference;
165 chunk_t secret;
166
167 now = time(NULL);
168 t = *(u_int32_t*)cookie.ptr;
169
170 if (cookie.len != COOKIE_LENGTH ||
171 t < now - this->secret_offset - COOKIE_LIFETIME)
172 {
173 DBG2(DBG_NET, "received cookie lifetime expired, rejecting");
174 return FALSE;
175 }
176
177 /* check if cookie is derived from old_secret */
178 if (t + this->secret_offset > this->secret_switch)
179 {
180 secret = chunk_from_thing(this->secret);
181 }
182 else
183 {
184 secret = chunk_from_thing(this->secret_old);
185 }
186
187 /* compare own calculation against received */
188 reference = cookie_build(this, message, t, secret);
189 if (chunk_equals(reference, cookie))
190 {
191 chunk_free(&reference);
192 return TRUE;
193 }
194 chunk_free(&reference);
195 return FALSE;
196 }
197
198 /**
199 * check if cookies are required, and if so, a valid cookie is included
200 */
201 static bool cookie_required(private_receiver_t *this, message_t *message)
202 {
203 bool failed = FALSE;
204
205 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
206 NULL) >= COOKIE_TRESHOLD)
207 {
208 /* check for a cookie. We don't use our parser here and do it
209 * quick and dirty for performance reasons.
210 * we assume to cookie is the first payload (which is a MUST), and
211 * the cookies SPI length is zero. */
212 packet_t *packet = message->get_packet(message);
213 chunk_t data = packet->get_data(packet);
214 if (data.len <
215 IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH + COOKIE_LENGTH ||
216 *(data.ptr + 16) != NOTIFY ||
217 *(u_int16_t*)(data.ptr + IKE_HEADER_LENGTH + 6) != htons(COOKIE))
218 {
219 /* no cookie found */
220 failed = TRUE;
221 }
222 else
223 {
224 data.ptr += IKE_HEADER_LENGTH + NOTIFY_PAYLOAD_HEADER_LENGTH;
225 data.len = COOKIE_LENGTH;
226 if (!cookie_verify(this, message, data))
227 {
228 DBG2(DBG_NET, "found cookie, but content invalid");
229 failed = TRUE;
230 }
231 }
232 packet->destroy(packet);
233 }
234 return failed;
235 }
236
237 /**
238 * check if peer has to many half open IKE_SAs
239 */
240 static bool peer_to_aggressive(private_receiver_t *this, message_t *message)
241 {
242 if (charon->ike_sa_manager->get_half_open_count(charon->ike_sa_manager,
243 message->get_source(message)) >= BLOCK_TRESHOLD)
244 {
245 return TRUE;
246 }
247 return FALSE;
248 }
249
250 /**
251 * Implementation of receiver_t.receive_packets.
252 */
253 static job_requeue_t receive_packets(private_receiver_t *this)
254 {
255 packet_t *packet;
256 message_t *message;
257 job_t *job;
258
259 /* read in a packet */
260 if (charon->socket->receive(charon->socket, &packet) != SUCCESS)
261 {
262 DBG2(DBG_NET, "receiving from socket failed!");
263 return JOB_REQUEUE_FAIR;
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 return JOB_REQUEUE_DIRECT;
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 return JOB_REQUEUE_DIRECT;
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 return JOB_REQUEUE_DIRECT;
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 return JOB_REQUEUE_DIRECT;
326 }
327 }
328 job = (job_t*)process_message_job_create(message);
329 charon->processor->queue_job(charon->processor, job);
330 return JOB_REQUEUE_DIRECT;
331 }
332
333 /**
334 * Implementation of receiver_t.destroy.
335 */
336 static void destroy(private_receiver_t *this)
337 {
338 this->job->cancel(this->job);
339 this->randomizer->destroy(this->randomizer);
340 this->hasher->destroy(this->hasher);
341 free(this);
342 }
343
344 /*
345 * Described in header.
346 */
347 receiver_t *receiver_create()
348 {
349 private_receiver_t *this = malloc_thing(private_receiver_t);
350 u_int32_t now = time(NULL);
351
352 this->public.destroy = (void(*)(receiver_t*)) destroy;
353
354 this->randomizer = randomizer_create();
355 this->hasher = hasher_create(HASH_SHA1);
356 this->secret_switch = now;
357 this->secret_offset = random() % now;
358 this->secret_used = 0;
359 this->randomizer->get_pseudo_random_bytes(this->randomizer, SECRET_LENGTH,
360 this->secret);
361 memcpy(this->secret_old, this->secret, SECRET_LENGTH);
362
363 this->job = callback_job_create((callback_job_cb_t)receive_packets,
364 this, NULL, NULL);
365 charon->processor->queue_job(charon->processor, (job_t*)this->job);
366
367 return &this->public;
368 }
369