2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
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>.
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
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>
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
44 typedef struct private_receiver_t private_receiver_t
;
47 * Private data of a receiver_t object.
49 struct private_receiver_t
{
51 * Public part of a receiver_t object.
56 * Threads job receiving packets
63 pthread_t assigned_thread
;
66 * current secret to use for cookie calculation
68 char secret
[SECRET_LENGTH
];
71 * previous secret used to verify older cookies
73 char secret_old
[SECRET_LENGTH
];
76 * how many times we have used "secret" so far
78 u_int32_t secret_used
;
81 * time we did the cookie switch
83 u_int32_t secret_switch
;
86 * time offset to use, hides our system time
88 u_int32_t secret_offset
;
91 * the randomizer to use for secret generation
93 randomizer_t
*randomizer
;
96 * hasher to use for cookie calculation
102 * send a notify back to the sender
104 static void send_notify(message_t
*request
, notify_type_t type
, chunk_t data
)
106 if (request
->get_request(request
) &&
107 request
->get_exchange_type(request
) == IKE_SA_INIT
)
112 ike_sa_id_t
*ike_sa_id
;
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
)
128 charon
->sender
->send(charon
->sender
, packet
);
129 response
->destroy(response
);
137 static chunk_t
cookie_build(private_receiver_t
*this, message_t
*message
,
138 u_int32_t t
, chunk_t secret
)
140 u_int64_t spi
= message
->get_initiator_spi(message
);
141 host_t
*ip
= message
->get_source(message
);
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
);
153 * verify a received cookie
155 static bool cookie_verify(private_receiver_t
*this, message_t
*message
,
163 t
= *(u_int32_t
*)cookie
.ptr
;
165 if (cookie
.len
!= sizeof(u_int32_t
) +
166 this->hasher
->get_hash_size(this->hasher
) ||
167 t
< now
- this->secret_offset
- COOKIE_LIFETIME
)
169 DBG2(DBG_NET
, "received cookie lifetime expired, rejecting");
173 /* check if cookie is derived from old_secret */
174 if (t
+ this->secret_offset
> this->secret_switch
)
176 secret
= chunk_from_thing(this->secret
);
180 secret
= chunk_from_thing(this->secret_old
);
183 /* compare own calculation against received */
184 reference
= cookie_build(this, message
, t
, secret
);
185 if (chunk_equals(reference
, cookie
))
187 chunk_free(&reference
);
190 chunk_free(&reference
);
195 * check if cookies are required, and if so, a valid cookie is included
197 static bool cookie_required(private_receiver_t
*this, message_t
*message
)
201 if (charon
->ike_sa_manager
->get_half_open_count(charon
->ike_sa_manager
,
202 NULL
) >= COOKIE_TRESHOLD
)
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
);
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
))
216 /* no cookie found */
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
))
225 DBG2(DBG_NET
, "found cookie, but content invalid");
229 packet
->destroy(packet
);
235 * check if peer has to many half open IKE_SAs
237 static bool peer_to_aggressive(private_receiver_t
*this, message_t
*message
)
239 if (charon
->ike_sa_manager
->get_half_open_count(charon
->ike_sa_manager
,
240 message
->get_source(message
)) >= BLOCK_TRESHOLD
)
248 * Implementation of receiver_t.receive_packets.
250 static job_requeue_t
receive_packets(private_receiver_t
*this)
256 /* read in a packet */
257 if (charon
->socket
->receive(charon
->socket
, &packet
) != SUCCESS
)
259 DBG2(DBG_NET
, "receiving from socket failed!");
260 return JOB_REQUEUE_FAIR
;
263 /* parse message header */
264 message
= message_create_from_packet(packet
);
265 if (message
->parse_header(message
) != SUCCESS
)
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
;
273 /* check IKE major version */
274 if (message
->get_major_version(message
) != IKE_MAJOR_VERSION
)
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
;
284 if (message
->get_request(message
) &&
285 message
->get_exchange_type(message
) == IKE_SA_INIT
)
287 /* check for cookies */
288 if (cookie_required(this, message
))
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
));
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
);
301 if (++this->secret_used
> COOKIE_REUSE
)
303 /* create new cookie */
304 DBG1(DBG_NET
, "generating new cookie secret after %d uses",
306 memcpy(this->secret_old
, this->secret
, SECRET_LENGTH
);
307 this->randomizer
->get_pseudo_random_bytes(this->randomizer
,
308 SECRET_LENGTH
, this->secret
);
309 this->secret_switch
= now
;
310 this->secret_used
= 0;
312 message
->destroy(message
);
313 return JOB_REQUEUE_DIRECT
;
316 /* check if peer has not too many IKE_SAs half open */
317 if (peer_to_aggressive(this, message
))
319 DBG1(DBG_NET
, "ignoring IKE_SA setup from %H, "
320 "peer to aggressive", message
->get_source(message
));
321 message
->destroy(message
);
322 return JOB_REQUEUE_DIRECT
;
325 job
= (job_t
*)process_message_job_create(message
);
326 charon
->processor
->queue_job(charon
->processor
, job
);
327 return JOB_REQUEUE_DIRECT
;
331 * Implementation of receiver_t.destroy.
333 static void destroy(private_receiver_t
*this)
335 this->job
->cancel(this->job
);
336 this->randomizer
->destroy(this->randomizer
);
337 this->hasher
->destroy(this->hasher
);
342 * Described in header.
344 receiver_t
*receiver_create()
346 private_receiver_t
*this = malloc_thing(private_receiver_t
);
347 u_int32_t now
= time(NULL
);
349 this->public.destroy
= (void(*)(receiver_t
*)) destroy
;
351 this->hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_PREFERRED
);
352 if (this->hasher
== NULL
)
354 DBG1(DBG_NET
, "creating cookie hasher failed, no hashers supported");
358 this->randomizer
= randomizer_create();
359 this->secret_switch
= now
;
360 this->secret_offset
= random() % now
;
361 this->secret_used
= 0;
362 this->randomizer
->get_pseudo_random_bytes(this->randomizer
, SECRET_LENGTH
,
364 memcpy(this->secret_old
, this->secret
, SECRET_LENGTH
);
366 this->job
= callback_job_create((callback_job_cb_t
)receive_packets
,
368 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
370 return &this->public;