- installing of child sa works
[strongswan.git] / Source / charon / threads / kernel_interface.c
1 /**
2 * @file kernel_interface.c
3 *
4 * @brief Implementation of kernel_interface_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 * Copyright (C) 2003 Herbert Xu.
12 *
13 * Contains modified parts from pluto.
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
24 */
25
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <linux/netlink.h>
29 #include <linux/xfrm.h>
30 #include <pthread.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #include "kernel_interface.h"
37
38 #include <daemon.h>
39 #include <utils/allocator.h>
40 #include <utils/linked_list.h>
41
42
43 #define KERNEL_ESP 50
44 #define KERNEL_AH 51
45
46
47 typedef struct netlink_message_t netlink_message_t;
48
49 /**
50 * Representation of ANY netlink message used
51 */
52 struct netlink_message_t {
53
54 /**
55 * header of the netlink message
56 */
57 struct nlmsghdr hdr;
58
59 union {
60 struct nlmsgerr e;
61 struct xfrm_userspi_info spi;
62 struct {
63 struct xfrm_usersa_info sa;
64 u_int8_t data[512];
65 };
66 };
67 };
68
69 typedef struct netlink_algo_t netlink_algo_t;
70
71 /**
72 * Add length and type to xfrm_algo
73 */
74 struct netlink_algo_t {
75 u_int16_t length;
76 u_int16_t type;
77 struct xfrm_algo algo;
78 };
79
80 typedef struct private_kernel_interface_t private_kernel_interface_t;
81
82 /**
83 * @brief Private Variables and Functions of kernel_interface class.
84 *
85 */
86 struct private_kernel_interface_t {
87 /**
88 * Public part of the kernel_interface_t object.
89 */
90 kernel_interface_t public;
91
92 /**
93 * Netlink communication socket.
94 */
95 int socket;
96
97 pid_t pid;
98 /**
99 * Sequence number for messages.
100 */
101 u_int32_t seq;
102
103 /**
104 * List of responded messages.
105 */
106 linked_list_t *responses;
107
108 /**
109 * Thread which receives messages.
110 */
111 pthread_t thread;
112
113 /**
114 * Mutex locks access to replies list.
115 */
116 pthread_mutex_t mutex;
117
118 /**
119 * Condvar allows signaling of threads waiting for a reply.
120 */
121 pthread_cond_t condvar;
122
123 /**
124 * Function for the thread, receives messages.
125 */
126 void (*receive_messages) (private_kernel_interface_t *this);
127
128 /**
129 * Sends a netlink_message_t down to the kernel and wait for reply.
130 */
131 status_t (*send_message) (private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response);
132 };
133
134 mapping_t kernel_encryption_algs_m[] = {
135 {ENCR_DES_IV64, ""},
136 {ENCR_DES, "des"},
137 {ENCR_3DES, "des3_ede"},
138 {ENCR_RC5, ""},
139 {ENCR_IDEA, "idea"},
140 {ENCR_CAST, "cast128"},
141 {ENCR_BLOWFISH, "blowfish"},
142 {ENCR_3IDEA, ""},
143 {ENCR_DES_IV32, ""},
144 {ENCR_NULL, ""},
145 {ENCR_AES_CBC, "aes"},
146 {ENCR_AES_CTR, ""},
147 {MAPPING_END, NULL}
148 };
149
150 mapping_t kernel_integrity_algs_m[] = {
151 {AUTH_HMAC_MD5_96, "md5"},
152 {AUTH_HMAC_SHA1_96, "sha1"},
153 {AUTH_DES_MAC, ""},
154 {AUTH_KPDK_MD5, ""},
155 {AUTH_AES_XCBC_96, ""},
156 {MAPPING_END, NULL}
157 };
158
159
160 static status_t get_spi(private_kernel_interface_t *this, host_t *src, host_t *dest, protocol_id_t protocol, bool tunnel_mode, u_int32_t *spi)
161 {
162 netlink_message_t request, *response;
163
164 memset(&request, 0, sizeof(request));
165 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.spi)));
166 request.hdr.nlmsg_flags = NLM_F_REQUEST;
167 request.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
168 request.spi.info.saddr = src->get_xfrm_addr(src);
169 request.spi.info.id.daddr = dest->get_xfrm_addr(dest);
170 request.spi.info.mode = tunnel_mode;
171 /* TODO: this should be done with getprotobyname() */
172 request.spi.info.id.proto = (protocol == ESP) ? KERNEL_ESP : KERNEL_AH;
173 request.spi.info.family = PF_INET;
174 request.spi.min = 100;
175 request.spi.max = 200;
176
177 if (this->send_message(this, &request, &response) != SUCCESS)
178 {
179 return FAILED;
180 }
181
182 if (response->hdr.nlmsg_type == NLMSG_ERROR)
183 {
184 return FAILED;
185 }
186
187 if (response->hdr.nlmsg_type != XFRM_MSG_NEWSA)
188 {
189 return FAILED;
190 }
191 else if (response->hdr.nlmsg_len < NLMSG_LENGTH(sizeof(response->sa)))
192 {
193 return FAILED;
194 }
195
196 *spi = response->sa.id.spi;
197 allocator_free(response);
198
199 return SUCCESS;
200 }
201
202 static status_t add_sa( private_kernel_interface_t *this,
203 host_t *me,
204 host_t *other,
205 u_int32_t spi,
206 int protocol,
207 bool tunnel_mode,
208 encryption_algorithm_t enc_alg,
209 chunk_t encryption_key,
210 integrity_algorithm_t int_alg,
211 chunk_t integrity_key,
212 bool replace)
213 {
214 netlink_message_t request, *response;
215 memset(&request, 0, sizeof(request));
216
217
218 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
219 request.hdr.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
220
221 request.sa.saddr = me->get_xfrm_addr(me);
222 request.sa.id.daddr = other->get_xfrm_addr(other);
223
224 request.sa.id.spi = spi;
225 /* TODO: this should be done with getprotobyname() */
226 request.sa.id.proto = (protocol == ESP) ? KERNEL_ESP : KERNEL_AH;
227 request.sa.family = me->get_family(me);
228 request.sa.mode = tunnel_mode;
229 request.sa.replay_window = 0; //sa->replay_window; ???
230 request.sa.reqid = 0; //sa->reqid; ???
231 request.sa.lft.soft_byte_limit = XFRM_INF;
232 request.sa.lft.soft_packet_limit = XFRM_INF;
233 request.sa.lft.hard_byte_limit = XFRM_INF;
234 request.sa.lft.hard_packet_limit = XFRM_INF;
235
236 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa)));
237
238 if (enc_alg != ENCR_UNDEFINED)
239 {
240 netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
241
242 nla->type = XFRMA_ALG_CRYPT;
243 nla->length = sizeof(netlink_algo_t) + encryption_key.len;
244 nla->algo.alg_key_len = encryption_key.len * 8;
245
246 strcpy(nla->algo.alg_name, mapping_find(kernel_encryption_algs_m, enc_alg));
247 memcpy(nla->algo.alg_key, encryption_key.ptr, encryption_key.len);
248
249 request.hdr.nlmsg_len += nla->length;
250 }
251
252 if (int_alg != AUTH_UNDEFINED)
253 {
254 netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
255
256 nla->type = XFRMA_ALG_AUTH;
257 nla->length = sizeof(netlink_algo_t) + integrity_key.len;
258 nla->algo.alg_key_len = integrity_key.len * 8;
259 strcpy(nla->algo.alg_name, mapping_find(kernel_integrity_algs_m, int_alg));
260 memcpy(nla->algo.alg_key, integrity_key.ptr, integrity_key.len);
261
262 request.hdr.nlmsg_len += nla->length;
263 }
264
265 /* add IPComp here*/
266
267 if (this->send_message(this, &request, &response) != SUCCESS)
268 {
269 allocator_free(response);
270 return FAILED;
271 }
272
273 allocator_free(response);
274 return SUCCESS;
275 }
276
277
278 static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response)
279 {
280 size_t length;
281 struct sockaddr_nl addr;
282
283 request->hdr.nlmsg_seq = ++this->seq;
284 request->hdr.nlmsg_pid = this->pid;
285
286 memset(&addr, 0, sizeof(struct sockaddr_nl));
287 addr.nl_family = AF_NETLINK;
288 addr.nl_pid = 0;
289 addr.nl_groups = 0;
290
291 length = sendto(this->socket,(void *)request, request->hdr.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr));
292
293 if (length < 0)
294 {
295 return FAILED;
296 }
297 else if (length != request->hdr.nlmsg_len)
298 {
299 return FAILED;
300 }
301
302 pthread_mutex_lock(&(this->mutex));
303
304 while (TRUE)
305 {
306 iterator_t *iterator;
307 bool found = FALSE;
308 /* search list, break if found */
309 iterator = this->responses->create_iterator(this->responses, TRUE);
310 while (iterator->has_next(iterator))
311 {
312 netlink_message_t *listed_response;
313 iterator->current(iterator, (void**)&listed_response);
314 if (listed_response->hdr.nlmsg_seq == request->hdr.nlmsg_seq)
315 {
316 /* matches our request, this is the reply */
317 *response = listed_response;
318 found = TRUE;
319 break;
320 }
321 }
322 iterator->destroy(iterator);
323
324 if (found)
325 {
326 break;
327 }
328 /* we should time out, if something goes wrong */
329 pthread_cond_wait(&(this->condvar), &(this->mutex));
330 }
331
332 pthread_mutex_unlock(&(this->mutex));
333
334 return SUCCESS;
335 }
336
337
338 static void receive_messages(private_kernel_interface_t *this)
339 {
340 while(TRUE)
341 {
342 netlink_message_t response, *listed_response;
343 while (TRUE)
344 {
345 struct sockaddr_nl addr;
346 socklen_t addr_length;
347 size_t length;
348
349 addr_length = sizeof(addr);
350
351 response.hdr.nlmsg_type = XFRM_MSG_NEWSA;
352 length = recvfrom(this->socket, &response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_length);
353 if (length < 0)
354 {
355 if (errno == EINTR)
356 {
357 /* interrupted, try again */
358 continue;
359 }
360 charon->kill(charon, "receiving from netlink socket failed");
361 }
362 if (!NLMSG_OK(&response.hdr, length))
363 {
364 /* bad netlink message */
365 continue;
366 }
367 if (addr.nl_pid != 0)
368 {
369 /* not from kernel. not interested, try another one */
370 continue;
371 }
372 break;
373 }
374
375 /* got a valid message.
376 * requests are handled on our own,
377 * responses are listed for the requesters
378 */
379 if (response.hdr.nlmsg_flags & NLM_F_REQUEST)
380 {
381 /* handle request */
382 }
383 else
384 {
385 /* add response to queue */
386 listed_response = allocator_alloc(sizeof(response));
387 memcpy(listed_response, &response, sizeof(response));
388
389 pthread_mutex_lock(&(this->mutex));
390 this->responses->insert_last(this->responses, (void*)listed_response);
391 pthread_mutex_unlock(&(this->mutex));
392 /* signal ALL waiting threads */
393 pthread_cond_broadcast(&(this->condvar));
394 }
395 /* get the next one */
396 }
397 }
398
399
400
401 /**
402 * Implementation of kernel_interface_t.destroy.
403 */
404 static void destroy(private_kernel_interface_t *this)
405 {
406 pthread_cancel(this->thread);
407 pthread_join(this->thread, NULL);
408 close(this->socket);
409 this->responses->destroy(this->responses);
410 allocator_free(this);
411 }
412
413 /*
414 * Described in header.
415 */
416 kernel_interface_t *kernel_interface_create()
417 {
418 private_kernel_interface_t *this = allocator_alloc_thing(private_kernel_interface_t);
419
420 /* public functions */
421 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,bool,u_int32_t*))get_spi;
422
423 this->public.add_sa = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,int,bool,encryption_algorithm_t,chunk_t,integrity_algorithm_t,chunk_t,bool))add_sa;
424
425
426 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
427
428 /* private members */
429 this->receive_messages = receive_messages;
430 this->send_message = send_message;
431 this->pid = getpid();
432 this->responses = linked_list_create();
433 pthread_mutex_init(&(this->mutex),NULL);
434 pthread_cond_init(&(this->condvar),NULL);
435 this->seq = 0;
436 this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
437 if (this->socket <= 0)
438 {
439 allocator_free(this);
440 charon->kill(charon, "Unable to create netlink socket");
441 }
442
443 if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
444 {
445 close(this->socket);
446 allocator_free(this);
447 charon->kill(charon, "Unable to create netlink thread");
448 }
449
450 charon->logger_manager->enable_logger_level(charon->logger_manager, TESTER, FULL);
451 return (&this->public);
452 }