further work done for simultaneous rekeying/delete
[strongswan.git] / src / 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) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
13 * Copyright (C) 2003 Herbert Xu.
14 *
15 * Contains modified parts from pluto.
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
21 *
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
26 */
27
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/udp.h>
33 #include <pthread.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <string.h>
38
39 #include "kernel_interface.h"
40
41 #include <daemon.h>
42 #include <utils/linked_list.h>
43 #include <queues/jobs/delete_child_sa_job.h>
44 #include <queues/jobs/rekey_child_sa_job.h>
45
46
47 #define KERNEL_ESP 50
48 #define KERNEL_AH 51
49
50 #define SPD_PRIORITY 1024
51
52 #define BUFFER_SIZE 1024
53
54 /* returns a pointer to the first rtattr following the nlmsghdr *nlh and the 'usual' netlink data x like 'struct xfrm_usersa_info' */
55 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
56 /* returns a pointer to the next rtattr following rta.
57 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!! */
58 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
59 /* returns the total size of attached rta data (after 'usual' netlink data x like 'struct xfrm_usersa_info') */
60 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
61
62 typedef struct kernel_algorithm_t kernel_algorithm_t;
63
64 /**
65 * Mapping from the algorithms defined in IKEv2 to
66 * kernel level algorithm names and their key length
67 */
68 struct kernel_algorithm_t {
69 /**
70 * Identifier specified in IKEv2
71 */
72 int ikev2_id;
73
74 /**
75 * Name of the algorithm, as used as kernel identifier
76 */
77 char *name;
78
79 /**
80 * Key length in bits, if fixed size
81 */
82 u_int key_size;
83 };
84 #define END_OF_LIST -1
85
86 /**
87 * Algorithms for encryption
88 */
89 kernel_algorithm_t encryption_algs[] = {
90 /* {ENCR_DES_IV64, "***", 0}, */
91 {ENCR_DES, "des", 64},
92 {ENCR_3DES, "des3_ede", 192},
93 /* {ENCR_RC5, "***", 0}, */
94 /* {ENCR_IDEA, "***", 0}, */
95 {ENCR_CAST, "cast128", 0},
96 {ENCR_BLOWFISH, "blowfish", 0},
97 /* {ENCR_3IDEA, "***", 0}, */
98 /* {ENCR_DES_IV32, "***", 0}, */
99 {ENCR_NULL, "cipher_null", 0},
100 {ENCR_AES_CBC, "aes", 0},
101 /* {ENCR_AES_CTR, "***", 0}, */
102 {END_OF_LIST, NULL, 0},
103 };
104
105 /**
106 * Algorithms for integrity protection
107 */
108 kernel_algorithm_t integrity_algs[] = {
109 {AUTH_HMAC_MD5_96, "md5", 128},
110 {AUTH_HMAC_SHA1_96, "sha1", 160},
111 /* {AUTH_DES_MAC, "***", 0}, */
112 /* {AUTH_KPDK_MD5, "***", 0}, */
113 /* {AUTH_AES_XCBC_96, "***", 0}, */
114 {END_OF_LIST, NULL, 0},
115 };
116
117 /**
118 * Look up a kernel algorithm name and its key size
119 */
120 char* lookup_algorithm(kernel_algorithm_t *kernel_algo, algorithm_t *ikev2_algo, u_int *key_size)
121 {
122 while (kernel_algo->ikev2_id != END_OF_LIST)
123 {
124 if (ikev2_algo->algorithm == kernel_algo->ikev2_id)
125 {
126 /* match, evaluate key length */
127 if (ikev2_algo->key_size)
128 { /* variable length */
129 *key_size = ikev2_algo->key_size;
130 }
131 else
132 { /* fixed length */
133 *key_size = kernel_algo->key_size;
134 }
135 return kernel_algo->name;
136 }
137 kernel_algo++;
138 }
139 return NULL;
140 }
141
142 typedef struct private_kernel_interface_t private_kernel_interface_t;
143
144 /**
145 * @brief Private Variables and Functions of kernel_interface class.
146 *
147 */
148 struct private_kernel_interface_t {
149 /**
150 * Public part of the kernel_interface_t object.
151 */
152 kernel_interface_t public;
153
154 /**
155 * Netlink communication socket.
156 */
157 int socket;
158
159 /**
160 * Process id of kernel thread
161 */
162 pid_t pid;
163
164 /**
165 * Sequence number for messages.
166 */
167 u_int32_t seq;
168
169 /**
170 * List of responded messages.
171 */
172 linked_list_t *responses;
173
174 /**
175 * Thread which receives messages.
176 */
177 pthread_t thread;
178
179 /**
180 * Mutex locks access to replies list.
181 */
182 pthread_mutex_t mutex;
183
184 /**
185 * Condvar allows signaling of threads waiting for a reply.
186 */
187 pthread_cond_t condvar;
188
189 /**
190 * Logger for XFRM stuff
191 */
192 logger_t *logger;
193
194 /**
195 * Function for the thread, receives messages.
196 */
197 void (*receive_messages) (private_kernel_interface_t *this);
198
199 /**
200 * Sends a netlink_message_t down to the kernel and wait for reply.
201 */
202 status_t (*send_message) (private_kernel_interface_t *this, struct nlmsghdr *request, struct nlmsghdr **response);
203 };
204
205 /**
206 * Implementation of kernel_interface_t.get_spi.
207 */
208 static status_t get_spi(private_kernel_interface_t *this,
209 host_t *src, host_t *dest,
210 protocol_id_t protocol, u_int32_t reqid,
211 u_int32_t *spi)
212 {
213 unsigned char request[BUFFER_SIZE];
214 struct nlmsghdr *response;
215
216 memset(&request, 0, sizeof(request));
217 status_t status = SUCCESS;
218
219 this->logger->log(this->logger, CONTROL|LEVEL2, "getting spi");
220
221 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
222 hdr->nlmsg_flags = NLM_F_REQUEST;
223 hdr->nlmsg_type = XFRM_MSG_ALLOCSPI;
224 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userspi_info));
225
226 struct xfrm_userspi_info *userspi = (struct xfrm_userspi_info*)NLMSG_DATA(hdr);
227 userspi->info.saddr = src->get_xfrm_addr(src);
228 userspi->info.id.daddr = dest->get_xfrm_addr(dest);
229 userspi->info.id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
230 userspi->info.mode = TRUE; /* tunnel mode */
231 userspi->info.reqid = reqid;
232 userspi->info.family = src->get_family(src);
233 userspi->min = 0xc0000000;
234 userspi->max = 0xcFFFFFFF;
235
236 if (this->send_message(this, hdr, &response) != SUCCESS)
237 {
238 this->logger->log(this->logger, ERROR, "netlink communication failed");
239 return FAILED;
240 }
241 else if (response->nlmsg_type == NLMSG_ERROR)
242 {
243 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an error: %s",
244 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
245 status = FAILED;
246 }
247 else if (response->nlmsg_type != XFRM_MSG_NEWSA)
248 {
249 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got a unknown reply");
250 status = FAILED;
251 }
252 else if (response->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)))
253 {
254 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an invalid reply");
255 status = FAILED;
256 }
257 else
258 {
259 *spi = ((struct xfrm_usersa_info*)NLMSG_DATA(response))->id.spi;
260 this->logger->log(this->logger, CONTROL|LEVEL1, "SPI is 0x%x", *spi);
261 }
262 free(response);
263
264 return status;
265 }
266
267 /**
268 * Implementation of kernel_interface_t.add_sa.
269 */
270 static status_t add_sa(private_kernel_interface_t *this,
271 host_t *me, host_t *other,
272 u_int32_t spi, protocol_id_t protocol,
273 u_int32_t reqid,
274 u_int64_t expire_soft, u_int64_t expire_hard,
275 algorithm_t *enc_alg, algorithm_t *int_alg,
276 prf_plus_t *prf_plus, natt_conf_t *natt,
277 bool replace)
278 {
279 unsigned char request[BUFFER_SIZE];
280 struct nlmsghdr *response;
281 char *alg_name;
282 size_t key_size;
283
284 memset(&request, 0, sizeof(request));
285 status_t status = SUCCESS;
286
287 this->logger->log(this->logger, CONTROL|LEVEL2, "adding SA");
288
289 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
290 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
291 hdr->nlmsg_type = replace ? XFRM_MSG_UPDSA : XFRM_MSG_NEWSA;
292 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_info));
293
294 struct xfrm_usersa_info *sa = (struct xfrm_usersa_info*)NLMSG_DATA(hdr);
295 sa->saddr = me->get_xfrm_addr(me);
296 sa->id.daddr = other->get_xfrm_addr(other);
297
298 sa->id.spi = spi;
299 sa->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
300 sa->family = me->get_family(me);
301 sa->mode = TRUE; /* tunnel mode */
302 sa->replay_window = 32;
303 sa->reqid = reqid;
304 /* we currently do not expire SAs by volume/packet count */
305 sa->lft.soft_byte_limit = XFRM_INF;
306 sa->lft.hard_byte_limit = XFRM_INF;
307 sa->lft.soft_packet_limit = XFRM_INF;
308 sa->lft.hard_packet_limit = XFRM_INF;
309 /* we use lifetimes since added, not since used */
310 sa->lft.soft_add_expires_seconds = expire_soft;
311 sa->lft.hard_add_expires_seconds = expire_hard;
312 sa->lft.soft_use_expires_seconds = 0;
313 sa->lft.hard_use_expires_seconds = 0;
314
315 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_usersa_info);
316
317 if (enc_alg->algorithm != ENCR_UNDEFINED)
318 {
319 rthdr->rta_type = XFRMA_ALG_CRYPT;
320 alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size);
321 if (alg_name == NULL)
322 {
323 this->logger->log(this->logger, ERROR, "Algorithm %s not supported by kernel!",
324 mapping_find(encryption_algorithm_m, enc_alg->algorithm));
325 return FAILED;
326 }
327 this->logger->log(this->logger, CONTROL|LEVEL2, " using encryption algorithm %s with key size %d",
328 mapping_find(encryption_algorithm_m, enc_alg->algorithm), key_size);
329
330 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
331 hdr->nlmsg_len += rthdr->rta_len;
332 if (hdr->nlmsg_len > sizeof(request))
333 {
334 return FAILED;
335 }
336
337 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
338 algo->alg_key_len = key_size;
339 strcpy(algo->alg_name, alg_name);
340 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
341
342 rthdr = XFRM_RTA_NEXT(rthdr);
343 }
344
345 if (int_alg->algorithm != AUTH_UNDEFINED)
346 {
347 rthdr->rta_type = XFRMA_ALG_AUTH;
348 alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size);
349 if (alg_name == NULL)
350 {
351 this->logger->log(this->logger, ERROR, "Algorithm %s not supported by kernel!",
352 mapping_find(integrity_algorithm_m, int_alg->algorithm));
353 return FAILED;
354 }
355 this->logger->log(this->logger, CONTROL|LEVEL2, " using integrity algorithm %s with key size %d",
356 mapping_find(integrity_algorithm_m, int_alg->algorithm), key_size);
357
358 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size);
359 hdr->nlmsg_len += rthdr->rta_len;
360 if (hdr->nlmsg_len > sizeof(request))
361 {
362 return FAILED;
363 }
364
365 struct xfrm_algo* algo = (struct xfrm_algo*)RTA_DATA(rthdr);
366 algo->alg_key_len = key_size;
367 strcpy(algo->alg_name, alg_name);
368 prf_plus->get_bytes(prf_plus, key_size / 8, algo->alg_key);
369
370 rthdr = XFRM_RTA_NEXT(rthdr);
371 }
372
373 /* TODO: add IPComp here */
374
375 if (natt)
376 {
377 rthdr->rta_type = XFRMA_ENCAP;
378 rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_encap_tmpl));
379
380 hdr->nlmsg_len += rthdr->rta_len;
381 if (hdr->nlmsg_len > sizeof(request))
382 {
383 return FAILED;
384 }
385
386 struct xfrm_encap_tmpl* encap = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
387 encap->encap_type = UDP_ENCAP_ESPINUDP;
388 encap->encap_sport = ntohs(natt->sport);
389 encap->encap_dport = ntohs(natt->dport);
390 memset(&encap->encap_oa, 0, sizeof (xfrm_address_t));
391 /* encap_oa could probably be derived from the
392 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
393 * pluto does the same as we do here but it uses encap_oa in the
394 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
395 * the kernel ignores it anyway
396 * -> does that mean that NAT-T encap doesn't work in transport mode?
397 * No. The reason the kernel ignores NAT-OA is that it recomputes
398 * (or, rather, just ignores) the checksum. If packets pass
399 * the IPSec checks it marks them "checksum ok" so OA isn't needed. */
400
401 rthdr = XFRM_RTA_NEXT(rthdr);
402 }
403
404 if (this->send_message(this, hdr, &response) != SUCCESS)
405 {
406 this->logger->log(this->logger, ERROR, "netlink communication failed");
407 return FAILED;
408 }
409 else if (response->nlmsg_type != NLMSG_ERROR)
410 {
411 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA not acknowledged");
412 status = FAILED;
413 }
414 else if (((struct nlmsgerr*)NLMSG_DATA(response))->error)
415 {
416 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA got an error: %s",
417 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
418 status = FAILED;
419 }
420
421 free(response);
422 return status;
423 }
424
425 static status_t update_sa_hosts(
426 private_kernel_interface_t *this,
427 host_t *src, host_t *dst,
428 host_t *new_src, host_t *new_dst,
429 int src_changes, int dst_changes,
430 u_int32_t spi, protocol_id_t protocol)
431 {
432 unsigned char request[BUFFER_SIZE];
433 struct nlmsghdr *update, *response;
434
435 memset(&request, 0, sizeof(request));
436 status_t status = SUCCESS;
437
438 this->logger->log(this->logger, CONTROL|LEVEL2, "getting SA");
439
440 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
441 hdr->nlmsg_flags = NLM_F_REQUEST;
442 hdr->nlmsg_type = XFRM_MSG_GETSA;
443 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
444
445 struct xfrm_usersa_id *sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
446 sa_id->daddr = dst->get_xfrm_addr(dst);
447 sa_id->spi = spi;
448 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
449 sa_id->family = dst->get_family(dst);
450
451 if (this->send_message(this, hdr, &update) != SUCCESS)
452 {
453 this->logger->log(this->logger, ERROR, "netlink communication failed");
454 return FAILED;
455 }
456 else if (update->nlmsg_type == NLMSG_ERROR)
457 {
458 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got an error: %s",
459 strerror(-((struct nlmsgerr*)NLMSG_DATA(update))->error));
460 free(update);
461 return FAILED;
462 }
463 else if (update->nlmsg_type != XFRM_MSG_NEWSA)
464 {
465 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got a unknown reply");
466 free(update);
467 return FAILED;
468 }
469 else if (update->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_usersa_info)))
470 {
471 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got an invalid reply");
472 free(update);
473 return FAILED;
474 }
475
476 this->logger->log(this->logger, CONTROL|LEVEL2, "updating SA");
477 update->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
478 update->nlmsg_type = XFRM_MSG_UPDSA;
479
480 struct xfrm_usersa_info *sa = (struct xfrm_usersa_info*)NLMSG_DATA(update);
481 if (src_changes & HOST_DIFF_ADDR)
482 {
483 sa->saddr = new_src->get_xfrm_addr(new_src);
484 }
485
486 if (dst_changes & HOST_DIFF_ADDR)
487 {
488 this->logger->log(this->logger, CONTROL|LEVEL2, "destination address changed! replacing SA");
489
490 update->nlmsg_type = XFRM_MSG_NEWSA;
491 sa->id.daddr = new_dst->get_xfrm_addr(new_dst);
492 }
493
494 if (src_changes & HOST_DIFF_PORT || dst_changes & HOST_DIFF_PORT)
495 {
496 struct rtattr *rthdr = XFRM_RTA(update, struct xfrm_usersa_info);
497 size_t rtsize = XFRM_PAYLOAD(update, struct xfrm_usersa_info);
498 while (RTA_OK(rthdr, rtsize))
499 {
500 if (rthdr->rta_type == XFRMA_ENCAP)
501 {
502 struct xfrm_encap_tmpl* encap = (struct xfrm_encap_tmpl*)RTA_DATA(rthdr);
503 encap->encap_sport = ntohs(new_src->get_port(new_src));
504 encap->encap_dport = ntohs(new_dst->get_port(new_dst));
505 break;
506 }
507 rthdr = RTA_NEXT(rthdr, rtsize);
508 }
509 }
510
511 if (this->send_message(this, update, &response) != SUCCESS)
512 {
513 this->logger->log(this->logger, ERROR, "netlink communication failed");
514 free(update);
515 return FAILED;
516 }
517 else if (response->nlmsg_type != NLMSG_ERROR)
518 {
519 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_XXXSA not acknowledged");
520 status = FAILED;
521 }
522 else if (((struct nlmsgerr*)NLMSG_DATA(response))->error)
523 {
524 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_XXXSA got an error: %s",
525 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
526 status = FAILED;
527 }
528 else if (dst_changes & HOST_DIFF_ADDR)
529 {
530 this->logger->log(this->logger, CONTROL|LEVEL2, "deleting old SA");
531 status = this->public.del_sa(&this->public, dst, spi, protocol);
532 }
533
534 free(update);
535 free(response);
536 return status;
537 }
538
539 static status_t del_sa( private_kernel_interface_t *this,
540 host_t *dst,
541 u_int32_t spi,
542 protocol_id_t protocol)
543 {
544 unsigned char request[BUFFER_SIZE];
545 struct nlmsghdr *response;
546
547 memset(&request, 0, sizeof(request));
548 status_t status = SUCCESS;
549
550 this->logger->log(this->logger, CONTROL|LEVEL2, "deleting SA");
551
552 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
553 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
554 hdr->nlmsg_type = XFRM_MSG_DELSA;
555 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_id));
556
557 struct xfrm_usersa_id *sa_id = (struct xfrm_usersa_id*)NLMSG_DATA(hdr);
558 sa_id->daddr = dst->get_xfrm_addr(dst);
559 sa_id->spi = spi;
560 sa_id->proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
561 sa_id->family = dst->get_family(dst);
562
563 if (this->send_message(this, hdr, &response) != SUCCESS)
564 {
565 this->logger->log(this->logger, ERROR, "netlink communication failed");
566 return FAILED;
567 }
568 else if (response->nlmsg_type != NLMSG_ERROR)
569 {
570 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELSA not acknowledged");
571 status = FAILED;
572 }
573 else if (((struct nlmsgerr*)NLMSG_DATA(response))->error)
574 {
575 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELSA got an error: %s",
576 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
577 status = FAILED;
578 }
579
580 free(response);
581 return status;
582 }
583
584 /**
585 * Implementation of kernel_interface_t.add_policy.
586 */
587 static status_t add_policy(private_kernel_interface_t *this,
588 host_t *me, host_t *other,
589 host_t *src, host_t *dst,
590 u_int8_t src_hostbits, u_int8_t dst_hostbits,
591 int direction, int upper_proto,
592 protocol_id_t protocol,
593 u_int32_t reqid)
594 {
595 unsigned char request[BUFFER_SIZE];
596 struct nlmsghdr *response;
597
598 memset(&request, 0, sizeof(request));
599 status_t status = SUCCESS;
600
601 this->logger->log(this->logger, CONTROL|LEVEL2, "adding policy");
602
603 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
604 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
605 hdr->nlmsg_type = XFRM_MSG_UPDPOLICY;
606 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info));
607
608 struct xfrm_userpolicy_info *policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(hdr);
609
610 policy->sel.sport = htons(src->get_port(src));
611 policy->sel.sport_mask = (policy->sel.sport) ? ~0 : 0;
612 policy->sel.saddr = src->get_xfrm_addr(src);
613 policy->sel.prefixlen_s = src_hostbits;
614
615 policy->sel.dport = htons(dst->get_port(dst));
616 policy->sel.dport_mask = (policy->sel.dport) ? ~0 : 0;
617 policy->sel.daddr = dst->get_xfrm_addr(dst);
618 policy->sel.prefixlen_d = dst_hostbits;
619
620 policy->sel.proto = upper_proto;
621 policy->sel.family = src->get_family(src);
622
623 policy->dir = direction;
624 policy->priority = SPD_PRIORITY;
625 policy->action = XFRM_POLICY_ALLOW;
626 policy->share = XFRM_SHARE_ANY;
627
628 /* policies currently don't expire */
629 policy->lft.soft_byte_limit = XFRM_INF;
630 policy->lft.soft_packet_limit = XFRM_INF;
631 policy->lft.hard_byte_limit = XFRM_INF;
632 policy->lft.hard_packet_limit = XFRM_INF;
633 policy->lft.soft_add_expires_seconds = 0;
634 policy->lft.hard_add_expires_seconds = 0;
635 policy->lft.soft_use_expires_seconds = 0;
636 policy->lft.hard_use_expires_seconds = 0;
637
638 struct rtattr *rthdr = XFRM_RTA(hdr, struct xfrm_userpolicy_info);
639 rthdr->rta_type = XFRMA_TMPL;
640
641 rthdr->rta_len = sizeof(struct xfrm_user_tmpl);
642 rthdr->rta_len = RTA_LENGTH(rthdr->rta_len);
643
644 hdr->nlmsg_len += rthdr->rta_len;
645 if (hdr->nlmsg_len > sizeof(request))
646 {
647 return FAILED;
648 }
649
650 struct xfrm_user_tmpl *tmpl = (struct xfrm_user_tmpl*)RTA_DATA(rthdr);
651 tmpl->reqid = reqid;
652 tmpl->id.proto = (protocol == PROTO_ESP) ? KERNEL_ESP : KERNEL_AH;
653 tmpl->aalgos = tmpl->ealgos = tmpl->calgos = ~0;
654 tmpl->mode = TRUE;
655
656 tmpl->saddr = me->get_xfrm_addr(me);
657 tmpl->id.daddr = other->get_xfrm_addr(other);
658
659 if (this->send_message(this, hdr, &response) != SUCCESS)
660 {
661 this->logger->log(this->logger, ERROR, "netlink communication failed");
662 return FAILED;
663 }
664 else if (response->nlmsg_type != NLMSG_ERROR)
665 {
666 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWPOLICY not acknowledged");
667 status = FAILED;
668 }
669 else if (((struct nlmsgerr*)NLMSG_DATA(response))->error)
670 {
671 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWPOLICY got an error: %s",
672 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
673 status = FAILED;
674 }
675
676 free(response);
677 return status;
678 }
679
680 static status_t query_policy(private_kernel_interface_t *this,
681 host_t *me, host_t *other,
682 host_t *src, host_t *dst,
683 u_int8_t src_hostbits, u_int8_t dst_hostbits,
684 int direction, int upper_proto,
685 time_t *use_time)
686 {
687 unsigned char request[BUFFER_SIZE];
688 struct nlmsghdr *response;
689
690 memset(&request, 0, sizeof(request));
691 status_t status = SUCCESS;
692
693 this->logger->log(this->logger, CONTROL|LEVEL2, "querying policy");
694
695 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
696 hdr->nlmsg_flags = NLM_F_REQUEST;
697 hdr->nlmsg_type = XFRM_MSG_GETPOLICY;
698 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
699
700 struct xfrm_userpolicy_id *policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
701 policy_id->sel.sport = htons(src->get_port(src));
702 policy_id->sel.sport_mask = (policy_id->sel.sport) ? ~0 : 0;
703 policy_id->sel.saddr = src->get_xfrm_addr(src);
704 policy_id->sel.prefixlen_s = src_hostbits;
705
706 policy_id->sel.dport = htons(dst->get_port(dst));
707 policy_id->sel.dport_mask = (policy_id->sel.dport) ? ~0 : 0;
708 policy_id->sel.daddr = dst->get_xfrm_addr(dst);
709 policy_id->sel.prefixlen_d = dst_hostbits;
710
711 policy_id->sel.proto = upper_proto;
712 policy_id->sel.family = src->get_family(src);
713
714 policy_id->dir = direction;
715
716 if (this->send_message(this, hdr, &response) != SUCCESS)
717 {
718 this->logger->log(this->logger, ERROR, "netlink communication failed");
719 return FAILED;
720 }
721 else if (response->nlmsg_type == NLMSG_ERROR)
722 {
723 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an error: %s",
724 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
725 free(response);
726 return FAILED;
727 }
728 else if (response->nlmsg_type != XFRM_MSG_NEWPOLICY)
729 {
730 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an unknown reply");
731 free(response);
732 return FAILED;
733 }
734 else if (response->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info)))
735 {
736 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an invalid reply");
737 free(response);
738 return FAILED;
739 }
740
741 struct xfrm_userpolicy_info *policy = (struct xfrm_userpolicy_info*)NLMSG_DATA(response);
742
743 *use_time = (time_t)policy->curlft.use_time;
744
745 free(response);
746 return status;
747 }
748
749 /**
750 * Implementation of kernel_interface_t.del_policy.
751 */
752 static status_t del_policy(private_kernel_interface_t *this,
753 host_t *me, host_t *other,
754 host_t *src, host_t *dst,
755 u_int8_t src_hostbits, u_int8_t dst_hostbits,
756 int direction, int upper_proto)
757 {
758 unsigned char request[BUFFER_SIZE];
759 struct nlmsghdr *response;
760
761 memset(&request, 0, sizeof(request));
762 status_t status = SUCCESS;
763
764 this->logger->log(this->logger, CONTROL|LEVEL2, "deleting policy");
765
766 struct nlmsghdr *hdr = (struct nlmsghdr*)request;
767 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
768 hdr->nlmsg_type = XFRM_MSG_DELPOLICY;
769 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id));
770
771 struct xfrm_userpolicy_id *policy_id = (struct xfrm_userpolicy_id*)NLMSG_DATA(hdr);
772 policy_id->sel.sport = htons(src->get_port(src));
773 policy_id->sel.sport_mask = (policy_id->sel.sport) ? ~0 : 0;
774 policy_id->sel.saddr = src->get_xfrm_addr(src);
775 policy_id->sel.prefixlen_s = src_hostbits;
776
777 policy_id->sel.dport = htons(dst->get_port(dst));
778 policy_id->sel.dport_mask = (policy_id->sel.dport) ? ~0 : 0;
779 policy_id->sel.daddr = dst->get_xfrm_addr(dst);
780 policy_id->sel.prefixlen_d = dst_hostbits;
781
782 policy_id->sel.proto = upper_proto;
783 policy_id->sel.family = src->get_family(src);
784
785 policy_id->dir = direction;
786
787 if (this->send_message(this, hdr, &response) != SUCCESS)
788 {
789 this->logger->log(this->logger, ERROR, "netlink communication failed");
790 return FAILED;
791 }
792 else if (response->nlmsg_type != NLMSG_ERROR)
793 {
794 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELPOLICY not acknowledged");
795 status = FAILED;
796 }
797 else if (((struct nlmsgerr*)NLMSG_DATA(response))->error)
798 {
799 this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELPOLICY got an error: %s",
800 strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error));
801 status = FAILED;
802 }
803
804 free(response);
805 return status;
806 }
807
808 /**
809 * Implementation of private_kernel_interface_t.send_message.
810 */
811 static status_t send_message(private_kernel_interface_t *this, struct nlmsghdr *request, struct nlmsghdr **response)
812 {
813 size_t length;
814 struct sockaddr_nl addr;
815
816 request->nlmsg_seq = ++this->seq;
817 request->nlmsg_pid = 0;
818
819 memset(&addr, 0, sizeof(struct sockaddr_nl));
820 addr.nl_family = AF_NETLINK;
821 addr.nl_pid = 0;
822 addr.nl_groups = 0;
823
824 length = sendto(this->socket,(void *)request, request->nlmsg_len, 0, (struct sockaddr *)&addr, sizeof(addr));
825
826 if (length < 0)
827 {
828 return FAILED;
829 }
830 else if (length != request->nlmsg_len)
831 {
832 return FAILED;
833 }
834
835 pthread_mutex_lock(&(this->mutex));
836
837 while (TRUE)
838 {
839 iterator_t *iterator;
840 bool found = FALSE;
841 /* search list, break if found */
842 iterator = this->responses->create_iterator(this->responses, TRUE);
843 while (iterator->has_next(iterator))
844 {
845 struct nlmsghdr *listed_response;
846 iterator->current(iterator, (void**)&listed_response);
847 if (listed_response->nlmsg_seq == request->nlmsg_seq)
848 {
849 /* matches our request, this is the reply */
850 *response = listed_response;
851 iterator->remove(iterator);
852 found = TRUE;
853 break;
854 }
855 }
856 iterator->destroy(iterator);
857
858 if (found)
859 {
860 break;
861 }
862 /* TODO: we should time out, if something goes wrong!??? */
863 pthread_cond_wait(&(this->condvar), &(this->mutex));
864 }
865
866 pthread_mutex_unlock(&(this->mutex));
867
868 return SUCCESS;
869 }
870
871 /**
872 * Implementation of private_kernel_interface_t.receive_messages.
873 */
874 static void receive_messages(private_kernel_interface_t *this)
875 {
876 while(TRUE)
877 {
878 unsigned char response[BUFFER_SIZE];
879 struct nlmsghdr *hdr, *listed_response;
880 while (TRUE)
881 {
882 struct sockaddr_nl addr;
883 socklen_t addr_length;
884 size_t length;
885
886 addr_length = sizeof(addr);
887
888 length = recvfrom(this->socket, &response, sizeof(response), 0, (struct sockaddr*)&addr, &addr_length);
889 if (length < 0)
890 {
891 if (errno == EINTR)
892 {
893 /* interrupted, try again */
894 continue;
895 }
896 charon->kill(charon, "receiving from netlink socket failed");
897 }
898 if (!NLMSG_OK((struct nlmsghdr *)response, length))
899 {
900 /* bad netlink message */
901 continue;
902 }
903 if (addr.nl_pid != 0)
904 {
905 /* not from kernel. not interested, try another one */
906 continue;
907 }
908 /* good message, handle it */
909 break;
910 }
911
912 /* we handle ACQUIRE and EXPIRE messages directly
913 */
914 hdr = (struct nlmsghdr*)response;
915 if (hdr->nlmsg_type == XFRM_MSG_ACQUIRE)
916 {
917 this->logger->log(this->logger, CONTROL,
918 "Received a XFRM_MSG_ACQUIRE. Ignored");
919 }
920 else if (hdr->nlmsg_type == XFRM_MSG_EXPIRE)
921 {
922 job_t *job;
923 protocol_id_t protocol;
924 u_int32_t spi;
925 struct xfrm_user_expire *expire;
926
927 expire = (struct xfrm_user_expire*)NLMSG_DATA(hdr);
928 protocol = expire->state.id.proto == KERNEL_ESP ?
929 PROTO_ESP : PROTO_AH;
930 spi = expire->state.id.spi;
931
932 this->logger->log(this->logger, CONTROL|LEVEL1,
933 "Received a XFRM_MSG_EXPIRE");
934 this->logger->log(this->logger, CONTROL,
935 "creating %s job for %s CHILD_SA 0x%x",
936 expire->hard ? "delete" : "rekey",
937 mapping_find(protocol_id_m, protocol), ntohl(spi));
938 if (expire->hard)
939 {
940 job = (job_t*)delete_child_sa_job_create(protocol, spi);
941 }
942 else
943 {
944 job = (job_t*)rekey_child_sa_job_create(protocol, spi);
945 }
946 charon->job_queue->add(charon->job_queue, job);
947 }
948 /* NLMSG_ERROR is sent back for acknowledge (or on error), an
949 * XFRM_MSG_NEWSA is returned when we alloc spis and when
950 * updating SAs.
951 * XFRM_MSG_NEWPOLICY is returned when we query a policy.
952 * list these responses for the sender
953 */
954 else if (hdr->nlmsg_type == NLMSG_ERROR ||
955 hdr->nlmsg_type == XFRM_MSG_NEWSA ||
956 hdr->nlmsg_type == XFRM_MSG_NEWPOLICY)
957 {
958 /* add response to queue */
959 listed_response = malloc(hdr->nlmsg_len);
960 memcpy(listed_response, &response, hdr->nlmsg_len);
961
962 pthread_mutex_lock(&(this->mutex));
963 this->responses->insert_last(this->responses, (void*)listed_response);
964 pthread_mutex_unlock(&(this->mutex));
965 /* signal ALL waiting threads */
966 pthread_cond_broadcast(&(this->condvar));
967 }
968 /* we are not interested in anything other.
969 * anyway, move on to the next message */
970 continue;
971 }
972 }
973
974 /**
975 * Implementation of kernel_interface_t.destroy.
976 */
977 static void destroy(private_kernel_interface_t *this)
978 {
979 pthread_cancel(this->thread);
980 pthread_join(this->thread, NULL);
981 close(this->socket);
982 this->responses->destroy(this->responses);
983 free(this);
984 }
985
986 /*
987 * Described in header.
988 */
989 kernel_interface_t *kernel_interface_create()
990 {
991 struct sockaddr_nl addr;
992 private_kernel_interface_t *this = malloc_thing(private_kernel_interface_t);
993
994 /* public functions */
995 this->public.get_spi = (status_t(*)(kernel_interface_t*,host_t*,host_t*,protocol_id_t,u_int32_t,u_int32_t*))get_spi;
996 this->public.add_sa = (status_t(*)(kernel_interface_t *,host_t*,host_t*,u_int32_t,protocol_id_t,u_int32_t,u_int64_t,u_int64_t,algorithm_t*,algorithm_t*,prf_plus_t*,natt_conf_t*,bool))add_sa;
997 this->public.update_sa_hosts = (status_t(*)(kernel_interface_t*,host_t*,host_t*,host_t*,host_t*,int,int,u_int32_t,protocol_id_t))update_sa_hosts;
998 this->public.del_sa = (status_t(*)(kernel_interface_t*,host_t*,u_int32_t,protocol_id_t))del_sa;
999 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,protocol_id_t,u_int32_t))add_policy;
1000 this->public.query_policy = (status_t(*)(kernel_interface_t*,host_t*,host_t*,host_t*,host_t*,u_int8_t,u_int8_t,int,int,time_t*))query_policy;
1001 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;
1002 this->public.destroy = (void(*)(kernel_interface_t*)) destroy;
1003
1004 /* private members */
1005 this->receive_messages = receive_messages;
1006 this->send_message = send_message;
1007 this->pid = getpid();
1008 this->responses = linked_list_create();
1009 this->logger = logger_manager->get_logger(logger_manager, XFRM);
1010 pthread_mutex_init(&(this->mutex),NULL);
1011 pthread_cond_init(&(this->condvar),NULL);
1012 this->seq = 0;
1013
1014 /* open netlink socket */
1015 this->socket = socket(PF_NETLINK, SOCK_RAW, NETLINK_XFRM);
1016 if (this->socket <= 0)
1017 {
1018 this->responses->destroy(this->responses);
1019 free(this);
1020 charon->kill(charon, "Unable to create netlink socket");
1021 }
1022
1023 /* bind the socket and reqister for ACQUIRE & EXPIRE */
1024 addr.nl_family = AF_NETLINK;
1025 addr.nl_pid = getpid();
1026 addr.nl_groups = XFRMGRP_ACQUIRE | XFRMGRP_EXPIRE;
1027 if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)) != 0)
1028 {
1029 this->responses->destroy(this->responses);
1030 close(this->socket);
1031 free(this);
1032 charon->kill(charon, "Unable to bind netlink socket");
1033 }
1034
1035 if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->receive_messages, this) != 0)
1036 {
1037 this->responses->destroy(this->responses);
1038 close(this->socket);
1039 free(this);
1040 charon->kill(charon, "Unable to create netlink thread");
1041 }
1042
1043 return (&this->public);
1044 }