Fix parsing of IPv6 headers in ip_packet_t
[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 <debug.h>
20 #include <library.h>
21 #include <threading/rwlock.h>
22 #include <utils/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 ipsec_sa_t *sa;
95 u_int8_t next_header;
96 u_int32_t spi;
97
98 packet = (esp_packet_t*)this->inbound_queue->dequeue(this->inbound_queue);
99
100 if (!packet->parse_header(packet, &spi))
101 {
102 packet->destroy(packet);
103 return JOB_REQUEUE_DIRECT;
104 }
105
106 sa = ipsec->sas->checkout_by_spi(ipsec->sas, spi,
107 packet->get_destination(packet));
108 if (!sa)
109 {
110 DBG2(DBG_ESP, "inbound ESP packet does not belong to an installed SA");
111 packet->destroy(packet);
112 return JOB_REQUEUE_DIRECT;
113 }
114
115 if (!sa->is_inbound(sa))
116 {
117 DBG1(DBG_ESP, "error: IPsec SA is not inbound");
118 packet->destroy(packet);
119 ipsec->sas->checkin(ipsec->sas, sa);
120 return JOB_REQUEUE_DIRECT;
121 }
122
123 if (packet->decrypt(packet, sa->get_esp_context(sa)) != SUCCESS)
124 {
125 ipsec->sas->checkin(ipsec->sas, sa);
126 packet->destroy(packet);
127 return JOB_REQUEUE_DIRECT;
128 }
129 ipsec->sas->checkin(ipsec->sas, sa);
130
131 next_header = packet->get_next_header(packet);
132 switch (next_header)
133 {
134 case IPPROTO_IPIP:
135 case IPPROTO_IPV6:
136 {
137 ipsec_policy_t *policy;
138 ip_packet_t *ip_packet;
139
140 ip_packet = packet->get_payload(packet);
141 policy = ipsec->policies->find_by_packet(ipsec->policies,
142 ip_packet, TRUE);
143 if (policy)
144 { /* TODO-IPSEC: update policy/sa stats? */
145 deliver_inbound(this, packet);
146 policy->destroy(policy);
147 break;
148 }
149 DBG1(DBG_ESP, "discarding inbound IP packet due to policy");
150 /* no matching policy found, fall-through */
151 }
152 case IPPROTO_NONE:
153 /* discard dummy packets */
154 /* fall-through */
155 default:
156 packet->destroy(packet);
157 break;
158 }
159 return JOB_REQUEUE_DIRECT;
160 }
161
162 /**
163 * Send an ESP packet using the registered outbound callback
164 */
165 static void send_outbound(private_ipsec_processor_t *this,
166 esp_packet_t *packet)
167 {
168 this->lock->read_lock(this->lock);
169 if (this->outbound.cb)
170 {
171 this->outbound.cb(this->outbound.data, packet);
172 }
173 else
174 {
175 DBG2(DBG_ESP, "no outbound callback registered, dropping packet");
176 packet->destroy(packet);
177 }
178 this->lock->unlock(this->lock);
179 }
180
181 /**
182 * Processes outbound packets
183 */
184 static job_requeue_t process_outbound(private_ipsec_processor_t *this)
185 {
186 ipsec_policy_t *policy;
187 esp_packet_t *esp_packet;
188 ip_packet_t *packet;
189 ipsec_sa_t *sa;
190 host_t *src, *dst;
191
192 packet = (ip_packet_t*)this->outbound_queue->dequeue(this->outbound_queue);
193
194 policy = ipsec->policies->find_by_packet(ipsec->policies, packet, FALSE);
195 if (!policy)
196 {
197 DBG1(DBG_ESP, "no matching outbound IPsec policy for %H == %H",
198 packet->get_source(packet), packet->get_destination(packet));
199 packet->destroy(packet);
200 return JOB_REQUEUE_DIRECT;
201 }
202
203 sa = ipsec->sas->checkout_by_reqid(ipsec->sas, policy->get_reqid(policy),
204 FALSE);
205 if (!sa)
206 { /* TODO-IPSEC: send an acquire to uppper layer */
207 DBG1(DBG_ESP, "could not find an outbound IPsec SA for reqid {%u}, "
208 "dropping packet", policy->get_reqid(policy));
209 packet->destroy(packet);
210 policy->destroy(policy);
211 return JOB_REQUEUE_DIRECT;
212 }
213 src = sa->get_source(sa);
214 dst = sa->get_destination(sa);
215 esp_packet = esp_packet_create_from_payload(src->clone(src),
216 dst->clone(dst), packet);
217 if (esp_packet->encrypt(esp_packet, sa->get_esp_context(sa),
218 sa->get_spi(sa)) != SUCCESS)
219 {
220 ipsec->sas->checkin(ipsec->sas, sa);
221 esp_packet->destroy(esp_packet);
222 policy->destroy(policy);
223 return JOB_REQUEUE_DIRECT;
224 }
225 /* TODO-IPSEC: update policy/sa counters? */
226 ipsec->sas->checkin(ipsec->sas, sa);
227 policy->destroy(policy);
228 send_outbound(this, esp_packet);
229 return JOB_REQUEUE_DIRECT;
230 }
231
232 METHOD(ipsec_processor_t, queue_inbound, void,
233 private_ipsec_processor_t *this, esp_packet_t *packet)
234 {
235 this->inbound_queue->enqueue(this->inbound_queue, packet);
236 }
237
238 METHOD(ipsec_processor_t, queue_outbound, void,
239 private_ipsec_processor_t *this, ip_packet_t *packet)
240 {
241 this->outbound_queue->enqueue(this->outbound_queue, packet);
242 }
243
244 METHOD(ipsec_processor_t, register_inbound, void,
245 private_ipsec_processor_t *this, ipsec_inbound_cb_t cb, void *data)
246 {
247 this->lock->write_lock(this->lock);
248 this->inbound.cb = cb;
249 this->inbound.data = data;
250 this->lock->unlock(this->lock);
251 }
252
253 METHOD(ipsec_processor_t, unregister_inbound, void,
254 private_ipsec_processor_t *this, ipsec_inbound_cb_t cb)
255 {
256 this->lock->write_lock(this->lock);
257 if (this->inbound.cb == cb)
258 {
259 this->inbound.cb = NULL;
260 }
261 this->lock->unlock(this->lock);
262 }
263
264 METHOD(ipsec_processor_t, register_outbound, void,
265 private_ipsec_processor_t *this, ipsec_outbound_cb_t cb, void *data)
266 {
267 this->lock->write_lock(this->lock);
268 this->outbound.cb = cb;
269 this->outbound.data = data;
270 this->lock->unlock(this->lock);
271 }
272
273 METHOD(ipsec_processor_t, unregister_outbound, void,
274 private_ipsec_processor_t *this, ipsec_outbound_cb_t cb)
275 {
276 this->lock->write_lock(this->lock);
277 if (this->outbound.cb == cb)
278 {
279 this->outbound.cb = NULL;
280 }
281 this->lock->unlock(this->lock);
282 }
283
284 METHOD(ipsec_processor_t, destroy, void,
285 private_ipsec_processor_t *this)
286 {
287 this->inbound_queue->destroy_offset(this->inbound_queue,
288 offsetof(esp_packet_t, destroy));
289 this->outbound_queue->destroy_offset(this->outbound_queue,
290 offsetof(ip_packet_t, destroy));
291 this->lock->destroy(this->lock);
292 free(this);
293 }
294
295 /**
296 * Described in header.
297 */
298 ipsec_processor_t *ipsec_processor_create()
299 {
300 private_ipsec_processor_t *this;
301
302 INIT(this,
303 .public = {
304 .queue_inbound = _queue_inbound,
305 .queue_outbound = _queue_outbound,
306 .register_inbound = _register_inbound,
307 .unregister_inbound = _unregister_inbound,
308 .register_outbound = _register_outbound,
309 .unregister_outbound = _unregister_outbound,
310 .destroy = _destroy,
311 },
312 .inbound_queue = blocking_queue_create(),
313 .outbound_queue = blocking_queue_create(),
314 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
315 );
316
317 lib->processor->queue_job(lib->processor,
318 (job_t*)callback_job_create((callback_job_cb_t)process_inbound, this,
319 NULL, (callback_job_cancel_t)return_false));
320 lib->processor->queue_job(lib->processor,
321 (job_t*)callback_job_create((callback_job_cb_t)process_outbound, this,
322 NULL, (callback_job_cancel_t)return_false));
323 return &this->public;
324 }