(no commit message)
[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 chunk_t encryption_key,
205 integrity_algorithm_t int_alg,
206 chunk_t integrity_key,
207 bool replace)
208 {
209 netlink_message_t request, *response;
210 POS;
211 memset(&request, 0, sizeof(request));
212
213 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
214 request.hdr.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
215
216 request.sa.saddr = me->get_xfrm_addr(me);
217 request.sa.id.daddr = other->get_xfrm_addr(other);
218
219 request.sa.id.spi = spi;
220 request.sa.id.proto = protocol;
221 request.sa.family = me->get_family(me);
222 request.sa.mode = tunnel_mode;
223 request.sa.replay_window = 0; //sa->replay_window; ???
224 request.sa.reqid = 0; //sa->reqid; ???
225 request.sa.lft.soft_byte_limit = XFRM_INF;
226 request.sa.lft.soft_packet_limit = XFRM_INF;
227 request.sa.lft.hard_byte_limit = XFRM_INF;
228 request.sa.lft.hard_packet_limit = XFRM_INF;
229
230 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa)));
231
232 if (enc_alg != ENCR_UNDEFINED)
233 {
234 netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
235
236 nla->type = XFRMA_ALG_CRYPT;
237 nla->length = sizeof(netlink_algo_t) + encryption_key.len;
238 nla->algo.alg_key_len = encryption_key.len * 8;
239
240 strcpy(nla->algo.alg_name, mapping_find(kernel_encryption_algs_m, enc_alg));
241 memcpy(nla->algo.alg_key, encryption_key.ptr, encryption_key.len);
242
243 request.hdr.nlmsg_len += nla->length;
244 }
245
246 if (int_alg != AUTH_UNDEFINED)
247 {
248 netlink_algo_t *nla = (netlink_algo_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
249
250 nla->type = XFRMA_ALG_AUTH;
251 nla->length = sizeof(netlink_algo_t) + integrity_key.len;
252 nla->algo.alg_key_len = integrity_key.len * 8;
253 strcpy(nla->algo.alg_name, mapping_find(kernel_integrity_algs_m, int_alg));
254 memcpy(nla->algo.alg_key, integrity_key.ptr, integrity_key.len);
255
256 request.hdr.nlmsg_len += nla->length;
257 }
258
259 /* add IPComp */
260
261 if (this->send_message(this, &request, &response) != SUCCESS)
262 {
263 allocator_free(response);
264 return FAILED;
265 }
266
267 allocator_free(response);
268 return SUCCESS;
269 }
270
271
272 static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response)
273 {
274 size_t length;
275 struct sockaddr_nl addr;
276
277 request->hdr.nlmsg_seq = ++this->seq;
278 request->hdr.nlmsg_pid = this->pid;
279
280 memset(&addr, 0, sizeof(struct sockaddr_nl));
281 addr.nl_family = AF_NETLINK;
282 addr.nl_pid = 0;
283 addr.nl_groups = 0;
284
285 length = sendto(this->socket,(void *)request, request->hdr.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr));
286
287 if (length < 0)
288 {
289 return FAILED;
290 }
291 else if (length != request->hdr.nlmsg_len)
292 {
293 return FAILED;
294 }
295
296 pthread_mutex_lock(&(this->mutex));
297
298 while (TRUE)
299 {
300 iterator_t *iterator;
301 bool found = FALSE;
302 /* search list, break if found */
303 iterator = this->responses->create_iterator(this->responses, TRUE);
304 while (iterator->has_next(iterator))
305 {
306 netlink_message_t *listed_response;
307 iterator->current(iterator, (void**)&listed_response);
308 if (listed_response->hdr.nlmsg_seq == request->hdr.nlmsg_seq)
309 {
310 /* matches our request, this is the reply */
311 *response = listed_response;
312 found = TRUE;
313 break;
314 }
315 }
316 iterator->destroy(iterator);
317
318 if (found)
319 {
320 break;
321 }
322 /* we should time out, if something goes wrong */
323 pthread_cond_wait(&(this->condvar), &(this->mutex));
324 }
325
326 pthread_mutex_unlock(&(this->mutex));
327
328 return SUCCESS;
329 }
330
331
332 static void receive_messages(private_kernel_interface_t *this)
333 {
334 while(TRUE)
335 {
336 netlink_message_t response, *listed_response;
337 while (TRUE)
338 {
339 struct sockaddr_nl addr;
340 socklen_t addr_length;
341 size_t length;
342
343 addr_length = sizeof(addr);
344
345 response.hdr.nlmsg_type = XFRM_MSG_NEWSA;
346 length = recvfrom(this->socket, &response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_length);
347 if (length < 0)
348 {
349 if (errno == EINTR)
350 {
351 /* interrupted, try again */
352 continue;
353 }
354 charon->kill(charon, "receiving from netlink socket failed");
355 }
356 if (!NLMSG_OK(&response.hdr, length))
357 {
358 /* bad netlink message */
359 continue;
360 }
361 if (addr.nl_pid != 0)
362 {
363 /* not from kernel. not interested, try another one */
364 continue;
365 }
366 break;
367 }
368
369 /* got a valid message.
370 * requests are handled on our own,
371 * responses are listed for the requesters
372 */
373 if (response.hdr.nlmsg_flags & NLM_F_REQUEST)
374 {
375 /* handle request */
376 }
377 else
378 {
379 /* add response to queue */
380 listed_response = allocator_alloc(sizeof(response));
381 memcpy(listed_response, &response, sizeof(response));
382
383 pthread_mutex_lock(&(this->mutex));
384 this->responses->insert_last(this->responses, (void*)listed_response);
385 pthread_mutex_unlock(&(this->mutex));
386 /* signal ALL waiting threads */
387 pthread_cond_broadcast(&(this->condvar));
388 }
389 /* get the next one */
390 }
391 }
392
393
394
395 /**
396 * Implementation of kernel_interface_t.destroy.
397 */
398 static void destroy(private_kernel_interface_t *this)
399 {
400 pthread_cancel(this->thread);
401 pthread_join(this->thread, NULL);
402 close(this->socket);
403 this->responses->destroy(this->responses);
404 allocator_free(this);
405 }
406
407 /*
408 * Described in header.
409 */
410 kernel_interface_t *kernel_interface_create()
411 {
412 private_kernel_interface_t *this = allocator_alloc_thing(private_kernel_interface_t);
413
414 /* public functions */
415 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,bool,u_int32_t*))get_spi;
416
417 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;
418
419
420 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
421
422 /* private members */
423 this->receive_messages = receive_messages;
424 this->send_message = send_message;
425 this->pid = getpid();
426 this->responses = linked_list_create();
427 pthread_mutex_init(&(this->mutex),NULL);
428 pthread_cond_init(&(this->condvar),NULL);
429 this->seq = 0;
430 this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
431 if (this->socket <= 0)
432 {
433 allocator_free(this);
434 charon->kill(charon, "Unable to create netlink socket");
435 }
436
437 if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
438 {
439 close(this->socket);
440 allocator_free(this);
441 charon->kill(charon, "Unable to create netlink thread");
442 }
443
444 charon->logger_manager->enable_logger_level(charon->logger_manager, TESTER, FULL);
445 return (&this->public);
446 }