2 * @file kernel_interface.c
4 * @brief Implementation of kernel_interface_t.
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.
15 * Contains modified parts from pluto.
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>.
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
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
32 #include <linux/xfrm.h>
33 #include <linux/udp.h>
40 #include "kernel_interface.h"
43 #include <utils/linked_list.h>
44 #include <queues/jobs/delete_child_sa_job.h>
45 #include <queues/jobs/rekey_child_sa_job.h>
46 #include <queues/jobs/acquire_job.h>
48 /** kernel level protocol identifiers */
52 /** default priority of installed policies */
54 #define PRIO_HIGH 2000
56 #define BUFFER_SIZE 1024
59 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
60 * 'usual' netlink data x like 'struct xfrm_usersa_info'
62 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
64 * returns a pointer to the next rtattr following rta.
65 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
67 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
69 * returns the total size of attached rta data
70 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
72 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
74 typedef struct kernel_algorithm_t kernel_algorithm_t
;
77 * Mapping from the algorithms defined in IKEv2 to
78 * kernel level algorithm names and their key length
80 struct kernel_algorithm_t
{
82 * Identifier specified in IKEv2
87 * Name of the algorithm, as used as kernel identifier
92 * Key length in bits, if fixed size
96 #define END_OF_LIST -1
99 * Algorithms for encryption
101 kernel_algorithm_t encryption_algs
[] = {
102 /* {ENCR_DES_IV64, "***", 0}, */
103 {ENCR_DES
, "des", 64},
104 {ENCR_3DES
, "des3_ede", 192},
105 /* {ENCR_RC5, "***", 0}, */
106 /* {ENCR_IDEA, "***", 0}, */
107 {ENCR_CAST
, "cast128", 0},
108 {ENCR_BLOWFISH
, "blowfish", 0},
109 /* {ENCR_3IDEA, "***", 0}, */
110 /* {ENCR_DES_IV32, "***", 0}, */
111 {ENCR_NULL
, "cipher_null", 0},
112 {ENCR_AES_CBC
, "aes", 0},
113 /* {ENCR_AES_CTR, "***", 0}, */
114 {END_OF_LIST
, NULL
, 0},
118 * Algorithms for integrity protection
120 kernel_algorithm_t integrity_algs
[] = {
121 {AUTH_HMAC_MD5_96
, "md5", 128},
122 {AUTH_HMAC_SHA1_96
, "sha1", 160},
123 /* {AUTH_DES_MAC, "***", 0}, */
124 /* {AUTH_KPDK_MD5, "***", 0}, */
125 /* {AUTH_AES_XCBC_96, "***", 0}, */
126 {END_OF_LIST
, NULL
, 0},
130 * Look up a kernel algorithm name and its key size
132 char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
133 algorithm_t
*ikev2_algo
, u_int
*key_size
)
135 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
137 if (ikev2_algo
->algorithm
== kernel_algo
->ikev2_id
)
139 /* match, evaluate key length */
140 if (ikev2_algo
->key_size
)
141 { /* variable length */
142 *key_size
= ikev2_algo
->key_size
;
146 *key_size
= kernel_algo
->key_size
;
148 return kernel_algo
->name
;
157 typedef struct kernel_policy_t kernel_policy_t
;
160 * Installed kernel policy.
162 struct kernel_policy_t
{
164 /** direction of this policy: in, out, forward */
167 /** reqid of the policy */
170 /** parameters of installed policy */
171 struct xfrm_selector sel
;
173 /** by how many CHILD_SA's this policy is used */
178 typedef struct private_kernel_interface_t private_kernel_interface_t
;
181 * Private Variables and Functions of kernel_interface class.
183 struct private_kernel_interface_t
{
185 * Public part of the kernel_interface_t object.
187 kernel_interface_t
public;
190 * List of installed policies (kernel_policy_t)
192 linked_list_t
*policies
;
195 * Mutex locks access to policies list.
197 pthread_mutex_t pol_mutex
;
200 * Netlink communication socket.
205 * Process id of kernel thread
210 * Sequence number for messages.
215 * List of responded messages.
217 linked_list_t
*responses
;
220 * Thread which receives messages.
225 * Mutex locks access to replies list.
227 pthread_mutex_t rep_mutex
;
230 * Condvar allows signaling of threads waiting for a reply.
232 pthread_cond_t condvar
;
237 * Send a message down to the kernel and wait for its response
239 static status_t
send_message(private_kernel_interface_t
*this,
240 struct nlmsghdr
*request
, struct nlmsghdr
**response
)
243 struct sockaddr_nl addr
;
245 request
->nlmsg_seq
= ++this->seq
;
246 request
->nlmsg_pid
= 0;
248 memset(&addr
, 0, sizeof(struct sockaddr_nl
));
249 addr
.nl_family
= AF_NETLINK
;
253 length
= sendto(this->socket
,(void *)request
, request
->nlmsg_len
, 0,
254 (struct sockaddr
*)&addr
, sizeof(addr
));
260 else if (length
!= request
->nlmsg_len
)
265 pthread_mutex_lock(&(this->rep_mutex
));
269 iterator_t
*iterator
;
270 struct nlmsghdr
*listed_response
;
273 /* search list, break if found */
274 iterator
= this->responses
->create_iterator(this->responses
, TRUE
);
275 while (iterator
->iterate(iterator
, (void**)&listed_response
))
277 if (listed_response
->nlmsg_seq
== request
->nlmsg_seq
)
279 /* matches our request, this is the reply */
280 *response
= listed_response
;
281 iterator
->remove(iterator
);
286 iterator
->destroy(iterator
);
292 /* TODO: we should time out, if something goes wrong!??? */
293 pthread_cond_wait(&(this->condvar
), &(this->rep_mutex
));
296 pthread_mutex_unlock(&(this->rep_mutex
));
302 * Implementation of private_kernel_interface_t.receive_messages.
304 static void receive_messages(private_kernel_interface_t
*this)
308 unsigned char response
[BUFFER_SIZE
];
309 struct nlmsghdr
*hdr
, *listed_response
;
312 struct sockaddr_nl addr
;
313 socklen_t addr_length
;
316 addr_length
= sizeof(addr
);
318 length
= recvfrom(this->socket
, &response
, sizeof(response
), 0, (struct sockaddr
*)&addr
, &addr_length
);
323 /* interrupted, try again */
326 charon
->kill(charon
, "receiving from netlink socket failed");
328 if (!NLMSG_OK((struct nlmsghdr
*)response
, length
))
330 /* bad netlink message */
333 if (addr
.nl_pid
!= 0)
335 /* not from kernel. not interested, try another one */
338 /* good message, handle it */
342 /* we handle ACQUIRE and EXPIRE messages directly */
343 hdr
= (struct nlmsghdr
*)response
;
344 if (hdr
->nlmsg_type
== XFRM_MSG_ACQUIRE
)
348 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
349 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
350 if (RTA_OK(rthdr
, rtsize
))
352 if (rthdr
->rta_type
== XFRMA_TMPL
)
354 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
360 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
364 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
365 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid %d",
367 job
= (job_t
*)acquire_job_create(reqid
);
368 charon
->job_queue
->add(charon
->job_queue
, job
);
371 else if (hdr
->nlmsg_type
== XFRM_MSG_EXPIRE
)
374 protocol_id_t protocol
;
375 u_int32_t spi
, reqid
;
376 struct xfrm_user_expire
*expire
;
378 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
379 protocol
= expire
->state
.id
.proto
== KERNEL_ESP ?
380 PROTO_ESP
: PROTO_AH
;
381 spi
= expire
->state
.id
.spi
;
382 reqid
= expire
->state
.reqid
;
384 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
385 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA 0x%x (reqid %d)",
386 expire
->hard ?
"delete" : "rekey",
387 protocol_id_names
, protocol
, ntohl(spi
),
391 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
395 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
397 charon
->job_queue
->add(charon
->job_queue
, job
);
399 /* NLMSG_ERROR is sent back for acknowledge (or on error), an
400 * XFRM_MSG_NEWSA is returned when we alloc spis and when
402 * XFRM_MSG_NEWPOLICY is returned when we query a policy.
403 * list these responses for the sender
405 else if (hdr
->nlmsg_type
== NLMSG_ERROR
||
406 hdr
->nlmsg_type
== XFRM_MSG_NEWSA
||
407 hdr
->nlmsg_type
== XFRM_MSG_NEWPOLICY
)
409 /* add response to queue */
410 listed_response
= malloc(hdr
->nlmsg_len
);
411 memcpy(listed_response
, &response
, hdr
->nlmsg_len
);
413 pthread_mutex_lock(&(this->rep_mutex
));
414 this->responses
->insert_last(this->responses
, (void*)listed_response
);
415 pthread_mutex_unlock(&(this->rep_mutex
));
416 /* signal ALL waiting threads */
417 pthread_cond_broadcast(&(this->condvar
));
419 /* we are not interested in anything other.
420 * anyway, move on to the next message */
426 * convert a host_t to a struct xfrm_address
428 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
430 chunk_t chunk
= host
->get_address(host
);
431 memcpy(xfrm
, chunk
.ptr
, max(chunk
.len
, sizeof(xfrm_address_t
)));
435 * Implementation of kernel_interface_t.get_spi.
437 static status_t
get_spi(private_kernel_interface_t
*this,
438 host_t
*src
, host_t
*dst
,
439 protocol_id_t protocol
, u_int32_t reqid
,
442 unsigned char request
[BUFFER_SIZE
];
443 struct nlmsghdr
*response
;
444 struct nlmsghdr
*hdr
;
445 struct xfrm_userspi_info
*userspi
;
447 memset(&request
, 0, sizeof(request
));
448 status_t status
= SUCCESS
;
450 DBG2(DBG_KNL
, "getting spi");
452 hdr
= (struct nlmsghdr
*)request
;
453 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
454 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
455 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
457 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
458 host2xfrm(src
, &userspi
->info
.saddr
);
459 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
460 userspi
->info
.id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
461 userspi
->info
.mode
= TRUE
; /* tunnel mode */
462 userspi
->info
.reqid
= reqid
;
463 userspi
->info
.family
= src
->get_family(src
);
464 userspi
->min
= 0xc0000000;
465 userspi
->max
= 0xcFFFFFFF;
467 if (send_message(this, hdr
, &response
) != SUCCESS
)
469 DBG1(DBG_KNL
, "netlink communication failed");
472 else if (response
->nlmsg_type
== NLMSG_ERROR
)
474 DBG1(DBG_KNL
, "netlink request XFRM_MSG_ALLOCSPI got an error: %s",
475 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
478 else if (response
->nlmsg_type
!= XFRM_MSG_NEWSA
)
480 DBG1(DBG_KNL
, "netlink request XFRM_MSG_ALLOCSPI got a unknown reply");
483 else if (response
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
)))
485 DBG1(DBG_KNL
, "netlink request XFRM_MSG_ALLOCSPI got an invalid reply");
490 *spi
= ((struct xfrm_usersa_info
*)NLMSG_DATA(response
))->id
.spi
;
491 DBG2(DBG_KNL
, "SPI is 0x%x", *spi
);
499 * Implementation of kernel_interface_t.add_sa.
501 static status_t
add_sa(private_kernel_interface_t
*this,
502 host_t
*src
, host_t
*dst
, u_int32_t spi
,
503 protocol_id_t protocol
, u_int32_t reqid
,
504 u_int64_t expire_soft
, u_int64_t expire_hard
,
505 algorithm_t
*enc_alg
, algorithm_t
*int_alg
,
506 prf_plus_t
*prf_plus
, natt_conf_t
*natt
,
509 unsigned char request
[BUFFER_SIZE
];
510 struct nlmsghdr
*response
;
513 struct nlmsghdr
*hdr
;
514 struct xfrm_usersa_info
*sa
;
516 memset(&request
, 0, sizeof(request
));
517 status_t status
= SUCCESS
;
519 DBG2(DBG_KNL
, "adding SA");
521 hdr
= (struct nlmsghdr
*)request
;
522 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
523 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
524 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
526 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
527 host2xfrm(src
, &sa
->saddr
);
528 host2xfrm(dst
, &sa
->id
.daddr
);
530 sa
->id
.proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
531 sa
->family
= src
->get_family(src
);
532 sa
->mode
= TRUE
; /* tunnel mode */
533 sa
->replay_window
= 32;
535 /* we currently do not expire SAs by volume/packet count */
536 sa
->lft
.soft_byte_limit
= XFRM_INF
;
537 sa
->lft
.hard_byte_limit
= XFRM_INF
;
538 sa
->lft
.soft_packet_limit
= XFRM_INF
;
539 sa
->lft
.hard_packet_limit
= XFRM_INF
;
540 /* we use lifetimes since added, not since used */
541 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
542 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
543 sa
->lft
.soft_use_expires_seconds
= 0;
544 sa
->lft
.hard_use_expires_seconds
= 0;
546 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
548 if (enc_alg
->algorithm
!= ENCR_UNDEFINED
)
550 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
551 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &key_size
);
552 if (alg_name
== NULL
)
554 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
555 encryption_algorithm_names
, enc_alg
->algorithm
);
558 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
559 encryption_algorithm_names
, enc_alg
->algorithm
, key_size
);
561 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
562 hdr
->nlmsg_len
+= rthdr
->rta_len
;
563 if (hdr
->nlmsg_len
> sizeof(request
))
568 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
569 algo
->alg_key_len
= key_size
;
570 strcpy(algo
->alg_name
, alg_name
);
571 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
573 rthdr
= XFRM_RTA_NEXT(rthdr
);
576 if (int_alg
->algorithm
!= AUTH_UNDEFINED
)
578 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
579 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &key_size
);
580 if (alg_name
== NULL
)
582 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
583 integrity_algorithm_names
, int_alg
->algorithm
);
586 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
587 integrity_algorithm_names
, int_alg
->algorithm
, key_size
);
589 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + key_size
);
590 hdr
->nlmsg_len
+= rthdr
->rta_len
;
591 if (hdr
->nlmsg_len
> sizeof(request
))
596 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
597 algo
->alg_key_len
= key_size
;
598 strcpy(algo
->alg_name
, alg_name
);
599 prf_plus
->get_bytes(prf_plus
, key_size
/ 8, algo
->alg_key
);
601 rthdr
= XFRM_RTA_NEXT(rthdr
);
604 /* TODO: add IPComp here */
608 rthdr
->rta_type
= XFRMA_ENCAP
;
609 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
611 hdr
->nlmsg_len
+= rthdr
->rta_len
;
612 if (hdr
->nlmsg_len
> sizeof(request
))
617 struct xfrm_encap_tmpl
* encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
618 encap
->encap_type
= UDP_ENCAP_ESPINUDP
;
619 encap
->encap_sport
= htons(natt
->sport
);
620 encap
->encap_dport
= htons(natt
->dport
);
621 memset(&encap
->encap_oa
, 0, sizeof (xfrm_address_t
));
622 /* encap_oa could probably be derived from the
623 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
624 * pluto does the same as we do here but it uses encap_oa in the
625 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
626 * the kernel ignores it anyway
627 * -> does that mean that NAT-T encap doesn't work in transport mode?
628 * No. The reason the kernel ignores NAT-OA is that it recomputes
629 * (or, rather, just ignores) the checksum. If packets pass
630 * the IPSec checks it marks them "checksum ok" so OA isn't needed. */
632 rthdr
= XFRM_RTA_NEXT(rthdr
);
635 if (send_message(this, hdr
, &response
) != SUCCESS
)
637 DBG1(DBG_KNL
, "netlink communication failed");
640 else if (response
->nlmsg_type
!= NLMSG_ERROR
)
642 DBG1(DBG_KNL
, "netlink request XFRM_MSG_NEWSA not acknowledged");
645 else if (((struct nlmsgerr
*)NLMSG_DATA(response
))->error
)
647 DBG1(DBG_KNL
, "netlink request XFRM_MSG_NEWSA got an error: %s",
648 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
657 * Implementation of kernel_interface_t.update_sa.
659 static status_t
update_sa(
660 private_kernel_interface_t
*this,
661 host_t
*src
, host_t
*dst
,
662 host_t
*new_src
, host_t
*new_dst
,
663 host_diff_t src_changes
, host_diff_t dst_changes
,
664 u_int32_t spi
, protocol_id_t protocol
)
666 unsigned char request
[BUFFER_SIZE
];
667 struct nlmsghdr
*update
, *response
;
668 struct nlmsghdr
*hdr
;
669 struct xfrm_usersa_id
*sa_id
;
671 memset(&request
, 0, sizeof(request
));
672 status_t status
= SUCCESS
;
674 DBG2(DBG_KNL
, "getting SA");
676 hdr
= (struct nlmsghdr
*)request
;
677 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
678 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
679 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
681 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
682 host2xfrm(dst
, &sa_id
->daddr
);
684 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
685 sa_id
->family
= dst
->get_family(dst
);
687 if (send_message(this, hdr
, &update
) != SUCCESS
)
689 DBG1(DBG_KNL
, "netlink communication failed");
692 else if (update
->nlmsg_type
== NLMSG_ERROR
)
694 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETSA got an error: %s",
695 strerror(-((struct nlmsgerr
*)NLMSG_DATA(update
))->error
));
699 else if (update
->nlmsg_type
!= XFRM_MSG_NEWSA
)
701 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETSA got a unknown reply");
705 else if (update
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
)))
707 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETSA got an invalid reply");
712 DBG2(DBG_KNL
, "updating SA");
713 update
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
714 update
->nlmsg_type
= XFRM_MSG_UPDSA
;
716 struct xfrm_usersa_info
*sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(update
);
717 if (src_changes
& HOST_DIFF_ADDR
)
719 host2xfrm(new_src
, &sa
->saddr
);
722 if (dst_changes
& HOST_DIFF_ADDR
)
724 DBG2(DBG_KNL
, "destination address changed! replacing SA");
726 update
->nlmsg_type
= XFRM_MSG_NEWSA
;
727 host2xfrm(new_dst
, &sa
->id
.daddr
);
730 if (src_changes
& HOST_DIFF_PORT
|| dst_changes
& HOST_DIFF_PORT
)
732 struct rtattr
*rthdr
= XFRM_RTA(update
, struct xfrm_usersa_info
);
733 size_t rtsize
= XFRM_PAYLOAD(update
, struct xfrm_usersa_info
);
734 while (RTA_OK(rthdr
, rtsize
))
736 if (rthdr
->rta_type
== XFRMA_ENCAP
)
738 struct xfrm_encap_tmpl
* encap
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
739 encap
->encap_sport
= ntohs(new_src
->get_port(new_src
));
740 encap
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
743 rthdr
= RTA_NEXT(rthdr
, rtsize
);
747 if (send_message(this, update
, &response
) != SUCCESS
)
749 DBG1(DBG_KNL
, "netlink communication failed");
753 else if (response
->nlmsg_type
!= NLMSG_ERROR
)
755 DBG1(DBG_KNL
, "netlink request XFRM_MSG_XXXSA not acknowledged");
758 else if (((struct nlmsgerr
*)NLMSG_DATA(response
))->error
)
760 DBG1(DBG_KNL
, "netlink request XFRM_MSG_XXXSA got an error: %s",
761 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
764 else if (dst_changes
& HOST_DIFF_ADDR
)
766 DBG2(DBG_KNL
, "deleting old SA");
767 status
= this->public.del_sa(&this->public, dst
, spi
, protocol
);
776 * Implementation of kernel_interface_t.query_sa.
778 static status_t
query_sa(private_kernel_interface_t
*this, host_t
*dst
,
779 u_int32_t spi
, protocol_id_t protocol
, u_int32_t
*use_time
)
781 unsigned char request
[BUFFER_SIZE
];
782 struct nlmsghdr
*response
;
783 struct nlmsghdr
*hdr
;
784 struct xfrm_usersa_id
*sa_id
;
785 struct xfrm_usersa_info
*sa_info
;
787 DBG2(DBG_KNL
, "querying SA");
788 memset(&request
, 0, sizeof(request
));
790 hdr
= (struct nlmsghdr
*)request
;
791 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
792 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
793 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
795 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
796 host2xfrm(dst
, &sa_id
->daddr
);
798 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
799 sa_id
->family
= dst
->get_family(dst
);
801 if (send_message(this, hdr
, &response
) != SUCCESS
)
803 DBG1(DBG_KNL
, "netlink communication failed");
806 else if (response
->nlmsg_type
!= XFRM_MSG_NEWSA
)
808 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETSA not acknowledged");
812 else if (response
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
)))
814 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETSA got an invalid reply");
819 sa_info
= (struct xfrm_usersa_info
*)NLMSG_DATA(response
);
820 *use_time
= sa_info
->curlft
.use_time
;
827 * Implementation of kernel_interface_t.del_sa.
829 static status_t
del_sa(private_kernel_interface_t
*this, host_t
*dst
,
830 u_int32_t spi
, protocol_id_t protocol
)
832 unsigned char request
[BUFFER_SIZE
];
833 struct nlmsghdr
*response
;
834 struct nlmsghdr
*hdr
;
835 struct xfrm_usersa_id
*sa_id
;
837 memset(&request
, 0, sizeof(request
));
838 status_t status
= SUCCESS
;
840 DBG2(DBG_KNL
, "deleting SA");
842 hdr
= (struct nlmsghdr
*)request
;
843 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
844 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
845 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
847 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
848 host2xfrm(dst
, &sa_id
->daddr
);
850 sa_id
->proto
= (protocol
== PROTO_ESP
) ? KERNEL_ESP
: KERNEL_AH
;
851 sa_id
->family
= dst
->get_family(dst
);
853 if (send_message(this, hdr
, &response
) != SUCCESS
)
855 DBG1(DBG_KNL
, "netlink communication failed");
858 else if (response
->nlmsg_type
!= NLMSG_ERROR
)
860 DBG1(DBG_KNL
, "netlink request XFRM_MSG_DELSA not acknowledged");
863 else if (((struct nlmsgerr
*)NLMSG_DATA(response
))->error
)
865 DBG1(DBG_KNL
, "netlink request XFRM_MSG_DELSA got an error: %s",
866 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
875 * convert a traffic selector address range to subnet and its mask.
877 static void ts2subnet(traffic_selector_t
* ts
,
878 xfrm_address_t
*net
, u_int8_t
*mask
)
880 /* there is no way to do this cleanly, as the address range may
881 * be anything else but a subnet. We use from_addr as subnet
882 * and try to calculate a usable subnet mask.
887 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
889 from
= ts
->get_from_address(ts
);
890 to
= ts
->get_to_address(ts
);
893 /* go trough all bits of the addresses, beginning in the front.
894 * As longer as they equal, the subnet gets larger */
895 for (byte
= 0; byte
< size
; byte
++)
897 for (bit
= 7; bit
>= 0; bit
--)
899 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
901 *mask
= ((7 - bit
) + (byte
* 8));
911 memcpy(net
, from
.ptr
, from
.len
);
917 * convert a traffic selector port range to port/portmask
919 static void ts2ports(traffic_selector_t
* ts
,
920 u_int16_t
*port
, u_int16_t
*mask
)
922 /* linux does not seem to accept complex portmasks. Only
923 * any or a specific port is allowed. We set to any, if we have
924 * a port range, or to a specific, if we have one port only.
928 from
= ts
->get_from_port(ts
);
929 to
= ts
->get_to_port(ts
);
944 * convert a pair of traffic_selectors to a xfrm_selector
946 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
947 traffic_selector_t
*dst
)
949 struct xfrm_selector sel
;
951 memset(&sel
, 0, sizeof(sel
));
952 sel
.family
= src
->get_type(src
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
953 /* src or dest proto may be "any" (0), use more restrictive one */
954 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
955 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
956 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
957 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
958 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
966 * Implementation of kernel_interface_t.add_policy.
968 static status_t
add_policy(private_kernel_interface_t
*this,
969 host_t
*src
, host_t
*dst
,
970 traffic_selector_t
*src_ts
,
971 traffic_selector_t
*dst_ts
,
972 policy_dir_t direction
, protocol_id_t protocol
,
973 u_int32_t reqid
, bool high_prio
, bool update
)
975 iterator_t
*iterator
;
976 kernel_policy_t
*current
, *policy
;
978 unsigned char request
[BUFFER_SIZE
];
979 struct nlmsghdr
*response
;
980 struct xfrm_userpolicy_info
*policy_info
;
981 struct nlmsghdr
*hdr
;
982 status_t status
= SUCCESS
;
984 /* create a policy */
985 policy
= malloc_thing(kernel_policy_t
);
986 memset(policy
, 0, sizeof(kernel_policy_t
));
987 policy
->sel
= ts2selector(src_ts
, dst_ts
);
988 policy
->direction
= direction
;
990 /* find the policy, which matches EXACTLY */
991 pthread_mutex_lock(&this->pol_mutex
);
992 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
993 while (iterator
->iterate(iterator
, (void**)¤t
))
995 if (memcmp(current
, policy
, sizeof(struct xfrm_selector
)) == 0 &&
996 policy
->direction
== current
->direction
)
999 /* use existing policy */
1002 current
->refcount
++;
1003 DBG2(DBG_KNL
, "policy already exists, increasing refcount");
1006 /* if added policy is for a ROUTED child_sa, do not
1007 * overwrite existing INSTALLED policy */
1008 iterator
->destroy(iterator
);
1009 pthread_mutex_unlock(&this->pol_mutex
);
1018 iterator
->destroy(iterator
);
1020 { /* apply the new one, if we have no such policy */
1021 this->policies
->insert_last(this->policies
, policy
);
1022 policy
->refcount
= 1;
1025 DBG2(DBG_KNL
, "adding policy");
1027 memset(&request
, 0, sizeof(request
));
1028 hdr
= (struct nlmsghdr
*)request
;
1029 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1030 hdr
->nlmsg_type
= XFRM_MSG_UPDPOLICY
;
1031 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1033 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1034 policy_info
->sel
= policy
->sel
;
1035 policy_info
->dir
= policy
->direction
;
1036 /* calculate priority based on source selector size, small size = high prio */
1037 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1038 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1039 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1040 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1041 policy_info
->action
= XFRM_POLICY_ALLOW
;
1042 policy_info
->share
= XFRM_SHARE_ANY
;
1043 pthread_mutex_unlock(&this->pol_mutex
);
1045 /* policies don't expire */
1046 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1047 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1048 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1049 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1050 policy_info
->lft
.soft_add_expires_seconds
= 0;
1051 policy_info
->lft
.hard_add_expires_seconds
= 0;
1052 policy_info
->lft
.soft_use_expires_seconds
= 0;
1053 policy_info
->lft
.hard_use_expires_seconds
= 0;
1055 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1056 rthdr
->rta_type
= XFRMA_TMPL
;
1058 rthdr
->rta_len
= sizeof(struct xfrm_user_tmpl
);
1059 rthdr
->rta_len
= RTA_LENGTH(rthdr
->rta_len
);
1061 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1062 if (hdr
->nlmsg_len
> sizeof(request
))
1067 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1068 tmpl
->reqid
= reqid
;
1069 tmpl
->id
.proto
= (protocol
== PROTO_AH
) ? KERNEL_AH
: KERNEL_ESP
;
1070 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1072 tmpl
->family
= src
->get_family(src
);
1074 host2xfrm(src
, &tmpl
->saddr
);
1075 host2xfrm(dst
, &tmpl
->id
.daddr
);
1077 if (send_message(this, hdr
, &response
) != SUCCESS
)
1079 DBG1(DBG_KNL
, "netlink communication failed");
1082 else if (response
->nlmsg_type
!= NLMSG_ERROR
)
1084 DBG1(DBG_KNL
, "netlink request XFRM_MSG_UPDPOLICY not acknowledged");
1087 else if (((struct nlmsgerr
*)NLMSG_DATA(response
))->error
)
1089 DBG1(DBG_KNL
, "netlink request XFRM_MSG_UPDPOLICY got an error: %s",
1090 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
1099 * Implementation of kernel_interface_t.query_policy.
1101 static status_t
query_policy(private_kernel_interface_t
*this,
1102 traffic_selector_t
*src_ts
,
1103 traffic_selector_t
*dst_ts
,
1104 policy_dir_t direction
, u_int32_t
*use_time
)
1106 unsigned char request
[BUFFER_SIZE
];
1107 struct nlmsghdr
*response
;
1108 struct nlmsghdr
*hdr
;
1109 struct xfrm_userpolicy_id
*policy_id
;
1110 struct xfrm_userpolicy_info
*policy
;
1112 memset(&request
, 0, sizeof(request
));
1113 status_t status
= SUCCESS
;
1115 DBG2(DBG_KNL
, "querying policy");
1117 hdr
= (struct nlmsghdr
*)request
;
1118 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1119 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1120 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1122 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1123 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1124 policy_id
->dir
= direction
;
1126 if (send_message(this, hdr
, &response
) != SUCCESS
)
1128 DBG1(DBG_KNL
, "netlink communication failed");
1131 else if (response
->nlmsg_type
== NLMSG_ERROR
)
1133 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETPOLICY got an error: %s",
1134 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
1138 else if (response
->nlmsg_type
!= XFRM_MSG_NEWPOLICY
)
1140 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETPOLICY got an unknown reply");
1144 else if (response
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
)))
1146 DBG1(DBG_KNL
, "netlink request XFRM_MSG_GETPOLICY got an invalid reply");
1151 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(response
);
1153 *use_time
= (time_t)policy
->curlft
.use_time
;
1160 * Implementation of kernel_interface_t.del_policy.
1162 static status_t
del_policy(private_kernel_interface_t
*this,
1163 traffic_selector_t
*src_ts
,
1164 traffic_selector_t
*dst_ts
,
1165 policy_dir_t direction
)
1167 kernel_policy_t
*current
, policy
, *to_delete
= NULL
;
1168 unsigned char request
[BUFFER_SIZE
];
1169 struct nlmsghdr
*response
;
1170 struct nlmsghdr
*hdr
;
1171 struct xfrm_userpolicy_id
*policy_id
;
1172 iterator_t
*iterator
;
1173 status_t status
= SUCCESS
;
1175 DBG2(DBG_KNL
, "deleting policy");
1177 /* create a policy */
1178 memset(&policy
, 0, sizeof(kernel_policy_t
));
1179 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1180 policy
.direction
= direction
;
1182 /* find the policy */
1183 pthread_mutex_lock(&this->pol_mutex
);
1184 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1185 while (iterator
->iterate(iterator
, (void**)¤t
))
1187 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
1188 policy
.direction
== current
->direction
)
1190 to_delete
= current
;
1191 if (--to_delete
->refcount
> 0)
1193 /* is used by more SAs, keep in kernel */
1194 DBG2(DBG_KNL
, "is used by other SAs, not removed");
1195 iterator
->destroy(iterator
);
1196 pthread_mutex_unlock(&this->pol_mutex
);
1199 /* remove if last reference */
1200 iterator
->remove(iterator
);
1204 iterator
->destroy(iterator
);
1205 pthread_mutex_unlock(&this->pol_mutex
);
1208 DBG1(DBG_KNL
, "no such policy found");
1212 memset(&request
, 0, sizeof(request
));
1214 hdr
= (struct nlmsghdr
*)request
;
1215 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1216 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1217 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1219 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1220 policy_id
->sel
= to_delete
->sel
;
1221 policy_id
->dir
= direction
;
1225 if (send_message(this, hdr
, &response
) != SUCCESS
)
1227 DBG1(DBG_KNL
, "netlink communication failed");
1230 else if (response
->nlmsg_type
!= NLMSG_ERROR
)
1232 DBG1(DBG_KNL
, "netlink request XFRM_MSG_DELPOLICY not acknowledged");
1235 else if (((struct nlmsgerr
*)NLMSG_DATA(response
))->error
)
1237 DBG1(DBG_KNL
, "netlink request XFRM_MSG_DELPOLICY got an error: %s",
1238 strerror(-((struct nlmsgerr
*)NLMSG_DATA(response
))->error
));
1247 * Implementation of kernel_interface_t.destroy.
1249 static void destroy(private_kernel_interface_t
*this)
1251 pthread_cancel(this->thread
);
1252 pthread_join(this->thread
, NULL
);
1253 close(this->socket
);
1254 this->responses
->destroy(this->responses
);
1255 this->policies
->destroy(this->policies
);
1260 * Described in header.
1262 kernel_interface_t
*kernel_interface_create()
1264 struct sockaddr_nl addr
;
1265 private_kernel_interface_t
*this = malloc_thing(private_kernel_interface_t
);
1267 /* public functions */
1268 this->public.get_spi
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
1269 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
;
1270 this->public.update_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_diff_t
,host_diff_t
))update_sa
;
1271 this->public.query_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
1272 this->public.del_sa
= (status_t(*)(kernel_interface_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
1273 this->public.add_policy
= (status_t(*)(kernel_interface_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,bool))add_policy
;
1274 this->public.query_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1275 this->public.del_policy
= (status_t(*)(kernel_interface_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
1276 this->public.destroy
= (void(*)(kernel_interface_t
*)) destroy
;
1278 /* private members */
1279 this->pid
= getpid();
1280 this->responses
= linked_list_create();
1281 this->policies
= linked_list_create();
1282 pthread_mutex_init(&(this->rep_mutex
),NULL
);
1283 pthread_mutex_init(&(this->pol_mutex
),NULL
);
1284 pthread_cond_init(&(this->condvar
),NULL
);
1287 /* open netlink socket */
1288 this->socket
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1289 if (this->socket
<= 0)
1291 this->responses
->destroy(this->responses
);
1293 charon
->kill(charon
, "Unable to create netlink socket");
1296 /* bind the socket and reqister for ACQUIRE & EXPIRE */
1297 addr
.nl_family
= AF_NETLINK
;
1298 addr
.nl_pid
= getpid();
1299 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
1300 if (bind(this->socket
, (struct sockaddr
*)&addr
, sizeof(addr
)) != 0)
1302 this->responses
->destroy(this->responses
);
1303 close(this->socket
);
1305 charon
->kill(charon
, "Unable to bind netlink socket");
1308 if (pthread_create(&this->thread
, NULL
, (void*(*)(void*))receive_messages
, this) != 0)
1310 this->responses
->destroy(this->responses
);
1311 close(this->socket
);
1313 charon
->kill(charon
, "Unable to create netlink thread");
1316 return &this->public;