testing: Start charon before Apache in tnc/tnccs-20-pdp-pt-tls
[strongswan.git] / src / libipsec / ipsec_processor.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ipsec.h"
17 #include "ipsec_processor.h"
18
19 #include <utils/debug.h>
20 #include <library.h>
21 #include <threading/rwlock.h>
22 #include <collections/blocking_queue.h>
23 #include <processing/jobs/callback_job.h>
24
25 typedef struct private_ipsec_processor_t private_ipsec_processor_t;
26
27 /**
28 * Private additions to ipsec_processor_t.
29 */
30 struct private_ipsec_processor_t {
31
32 /**
33 * Public members
34 */
35 ipsec_processor_t public;
36
37 /**
38 * Queue for inbound packets (esp_packet_t*)
39 */
40 blocking_queue_t *inbound_queue;
41
42 /**
43 * Queue for outbound packets (ip_packet_t*)
44 */
45 blocking_queue_t *outbound_queue;
46
47 /**
48 * Registered inbound callback
49 */
50 struct {
51 ipsec_inbound_cb_t cb;
52 void *data;
53 } inbound;
54
55 /**
56 * Registered outbound callback
57 */
58 struct {
59 ipsec_outbound_cb_t cb;
60 void *data;
61 } outbound;
62
63 /**
64 * Lock used to synchronize access to the callbacks
65 */
66 rwlock_t *lock;
67 };
68
69 /**
70 * Deliver an inbound IP packet to the registered listener
71 */
72 static void deliver_inbound(private_ipsec_processor_t *this,
73 esp_packet_t *packet)
74 {
75 this->lock->read_lock(this->lock);
76 if (this->inbound.cb)
77 {
78 this->inbound.cb(this->inbound.data, packet->extract_payload(packet));
79 }
80 else
81 {
82 DBG2(DBG_ESP, "no inbound callback registered, dropping packet");
83 }
84 packet->destroy(packet);
85 this->lock->unlock(this->lock);
86 }
87
88 /**
89 * Processes inbound packets
90 */
91 static job_requeue_t process_inbound(private_ipsec_processor_t *this)
92 {
93 esp_packet_t *packet;
94 ip_packet_t *ip_packet;
95 ipsec_sa_t *sa;
96 uint8_t next_header;
97 uint32_t spi, reqid;
98
99 packet = (esp_packet_t*)this->inbound_queue->dequeue(this->inbound_queue);
100
101 if (!packet->parse_header(packet, &spi))
102 {
103 packet->destroy(packet);
104 return JOB_REQUEUE_DIRECT;
105 }
106
107 sa = ipsec->sas->checkout_by_spi(ipsec->sas, spi,
108 packet->get_destination(packet));
109 if (!sa)
110 {
111 DBG2(DBG_ESP, "inbound ESP packet does not belong to an installed SA");
112 packet->destroy(packet);
113 return JOB_REQUEUE_DIRECT;
114 }
115
116 if (!sa->is_inbound(sa))
117 {
118 DBG1(DBG_ESP, "error: IPsec SA is not inbound");
119 packet->destroy(packet);
120 ipsec->sas->checkin(ipsec->sas, sa);
121 return JOB_REQUEUE_DIRECT;
122 }
123
124 if (packet->decrypt(packet, sa->get_esp_context(sa)) != SUCCESS)
125 {
126 ipsec->sas->checkin(ipsec->sas, sa);
127 packet->destroy(packet);
128 return JOB_REQUEUE_DIRECT;
129 }
130 ip_packet = packet->get_payload(packet);
131 sa->update_usestats(sa, ip_packet->get_encoding(ip_packet).len);
132 reqid = sa->get_reqid(sa);
133 ipsec->sas->checkin(ipsec->sas, sa);
134
135 next_header = packet->get_next_header(packet);
136 switch (next_header)
137 {
138 case IPPROTO_IPIP:
139 case IPPROTO_IPV6:
140 {
141 ipsec_policy_t *policy;
142
143 policy = ipsec->policies->find_by_packet(ipsec->policies,
144 ip_packet, TRUE, reqid);
145 if (policy)
146 {
147 deliver_inbound(this, packet);
148 policy->destroy(policy);
149 break;
150 }
151 DBG1(DBG_ESP, "discarding inbound IP packet %H == %H due to "
152 "policy", ip_packet->get_source(ip_packet),
153 ip_packet->get_destination(ip_packet));
154 /* no matching policy found, fall-through */
155 }
156 case IPPROTO_NONE:
157 /* discard dummy packets */
158 /* fall-through */
159 default:
160 packet->destroy(packet);
161 break;
162 }
163 return JOB_REQUEUE_DIRECT;
164 }
165
166 /**
167 * Send an ESP packet using the registered outbound callback
168 */
169 static void send_outbound(private_ipsec_processor_t *this,
170 esp_packet_t *packet)
171 {
172 this->lock->read_lock(this->lock);
173 if (this->outbound.cb)
174 {
175 this->outbound.cb(this->outbound.data, packet);
176 }
177 else
178 {
179 DBG2(DBG_ESP, "no outbound callback registered, dropping packet");
180 packet->destroy(packet);
181 }
182 this->lock->unlock(this->lock);
183 }
184
185 /**
186 * Processes outbound packets
187 */
188 static job_requeue_t process_outbound(private_ipsec_processor_t *this)
189 {
190 ipsec_policy_t *policy;
191 esp_packet_t *esp_packet;
192 ip_packet_t *packet;
193 ipsec_sa_t *sa;
194 host_t *src, *dst;
195
196 packet = (ip_packet_t*)this->outbound_queue->dequeue(this->outbound_queue);
197
198 policy = ipsec->policies->find_by_packet(ipsec->policies, packet, FALSE, 0);
199 if (!policy)
200 {
201 DBG2(DBG_ESP, "no matching outbound IPsec policy for %H == %H",
202 packet->get_source(packet), packet->get_destination(packet));
203 packet->destroy(packet);
204 return JOB_REQUEUE_DIRECT;
205 }
206
207 sa = ipsec->sas->checkout_by_reqid(ipsec->sas, policy->get_reqid(policy),
208 FALSE);
209 if (!sa)
210 { /* TODO-IPSEC: send an acquire to uppper layer */
211 DBG1(DBG_ESP, "could not find an outbound IPsec SA for reqid {%u}, "
212 "dropping packet", policy->get_reqid(policy));
213 packet->destroy(packet);
214 policy->destroy(policy);
215 return JOB_REQUEUE_DIRECT;
216 }
217 src = sa->get_source(sa);
218 dst = sa->get_destination(sa);
219 esp_packet = esp_packet_create_from_payload(src->clone(src),
220 dst->clone(dst), packet);
221 if (esp_packet->encrypt(esp_packet, sa->get_esp_context(sa),
222 sa->get_spi(sa)) != SUCCESS)
223 {
224 ipsec->sas->checkin(ipsec->sas, sa);
225 esp_packet->destroy(esp_packet);
226 policy->destroy(policy);
227 return JOB_REQUEUE_DIRECT;
228 }
229 sa->update_usestats(sa, packet->get_encoding(packet).len);
230 ipsec->sas->checkin(ipsec->sas, sa);
231 policy->destroy(policy);
232 send_outbound(this, esp_packet);
233 return JOB_REQUEUE_DIRECT;
234 }
235
236 METHOD(ipsec_processor_t, queue_inbound, void,
237 private_ipsec_processor_t *this, esp_packet_t *packet)
238 {
239 this->inbound_queue->enqueue(this->inbound_queue, packet);
240 }
241
242 METHOD(ipsec_processor_t, queue_outbound, void,
243 private_ipsec_processor_t *this, ip_packet_t *packet)
244 {
245 this->outbound_queue->enqueue(this->outbound_queue, packet);
246 }
247
248 METHOD(ipsec_processor_t, register_inbound, void,
249 private_ipsec_processor_t *this, ipsec_inbound_cb_t cb, void *data)
250 {
251 this->lock->write_lock(this->lock);
252 this->inbound.cb = cb;
253 this->inbound.data = data;
254 this->lock->unlock(this->lock);
255 }
256
257 METHOD(ipsec_processor_t, unregister_inbound, void,
258 private_ipsec_processor_t *this, ipsec_inbound_cb_t cb)
259 {
260 this->lock->write_lock(this->lock);
261 if (this->inbound.cb == cb)
262 {
263 this->inbound.cb = NULL;
264 }
265 this->lock->unlock(this->lock);
266 }
267
268 METHOD(ipsec_processor_t, register_outbound, void,
269 private_ipsec_processor_t *this, ipsec_outbound_cb_t cb, void *data)
270 {
271 this->lock->write_lock(this->lock);
272 this->outbound.cb = cb;
273 this->outbound.data = data;
274 this->lock->unlock(this->lock);
275 }
276
277 METHOD(ipsec_processor_t, unregister_outbound, void,
278 private_ipsec_processor_t *this, ipsec_outbound_cb_t cb)
279 {
280 this->lock->write_lock(this->lock);
281 if (this->outbound.cb == cb)
282 {
283 this->outbound.cb = NULL;
284 }
285 this->lock->unlock(this->lock);
286 }
287
288 METHOD(ipsec_processor_t, destroy, void,
289 private_ipsec_processor_t *this)
290 {
291 this->inbound_queue->destroy_offset(this->inbound_queue,
292 offsetof(esp_packet_t, destroy));
293 this->outbound_queue->destroy_offset(this->outbound_queue,
294 offsetof(ip_packet_t, destroy));
295 this->lock->destroy(this->lock);
296 free(this);
297 }
298
299 /**
300 * Described in header.
301 */
302 ipsec_processor_t *ipsec_processor_create()
303 {
304 private_ipsec_processor_t *this;
305
306 INIT(this,
307 .public = {
308 .queue_inbound = _queue_inbound,
309 .queue_outbound = _queue_outbound,
310 .register_inbound = _register_inbound,
311 .unregister_inbound = _unregister_inbound,
312 .register_outbound = _register_outbound,
313 .unregister_outbound = _unregister_outbound,
314 .destroy = _destroy,
315 },
316 .inbound_queue = blocking_queue_create(),
317 .outbound_queue = blocking_queue_create(),
318 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
319 );
320
321 lib->processor->queue_job(lib->processor,
322 (job_t*)callback_job_create((callback_job_cb_t)process_inbound, this,
323 NULL, (callback_job_cancel_t)return_false));
324 lib->processor->queue_job(lib->processor,
325 (job_t*)callback_job_create((callback_job_cb_t)process_outbound, this,
326 NULL, (callback_job_cancel_t)return_false));
327 return &this->public;
328 }