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