- split up in libstrong, charon, stroke, testing done
[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 <pthread.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "kernel_interface.h"
36
37 #include <daemon.h>
38 #include <utils/linked_list.h>
39
40
41 #define KERNEL_ESP 50
42 #define KERNEL_AH 51
43
44 #define SPD_PRIORITY 1024
45
46 #define XFRM_DATA_LENGTH 512
47
48
49 typedef struct xfrm_data_t xfrm_data_t;
50
51 /**
52 * Lenght/Type/data struct for userdata in xfrm
53 * We dont use the "I-don't-know-where-they-come-from"-structs
54 * used in the kernel.
55 */
56 struct xfrm_data_t {
57 /**
58 * length of the data
59 */
60 u_int16_t length;
61
62 /**
63 * type of data
64 */
65 u_int16_t type;
66
67 /**
68 * and the data itself, for different purposes
69 */
70 union {
71 /** algorithm */
72 struct xfrm_algo algo;
73 /** policy tmpl */
74 struct xfrm_user_tmpl tmpl[2];
75 };
76 };
77
78
79 typedef struct netlink_message_t netlink_message_t;
80
81 /**
82 * Representation of ANY netlink message used
83 */
84 struct netlink_message_t {
85
86 /**
87 * header of the netlink message
88 */
89 struct nlmsghdr hdr;
90
91 union {
92 /** error message */
93 struct nlmsgerr e;
94 /** message for spi allocation */
95 struct xfrm_userspi_info spi;
96 /** message for SA manipulation */
97 struct xfrm_usersa_id sa_id;
98 /** message for SA installation */
99 struct xfrm_usersa_info sa;
100 /** message for policy manipulation */
101 struct xfrm_userpolicy_id policy_id;
102 /** message for policy installation */
103 struct xfrm_userpolicy_info policy;
104 };
105 u_int8_t data[XFRM_DATA_LENGTH];
106 };
107
108
109 typedef struct private_kernel_interface_t private_kernel_interface_t;
110
111 /**
112 * @brief Private Variables and Functions of kernel_interface class.
113 *
114 */
115 struct private_kernel_interface_t {
116 /**
117 * Public part of the kernel_interface_t object.
118 */
119 kernel_interface_t public;
120
121 /**
122 * Netlink communication socket.
123 */
124 int socket;
125
126 pid_t pid;
127 /**
128 * Sequence number for messages.
129 */
130 u_int32_t seq;
131
132 /**
133 * List of responded messages.
134 */
135 linked_list_t *responses;
136
137 /**
138 * Thread which receives messages.
139 */
140 pthread_t thread;
141
142 /**
143 * Mutex locks access to replies list.
144 */
145 pthread_mutex_t mutex;
146
147 /**
148 * Condvar allows signaling of threads waiting for a reply.
149 */
150 pthread_cond_t condvar;
151
152 /**
153 * Function for the thread, receives messages.
154 */
155 void (*receive_messages) (private_kernel_interface_t *this);
156
157 /**
158 * Sends a netlink_message_t down to the kernel and wait for reply.
159 */
160 status_t (*send_message) (private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response);
161 };
162
163 /**
164 * In the kernel, algorithms are identified as strings, we use our
165 * mapping functions...
166 * Algorithms for encryption.
167 * TODO: Add missing algorithm strings
168 */
169 mapping_t kernel_encryption_algs_m[] = {
170 {ENCR_DES_IV64, ""},
171 {ENCR_DES, "des"},
172 {ENCR_3DES, "des3_ede"},
173 {ENCR_RC5, ""},
174 {ENCR_IDEA, "idea"},
175 {ENCR_CAST, "cast128"},
176 {ENCR_BLOWFISH, "blowfish"},
177 {ENCR_3IDEA, ""},
178 {ENCR_DES_IV32, ""},
179 {ENCR_NULL, ""},
180 {ENCR_AES_CBC, "aes"},
181 {ENCR_AES_CTR, ""},
182 {MAPPING_END, NULL}
183 };
184 /**
185 * In the kernel, algorithms are identified as strings, we use our
186 * mapping functions...
187 * Algorithms for integrity protection.
188 * TODO: Add missing algorithm strings
189 */
190 mapping_t kernel_integrity_algs_m[] = {
191 {AUTH_HMAC_MD5_96, "md5"},
192 {AUTH_HMAC_SHA1_96, "sha1"},
193 {AUTH_DES_MAC, ""},
194 {AUTH_KPDK_MD5, ""},
195 {AUTH_AES_XCBC_96, ""},
196 {MAPPING_END, NULL}
197 };
198
199 /**
200 * Implementation of kernel_interface_t.get_spi.
201 */
202 static status_t get_spi(private_kernel_interface_t *this,
203 host_t *src, host_t *dest,
204 protocol_id_t protocol, u_int32_t reqid,
205 u_int32_t *spi)
206 {
207 netlink_message_t request, *response;
208 status_t status = SUCCESS;
209
210 memset(&request, 0, sizeof(request));
211 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.spi)));
212 request.hdr.nlmsg_flags = NLM_F_REQUEST;
213 request.hdr.nlmsg_type = XFRM_MSG_ALLOCSPI;
214 request.spi.info.saddr = src->get_xfrm_addr(src);
215 request.spi.info.id.daddr = dest->get_xfrm_addr(dest);
216 request.spi.info.mode = TRUE; /* tunnel mode */
217 request.spi.info.reqid = reqid;
218 request.spi.info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
219 request.spi.info.family = PF_INET;
220 request.spi.min = 0xc0000000;
221 request.spi.max = 0xcFFFFFFF;
222
223 if (this->send_message(this, &request, &response) != SUCCESS)
224 {
225 status = FAILED;
226 }
227 else if (response->hdr.nlmsg_type == NLMSG_ERROR)
228 {
229 status = FAILED;
230 }
231 else if (response->hdr.nlmsg_type != XFRM_MSG_NEWSA)
232 {
233 status = FAILED;
234 }
235 else if (response->hdr.nlmsg_len < NLMSG_LENGTH(sizeof(response->sa)))
236 {
237 status = FAILED;
238 }
239
240 *spi = response->sa.id.spi;
241 free(response);
242
243 return status;
244 }
245
246 /**
247 * Implementation of kernel_interface_t.add_sa.
248 */
249 static status_t add_sa( private_kernel_interface_t *this,
250 host_t *me,
251 host_t *other,
252 u_int32_t spi,
253 int protocol,
254 u_int32_t reqid,
255 encryption_algorithm_t enc_alg,
256 chunk_t encryption_key,
257 integrity_algorithm_t int_alg,
258 chunk_t integrity_key,
259 bool replace)
260 {
261 netlink_message_t request, *response;
262 memset(&request, 0, sizeof(request));
263 status_t status;
264
265 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
266 request.hdr.nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
267
268 request.sa.saddr = me->get_xfrm_addr(me);
269 request.sa.id.daddr = other->get_xfrm_addr(other);
270
271 request.sa.id.spi = spi;
272 request.sa.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
273 request.sa.family = me->get_family(me);
274 request.sa.mode = TRUE; /* tunnel mode */
275 request.sa.replay_window = 32;
276 request.sa.reqid = reqid;
277 request.sa.lft.soft_byte_limit = XFRM_INF;
278 request.sa.lft.soft_packet_limit = XFRM_INF;
279 request.sa.lft.hard_byte_limit = XFRM_INF;
280 request.sa.lft.hard_packet_limit = XFRM_INF;
281
282 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa)));
283
284 if (enc_alg != ENCR_UNDEFINED)
285 {
286 xfrm_data_t *data = (xfrm_data_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
287
288 data->type = XFRMA_ALG_CRYPT;
289 data->length = 4 + sizeof(data->algo) + encryption_key.len;
290 data->algo.alg_key_len = encryption_key.len * 8;
291 request.hdr.nlmsg_len += data->length;
292 if (request.hdr.nlmsg_len > sizeof(request))
293 {
294 return FAILED;
295 }
296 strcpy(data->algo.alg_name, mapping_find(kernel_encryption_algs_m, enc_alg));
297 memcpy(data->algo.alg_key, encryption_key.ptr, encryption_key.len);
298 }
299
300 if (int_alg != AUTH_UNDEFINED)
301 {
302 xfrm_data_t *data = (xfrm_data_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
303
304 data->type = XFRMA_ALG_AUTH;
305 data->length = 4 + sizeof(data->algo) + integrity_key.len;
306 data->algo.alg_key_len = integrity_key.len * 8;
307 request.hdr.nlmsg_len += data->length;
308 if (request.hdr.nlmsg_len > sizeof(request))
309 {
310 return FAILED;
311 }
312 strcpy(data->algo.alg_name, mapping_find(kernel_integrity_algs_m, int_alg));
313 memcpy(data->algo.alg_key, integrity_key.ptr, integrity_key.len);
314 }
315
316 /* TODO: add IPComp here*/
317
318 if (this->send_message(this, &request, &response) != SUCCESS)
319 {
320 status = FAILED;
321 }
322 else if (response->hdr.nlmsg_type != NLMSG_ERROR)
323 {
324 status = FAILED;
325 }
326 else if (response->e.error)
327 {
328 status = FAILED;
329 }
330
331 free(response);
332 return SUCCESS;
333 }
334
335 static status_t del_sa( private_kernel_interface_t *this,
336 host_t *dst,
337 u_int32_t spi,
338 protocol_id_t protocol)
339 {
340 netlink_message_t request, *response;
341 memset(&request, 0, sizeof(request));
342 status_t status;
343
344 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
345 request.hdr.nlmsg_type = XFRM_MSG_DELSA;
346
347 request.sa_id.daddr = dst->get_xfrm_addr(dst);
348
349 request.sa_id.spi = spi;
350 request.sa_id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
351 request.sa_id.family = dst->get_family(dst);
352
353 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.sa_id)));
354
355 if (this->send_message(this, &request, &response) != SUCCESS)
356 {
357 status = FAILED;
358 }
359 else if (response->hdr.nlmsg_type != NLMSG_ERROR)
360 {
361 status = FAILED;
362 }
363 else if (response->e.error)
364 {
365 status = FAILED;
366 }
367
368 free(response);
369 return SUCCESS;
370 }
371
372 /**
373 * Implementation of kernel_interface_t.add_policy.
374 */
375 static status_t add_policy(private_kernel_interface_t *this,
376 host_t *me, host_t *other,
377 host_t *src, host_t *dst,
378 u_int8_t src_hostbits, u_int8_t dst_hostbits,
379 int direction, int upper_proto,
380 bool ah, bool esp,
381 u_int32_t reqid)
382 {
383 netlink_message_t request, *response;
384 status_t status = SUCCESS;
385
386 memset(&request, 0, sizeof(request));
387 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
388
389 request.policy.sel.sport = htons(src->get_port(src));
390 request.policy.sel.dport = htons(dst->get_port(dst));
391 request.policy.sel.sport_mask = (request.policy.sel.sport) ? ~0 : 0;
392 request.policy.sel.dport_mask = (request.policy.sel.dport) ? ~0 : 0;
393 request.policy.sel.saddr = src->get_xfrm_addr(src);
394 request.policy.sel.daddr = dst->get_xfrm_addr(dst);
395 request.policy.sel.prefixlen_s = src_hostbits;
396 request.policy.sel.prefixlen_d = dst_hostbits;
397 request.policy.sel.proto = upper_proto;
398 request.policy.sel.family = src->get_family(src);
399
400 request.hdr.nlmsg_type = XFRM_MSG_NEWPOLICY;
401 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.policy)));
402
403 request.policy.dir = direction;
404 request.policy.priority = SPD_PRIORITY;
405 request.policy.action = XFRM_POLICY_ALLOW;
406 request.policy.share = XFRM_SHARE_ANY;
407
408 request.policy.lft.soft_byte_limit = XFRM_INF;
409 request.policy.lft.soft_packet_limit = XFRM_INF;
410 request.policy.lft.hard_byte_limit = XFRM_INF;
411 request.policy.lft.hard_packet_limit = XFRM_INF;
412
413 if (esp || ah)
414 {
415 xfrm_data_t *data;
416 int tmpl_pos = 0;
417 data = (xfrm_data_t*)(((u_int8_t*)&request) + request.hdr.nlmsg_len);
418 data->type = XFRMA_TMPL;
419 if (esp)
420 {
421 data->tmpl[tmpl_pos].reqid = reqid;
422 data->tmpl[tmpl_pos].id.proto = KERNEL_ESP;
423 data->tmpl[tmpl_pos].aalgos = data->tmpl[tmpl_pos].ealgos = data->tmpl[tmpl_pos].calgos = ~0;
424 data->tmpl[tmpl_pos].mode = TRUE;
425
426 data->tmpl[tmpl_pos].saddr = me->get_xfrm_addr(me);
427 data->tmpl[tmpl_pos].id.daddr = me->get_xfrm_addr(other);
428
429 tmpl_pos++;
430 }
431 if (ah)
432 {
433 data->tmpl[tmpl_pos].reqid = reqid;
434 data->tmpl[tmpl_pos].id.proto = KERNEL_AH;
435 data->tmpl[tmpl_pos].aalgos = data->tmpl[tmpl_pos].ealgos = data->tmpl[tmpl_pos].calgos = ~0;
436 data->tmpl[tmpl_pos].mode = TRUE;
437
438 data->tmpl[tmpl_pos].saddr = me->get_xfrm_addr(me);
439 data->tmpl[tmpl_pos].id.daddr = other->get_xfrm_addr(other);
440
441 tmpl_pos++;
442 }
443 data->length = 4 + sizeof(struct xfrm_user_tmpl) * tmpl_pos;
444 request.hdr.nlmsg_len += data->length;
445 }
446
447 if (this->send_message(this, &request, &response) != SUCCESS)
448 {
449 status = FAILED;
450 }
451 else if (response->hdr.nlmsg_type != NLMSG_ERROR)
452 {
453 status = FAILED;
454 }
455 else if (response->e.error)
456 {
457 status = FAILED;
458 }
459
460 free(response);
461 return status;
462 }
463
464 /**
465 * Implementation of kernel_interface_t.del_policy.
466 */
467 static status_t del_policy(private_kernel_interface_t *this,
468 host_t *me, host_t *other,
469 host_t *src, host_t *dst,
470 u_int8_t src_hostbits, u_int8_t dst_hostbits,
471 int direction, int upper_proto)
472 {
473 netlink_message_t request, *response;
474 status_t status = SUCCESS;
475
476 memset(&request, 0, sizeof(request));
477 request.hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
478
479 request.policy_id.sel.sport = htons(src->get_port(src));
480 request.policy_id.sel.dport = htons(dst->get_port(dst));
481 request.policy_id.sel.sport_mask = (request.policy.sel.sport) ? ~0 : 0;
482 request.policy_id.sel.dport_mask = (request.policy.sel.dport) ? ~0 : 0;
483 request.policy_id.sel.saddr = src->get_xfrm_addr(src);
484 request.policy_id.sel.daddr = dst->get_xfrm_addr(dst);
485 request.policy_id.sel.prefixlen_s = src_hostbits;
486 request.policy_id.sel.prefixlen_d = dst_hostbits;
487 request.policy_id.sel.proto = upper_proto;
488 request.policy_id.sel.family = src->get_family(src);
489
490 request.policy_id.dir = direction;
491
492 request.hdr.nlmsg_type = XFRM_MSG_DELPOLICY;
493 request.hdr.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request.policy_id)));
494
495 if (this->send_message(this, &request, &response) != SUCCESS)
496 {
497 status = FAILED;
498 }
499 else if (response->hdr.nlmsg_type != NLMSG_ERROR)
500 {
501 status = FAILED;
502 }
503 else if (response->e.error)
504 {
505 status = FAILED;
506 }
507
508 free(response);
509 return status;
510 }
511
512 /**
513 * Implementation of private_kernel_interface_t.send_message.
514 */
515 static status_t send_message(private_kernel_interface_t *this, netlink_message_t *request, netlink_message_t **response)
516 {
517 size_t length;
518 struct sockaddr_nl addr;
519
520 request->hdr.nlmsg_seq = ++this->seq;
521 request->hdr.nlmsg_pid = this->pid;
522
523 memset(&addr, 0, sizeof(struct sockaddr_nl));
524 addr.nl_family = AF_NETLINK;
525 addr.nl_pid = 0;
526 addr.nl_groups = 0;
527
528 length = sendto(this->socket,(void *)request, request->hdr.nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr));
529
530 if (length < 0)
531 {
532 return FAILED;
533 }
534 else if (length != request->hdr.nlmsg_len)
535 {
536 return FAILED;
537 }
538
539 pthread_mutex_lock(&(this->mutex));
540
541 while (TRUE)
542 {
543 iterator_t *iterator;
544 bool found = FALSE;
545 /* search list, break if found */
546 iterator = this->responses->create_iterator(this->responses, TRUE);
547 while (iterator->has_next(iterator))
548 {
549 netlink_message_t *listed_response;
550 iterator->current(iterator, (void**)&listed_response);
551 if (listed_response->hdr.nlmsg_seq == request->hdr.nlmsg_seq)
552 {
553 /* matches our request, this is the reply */
554 *response = listed_response;
555 found = TRUE;
556 break;
557 }
558 }
559 iterator->destroy(iterator);
560
561 if (found)
562 {
563 break;
564 }
565 /* TODO: we should time out, if something goes wrong!??? */
566 pthread_cond_wait(&(this->condvar), &(this->mutex));
567 }
568
569 pthread_mutex_unlock(&(this->mutex));
570
571 return SUCCESS;
572 }
573
574 /**
575 * Implementation of private_kernel_interface_t.receive_messages.
576 */
577 static void receive_messages(private_kernel_interface_t *this)
578 {
579 while(TRUE)
580 {
581 netlink_message_t response, *listed_response;
582 while (TRUE)
583 {
584 struct sockaddr_nl addr;
585 socklen_t addr_length;
586 size_t length;
587
588 addr_length = sizeof(addr);
589
590 response.hdr.nlmsg_type = XFRM_MSG_NEWSA;
591 length = recvfrom(this->socket, &response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_length);
592 if (length < 0)
593 {
594 if (errno == EINTR)
595 {
596 /* interrupted, try again */
597 continue;
598 }
599 charon->kill(charon, "receiving from netlink socket failed");
600 }
601 if (!NLMSG_OK(&response.hdr, length))
602 {
603 /* bad netlink message */
604 continue;
605 }
606 if (addr.nl_pid != 0)
607 {
608 /* not from kernel. not interested, try another one */
609 continue;
610 }
611 break;
612 }
613
614 /* got a valid message.
615 * requests are handled on our own,
616 * responses are listed for the requesters
617 */
618 if (response.hdr.nlmsg_flags & NLM_F_REQUEST)
619 {
620 /* handle request */
621 }
622 else
623 {
624 /* add response to queue */
625 listed_response = malloc(sizeof(response));
626 memcpy(listed_response, &response, sizeof(response));
627
628 pthread_mutex_lock(&(this->mutex));
629 this->responses->insert_last(this->responses, (void*)listed_response);
630 pthread_mutex_unlock(&(this->mutex));
631 /* signal ALL waiting threads */
632 pthread_cond_broadcast(&(this->condvar));
633 }
634 /* get the next one */
635 }
636 }
637
638 /**
639 * Implementation of kernel_interface_t.destroy.
640 */
641 static void destroy(private_kernel_interface_t *this)
642 {
643 pthread_cancel(this->thread);
644 pthread_join(this->thread, NULL);
645 close(this->socket);
646 this->responses->destroy(this->responses);
647 free(this);
648 }
649
650 /*
651 * Described in header.
652 */
653 kernel_interface_t *kernel_interface_create()
654 {
655 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
656
657 /* public functions */
658 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
659 this->public.add_sa = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,encryption_algorithm_t,chunk_t,integrity_algorithm_t,chunk_t,bool))add_sa;
660 this->public.add_policy = (status_t(*)(kernel_interface_t*,host_t*, host_t*,host_t*,host_t*,u_int8_t,u_int8_t,int,int,bool,bool,u_int32_t))add_policy;
661 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
662 this->public.del_policy = (status_t(*)(kernel_interface_t*,host_t*,host_t*,host_t*,host_t*,u_int8_t,u_int8_t,int,int))del_policy;
663
664 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
665
666 /* private members */
667 this->receive_messages = receive_messages;
668 this->send_message = send_message;
669 this->pid = getpid();
670 this->responses = linked_list_create();
671 pthread_mutex_init(&(this->mutex),NULL);
672 pthread_cond_init(&(this->condvar),NULL);
673 this->seq = 0;
674 this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
675 if (this->socket <= 0)
676 {
677 free(this);
678 charon->kill(charon, "Unable to create netlink socket");
679 }
680
681 if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
682 {
683 close(this->socket);
684 free(this);
685 charon->kill(charon, "Unable to create netlink thread");
686 }
687
688 logger_manager->enable_log_level(logger_manager, TESTER, FULL);
689 return (&this->public);
690 }