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