2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2007 Martin Willi
4 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
5 * Copyright (C) 2006 Daniel Roethlisberger
6 * Copyright (C) 2005 Jan Hutter
7 * Hochschule fuer Technik Rapperswil
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/xfrm.h>
28 #include <linux/udp.h>
29 #include <netinet/in.h>
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
39 #include <utils/linked_list.h>
40 #include <processing/jobs/callback_job.h>
41 #include <processing/jobs/acquire_job.h>
42 #include <processing/jobs/rekey_child_sa_job.h>
43 #include <processing/jobs/delete_child_sa_job.h>
45 /** required for Linux 2.6.26 kernel and later */
46 #ifndef XFRM_STATE_AF_UNSPEC
47 #define XFRM_STATE_AF_UNSPEC 32
50 /** default priority of installed policies */
52 #define PRIO_HIGH 2000
55 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
56 * 'usual' netlink data x like 'struct xfrm_usersa_info'
58 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
60 * returns a pointer to the next rtattr following rta.
61 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
63 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
65 * returns the total size of attached rta data
66 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
68 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
70 typedef struct kernel_algorithm_t kernel_algorithm_t
;
73 * Mapping from the algorithms defined in IKEv2 to
74 * kernel level algorithm names and their key length
76 struct kernel_algorithm_t
{
78 * Identifier specified in IKEv2
83 * Name of the algorithm, as used as kernel identifier
88 * Key length in bits, if fixed size
93 ENUM(policy_dir_names
, POLICY_IN
, POLICY_FWD
,
99 #define END_OF_LIST -1
102 * Algorithms for encryption
104 static kernel_algorithm_t encryption_algs
[] = {
105 /* {ENCR_DES_IV64, "***", 0}, */
106 {ENCR_DES
, "des", 64},
107 {ENCR_3DES
, "des3_ede", 192},
108 /* {ENCR_RC5, "***", 0}, */
109 /* {ENCR_IDEA, "***", 0}, */
110 {ENCR_CAST
, "cast128", 0},
111 {ENCR_BLOWFISH
, "blowfish", 0},
112 /* {ENCR_3IDEA, "***", 0}, */
113 /* {ENCR_DES_IV32, "***", 0}, */
114 {ENCR_NULL
, "cipher_null", 0},
115 {ENCR_AES_CBC
, "aes", 0},
116 /* {ENCR_AES_CTR, "***", 0}, */
117 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))", 64}, /* key_size = ICV size */
118 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))", 96}, /* key_size = ICV size */
119 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))", 128}, /* key_size = ICV size */
120 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))", 64}, /* key_size = ICV size */
121 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))", 96}, /* key_size = ICV size */
122 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))", 128}, /* key_size = ICV size */
123 {END_OF_LIST
, NULL
, 0},
127 * Algorithms for integrity protection
129 static kernel_algorithm_t integrity_algs
[] = {
130 {AUTH_HMAC_MD5_96
, "md5", 128},
131 {AUTH_HMAC_SHA1_96
, "sha1", 160},
132 {AUTH_HMAC_SHA2_256_128
, "sha256", 256},
133 {AUTH_HMAC_SHA2_384_192
, "sha384", 384},
134 {AUTH_HMAC_SHA2_512_256
, "sha512", 512},
135 /* {AUTH_DES_MAC, "***", 0}, */
136 /* {AUTH_KPDK_MD5, "***", 0}, */
137 {AUTH_AES_XCBC_96
, "xcbc(aes)", 128},
138 {END_OF_LIST
, NULL
, 0},
142 * Algorithms for IPComp
144 static kernel_algorithm_t compression_algs
[] = {
145 /* {IPCOMP_OUI, "***", 0}, */
146 {IPCOMP_DEFLATE
, "deflate", 0},
147 {IPCOMP_LZS
, "lzs", 0},
148 {IPCOMP_LZJH
, "lzjh", 0},
149 {END_OF_LIST
, NULL
, 0},
153 * Look up a kernel algorithm name and its key size
155 static char* lookup_algorithm(kernel_algorithm_t
*kernel_algo
,
156 u_int16_t ikev2_algo
, u_int16_t
*key_size
)
158 while (kernel_algo
->ikev2_id
!= END_OF_LIST
)
160 if (ikev2_algo
== kernel_algo
->ikev2_id
)
162 /* match, evaluate key length */
163 if (key_size
&& *key_size
== 0)
164 { /* update key size if not set */
165 *key_size
= kernel_algo
->key_size
;
167 return kernel_algo
->name
;
174 typedef struct route_entry_t route_entry_t
;
177 * installed routing entry
179 struct route_entry_t
{
180 /** Name of the interface the route is bound to */
183 /** Source ip of the route */
186 /** gateway for this route */
189 /** Destination net */
192 /** Destination net prefixlen */
197 * destroy an route_entry_t object
199 static void route_entry_destroy(route_entry_t
*this)
202 this->src_ip
->destroy(this->src_ip
);
203 this->gateway
->destroy(this->gateway
);
204 chunk_free(&this->dst_net
);
208 typedef struct policy_entry_t policy_entry_t
;
211 * installed kernel policy.
213 struct policy_entry_t
{
215 /** direction of this policy: in, out, forward */
218 /** reqid of the policy */
221 /** parameters of installed policy */
222 struct xfrm_selector sel
;
224 /** associated route installed for this policy */
225 route_entry_t
*route
;
227 /** by how many CHILD_SA's this policy is used */
231 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
234 * Private variables and functions of kernel_netlink class.
236 struct private_kernel_netlink_ipsec_t
{
238 * Public part of the kernel_netlink_t object.
240 kernel_netlink_ipsec_t
public;
243 * mutex to lock access to various lists
245 pthread_mutex_t mutex
;
248 * List of installed policies (policy_entry_t)
250 linked_list_t
*policies
;
253 * job receiving netlink events
258 * Netlink xfrm socket (IPsec)
260 netlink_socket_t
*socket_xfrm
;
263 * netlink xfrm socket to receive acquire and expire events
265 int socket_xfrm_events
;
268 * whether to install routes along policies
274 * convert a IKEv2 specific protocol identifier to the kernel one
276 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
290 * reverse of ike2kernel
292 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
306 * convert a host_t to a struct xfrm_address
308 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
310 chunk_t chunk
= host
->get_address(host
);
311 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
315 * convert a traffic selector address range to subnet and its mask.
317 static void ts2subnet(traffic_selector_t
* ts
,
318 xfrm_address_t
*net
, u_int8_t
*mask
)
320 /* there is no way to do this cleanly, as the address range may
321 * be anything else but a subnet. We use from_addr as subnet
322 * and try to calculate a usable subnet mask.
327 size_t size
= (ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE
) ?
4 : 16;
329 from
= ts
->get_from_address(ts
);
330 to
= ts
->get_to_address(ts
);
333 /* go trough all bits of the addresses, beginning in the front.
334 * as long as they are equal, the subnet gets larger
336 for (byte
= 0; byte
< size
; byte
++)
338 for (bit
= 7; bit
>= 0; bit
--)
340 if ((1<<bit
& from
.ptr
[byte
]) != (1<<bit
& to
.ptr
[byte
]))
342 *mask
= ((7 - bit
) + (byte
* 8));
352 memcpy(net
, from
.ptr
, from
.len
);
358 * convert a traffic selector port range to port/portmask
360 static void ts2ports(traffic_selector_t
* ts
,
361 u_int16_t
*port
, u_int16_t
*mask
)
363 /* linux does not seem to accept complex portmasks. Only
364 * any or a specific port is allowed. We set to any, if we have
365 * a port range, or to a specific, if we have one port only.
369 from
= ts
->get_from_port(ts
);
370 to
= ts
->get_to_port(ts
);
385 * convert a pair of traffic_selectors to a xfrm_selector
387 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
388 traffic_selector_t
*dst
)
390 struct xfrm_selector sel
;
392 memset(&sel
, 0, sizeof(sel
));
393 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
394 /* src or dest proto may be "any" (0), use more restrictive one */
395 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
396 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
397 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
398 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
399 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
408 * process a XFRM_MSG_ACQUIRE from kernel
410 static void process_acquire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
415 struct rtattr
*rtattr
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
416 size_t rtsize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_tmpl
);
418 if (RTA_OK(rtattr
, rtsize
))
420 if (rtattr
->rta_type
== XFRMA_TMPL
)
422 struct xfrm_user_tmpl
* tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rtattr
);
424 proto
= tmpl
->id
.proto
;
434 /* acquire for AH/ESP only, not for IPCOMP */
439 DBG1(DBG_KNL
, "received a XFRM_MSG_ACQUIRE, but no reqid found");
442 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
443 DBG1(DBG_KNL
, "creating acquire job for CHILD_SA with reqid {%d}", reqid
);
444 job
= (job_t
*)acquire_job_create(reqid
);
445 charon
->processor
->queue_job(charon
->processor
, job
);
449 * process a XFRM_MSG_EXPIRE from kernel
451 static void process_expire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
454 protocol_id_t protocol
;
455 u_int32_t spi
, reqid
;
456 struct xfrm_user_expire
*expire
;
458 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
459 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
460 spi
= expire
->state
.id
.spi
;
461 reqid
= expire
->state
.reqid
;
463 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
465 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
467 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%d} "
468 "which is not a CHILD_SA", ntohl(spi
), reqid
);
472 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
473 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
474 protocol
, ntohl(spi
), reqid
);
477 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
481 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
483 charon
->processor
->queue_job(charon
->processor
, job
);
487 * Receives events from kernel
489 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
492 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
493 struct sockaddr_nl addr
;
494 socklen_t addr_len
= sizeof(addr
);
497 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
498 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
499 (struct sockaddr
*)&addr
, &addr_len
);
500 pthread_setcancelstate(oldstate
, NULL
);
507 /* interrupted, try again */
508 return JOB_REQUEUE_DIRECT
;
510 /* no data ready, select again */
511 return JOB_REQUEUE_DIRECT
;
513 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
515 return JOB_REQUEUE_FAIR
;
519 if (addr
.nl_pid
!= 0)
520 { /* not from kernel. not interested, try another one */
521 return JOB_REQUEUE_DIRECT
;
524 while (NLMSG_OK(hdr
, len
))
526 switch (hdr
->nlmsg_type
)
528 case XFRM_MSG_ACQUIRE
:
529 process_acquire(this, hdr
);
531 case XFRM_MSG_EXPIRE
:
532 process_expire(this, hdr
);
537 hdr
= NLMSG_NEXT(hdr
, len
);
539 return JOB_REQUEUE_DIRECT
;
543 * Tries to find an ip address of a local interface that is included in the
544 * supplied traffic selector.
546 static status_t
get_address_by_ts(private_kernel_netlink_ipsec_t
*this,
547 traffic_selector_t
*ts
, host_t
**ip
)
554 DBG2(DBG_KNL
, "getting a local address in traffic selector %R", ts
);
556 /* if we have a family which includes localhost, we do not
557 * search for an IP, we use the default */
558 family
= ts
->get_type(ts
) == TS_IPV4_ADDR_RANGE ? AF_INET
: AF_INET6
;
560 if (family
== AF_INET
)
562 host
= host_create_from_string("127.0.0.1", 0);
566 host
= host_create_from_string("::1", 0);
569 if (ts
->includes(ts
, host
))
571 *ip
= host_create_any(family
);
573 DBG2(DBG_KNL
, "using host %H", *ip
);
578 addrs
= charon
->kernel_interface
->create_address_enumerator(
579 charon
->kernel_interface
, TRUE
, TRUE
);
580 while (addrs
->enumerate(addrs
, (void**)&host
))
582 if (ts
->includes(ts
, host
))
585 *ip
= host
->clone(host
);
589 addrs
->destroy(addrs
);
593 DBG1(DBG_KNL
, "no local address found in traffic selector %R", ts
);
596 DBG2(DBG_KNL
, "using host %H", *ip
);
601 * Get an SPI for a specific protocol from the kernel.
603 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
604 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
605 u_int32_t reqid
, u_int32_t
*spi
)
607 unsigned char request
[NETLINK_BUFFER_SIZE
];
608 struct nlmsghdr
*hdr
, *out
;
609 struct xfrm_userspi_info
*userspi
;
610 u_int32_t received_spi
= 0;
613 memset(&request
, 0, sizeof(request
));
615 hdr
= (struct nlmsghdr
*)request
;
616 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
617 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
618 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
620 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
621 host2xfrm(src
, &userspi
->info
.saddr
);
622 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
623 userspi
->info
.id
.proto
= proto
;
624 userspi
->info
.mode
= TRUE
; /* tunnel mode */
625 userspi
->info
.reqid
= reqid
;
626 userspi
->info
.family
= src
->get_family(src
);
630 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
633 while (NLMSG_OK(hdr
, len
))
635 switch (hdr
->nlmsg_type
)
639 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
640 received_spi
= usersa
->id
.spi
;
645 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
647 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
648 strerror(-err
->error
), -err
->error
);
652 hdr
= NLMSG_NEXT(hdr
, len
);
662 if (received_spi
== 0)
672 * Implementation of kernel_interface_t.get_spi.
674 static status_t
get_spi(private_kernel_netlink_ipsec_t
*this,
675 host_t
*src
, host_t
*dst
,
676 protocol_id_t protocol
, u_int32_t reqid
,
679 DBG2(DBG_KNL
, "getting SPI for reqid {%d}", reqid
);
681 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
682 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
684 DBG1(DBG_KNL
, "unable to get SPI for reqid {%d}", reqid
);
688 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%d}", ntohl(*spi
), reqid
);
694 * Implementation of kernel_interface_t.get_cpi.
696 static status_t
get_cpi(private_kernel_netlink_ipsec_t
*this,
697 host_t
*src
, host_t
*dst
,
698 u_int32_t reqid
, u_int16_t
*cpi
)
700 u_int32_t received_spi
= 0;
702 DBG2(DBG_KNL
, "getting CPI for reqid {%d}", reqid
);
704 if (get_spi_internal(this, src
, dst
,
705 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
707 DBG1(DBG_KNL
, "unable to get CPI for reqid {%d}", reqid
);
711 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
713 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%d}", ntohs(*cpi
), reqid
);
719 * Implementation of kernel_interface_t.add_sa.
721 static status_t
add_sa(private_kernel_netlink_ipsec_t
*this,
722 host_t
*src
, host_t
*dst
, u_int32_t spi
,
723 protocol_id_t protocol
, u_int32_t reqid
,
724 u_int64_t expire_soft
, u_int64_t expire_hard
,
725 u_int16_t enc_alg
, u_int16_t enc_size
,
726 u_int16_t int_alg
, u_int16_t int_size
,
727 prf_plus_t
*prf_plus
, mode_t mode
,
728 u_int16_t ipcomp
, bool encap
,
731 unsigned char request
[NETLINK_BUFFER_SIZE
];
733 /* additional 4 octets KEYMAT required for AES-GCM as of RFC4106 8.1. */
734 u_int16_t add_keymat
= 32;
735 struct nlmsghdr
*hdr
;
736 struct xfrm_usersa_info
*sa
;
738 memset(&request
, 0, sizeof(request
));
740 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%d}", ntohl(spi
), reqid
);
742 hdr
= (struct nlmsghdr
*)request
;
743 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
744 hdr
->nlmsg_type
= replace ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
745 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
747 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
748 host2xfrm(src
, &sa
->saddr
);
749 host2xfrm(dst
, &sa
->id
.daddr
);
751 sa
->id
.proto
= proto_ike2kernel(protocol
);
752 sa
->family
= src
->get_family(src
);
754 if (mode
== MODE_TUNNEL
)
756 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
758 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
760 /* we currently do not expire SAs by volume/packet count */
761 sa
->lft
.soft_byte_limit
= XFRM_INF
;
762 sa
->lft
.hard_byte_limit
= XFRM_INF
;
763 sa
->lft
.soft_packet_limit
= XFRM_INF
;
764 sa
->lft
.hard_packet_limit
= XFRM_INF
;
765 /* we use lifetimes since added, not since used */
766 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
767 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
768 sa
->lft
.soft_use_expires_seconds
= 0;
769 sa
->lft
.hard_use_expires_seconds
= 0;
771 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
778 case ENCR_AES_CCM_ICV8
:
779 case ENCR_AES_CCM_ICV12
:
780 case ENCR_AES_CCM_ICV16
:
781 /* AES-CCM needs only 3 additional octets KEYMAT as of RFC 4309 7.1. */
784 case ENCR_AES_GCM_ICV8
:
785 case ENCR_AES_GCM_ICV12
:
786 case ENCR_AES_GCM_ICV16
:
788 u_int16_t icv_size
= 0;
789 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
790 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &icv_size
);
791 if (alg_name
== NULL
)
793 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
794 encryption_algorithm_names
, enc_alg
);
797 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
798 encryption_algorithm_names
, enc_alg
, enc_size
);
800 /* additional KEYMAT required */
801 enc_size
+= add_keymat
;
803 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_size
/ 8);
804 hdr
->nlmsg_len
+= rthdr
->rta_len
;
805 if (hdr
->nlmsg_len
> sizeof(request
))
810 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
811 algo
->alg_key_len
= enc_size
;
812 algo
->alg_icv_len
= icv_size
;
813 strcpy(algo
->alg_name
, alg_name
);
814 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
816 rthdr
= XFRM_RTA_NEXT(rthdr
);
821 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
822 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
, &enc_size
);
823 if (alg_name
== NULL
)
825 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
826 encryption_algorithm_names
, enc_alg
);
829 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
830 encryption_algorithm_names
, enc_alg
, enc_size
);
832 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_size
/ 8);
833 hdr
->nlmsg_len
+= rthdr
->rta_len
;
834 if (hdr
->nlmsg_len
> sizeof(request
))
839 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
840 algo
->alg_key_len
= enc_size
;
841 strcpy(algo
->alg_name
, alg_name
);
842 prf_plus
->get_bytes(prf_plus
, enc_size
/ 8, algo
->alg_key
);
844 rthdr
= XFRM_RTA_NEXT(rthdr
);
849 if (int_alg
!= AUTH_UNDEFINED
)
851 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
852 alg_name
= lookup_algorithm(integrity_algs
, int_alg
, &int_size
);
853 if (alg_name
== NULL
)
855 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
856 integrity_algorithm_names
, int_alg
);
859 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
860 integrity_algorithm_names
, int_alg
, int_size
);
862 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_size
/ 8);
863 hdr
->nlmsg_len
+= rthdr
->rta_len
;
864 if (hdr
->nlmsg_len
> sizeof(request
))
869 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
870 algo
->alg_key_len
= int_size
;
871 strcpy(algo
->alg_name
, alg_name
);
872 prf_plus
->get_bytes(prf_plus
, int_size
/ 8, algo
->alg_key
);
874 rthdr
= XFRM_RTA_NEXT(rthdr
);
877 if (ipcomp
!= IPCOMP_NONE
)
879 rthdr
->rta_type
= XFRMA_ALG_COMP
;
880 alg_name
= lookup_algorithm(compression_algs
, ipcomp
, NULL
);
881 if (alg_name
== NULL
)
883 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
884 ipcomp_transform_names
, ipcomp
);
887 DBG2(DBG_KNL
, " using compression algorithm %N",
888 ipcomp_transform_names
, ipcomp
);
890 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
891 hdr
->nlmsg_len
+= rthdr
->rta_len
;
892 if (hdr
->nlmsg_len
> sizeof(request
))
897 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
898 algo
->alg_key_len
= 0;
899 strcpy(algo
->alg_name
, alg_name
);
901 rthdr
= XFRM_RTA_NEXT(rthdr
);
906 rthdr
->rta_type
= XFRMA_ENCAP
;
907 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
909 hdr
->nlmsg_len
+= rthdr
->rta_len
;
910 if (hdr
->nlmsg_len
> sizeof(request
))
915 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
916 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
917 tmpl
->encap_sport
= htons(src
->get_port(src
));
918 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
919 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
920 /* encap_oa could probably be derived from the
921 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
922 * pluto does the same as we do here but it uses encap_oa in the
923 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
924 * the kernel ignores it anyway
925 * -> does that mean that NAT-T encap doesn't work in transport mode?
926 * No. The reason the kernel ignores NAT-OA is that it recomputes
927 * (or, rather, just ignores) the checksum. If packets pass
928 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
929 rthdr
= XFRM_RTA_NEXT(rthdr
);
932 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
934 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
941 * Get the replay state (i.e. sequence numbers) of an SA.
943 static status_t
get_replay_state(private_kernel_netlink_ipsec_t
*this,
944 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
945 struct xfrm_replay_state
*replay
)
947 unsigned char request
[NETLINK_BUFFER_SIZE
];
948 struct nlmsghdr
*hdr
, *out
= NULL
;
949 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
954 memset(&request
, 0, sizeof(request
));
956 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x", ntohl(spi
));
958 hdr
= (struct nlmsghdr
*)request
;
959 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
960 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
961 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
963 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
964 aevent_id
->flags
= XFRM_AE_RVAL
;
966 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
967 aevent_id
->sa_id
.spi
= spi
;
968 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
969 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
971 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
974 while (NLMSG_OK(hdr
, len
))
976 switch (hdr
->nlmsg_type
)
980 out_aevent
= NLMSG_DATA(hdr
);
985 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
986 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
987 strerror(-err
->error
), -err
->error
);
991 hdr
= NLMSG_NEXT(hdr
, len
);
1000 if (out_aevent
== NULL
)
1002 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1008 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1009 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1010 while(RTA_OK(rta
, rtasize
))
1012 if (rta
->rta_type
== XFRMA_REPLAY_VAL
)
1014 memcpy(replay
, RTA_DATA(rta
), rta
->rta_len
);
1018 rta
= RTA_NEXT(rta
, rtasize
);
1021 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1028 * Implementation of kernel_interface_t.update_sa.
1030 static status_t
update_sa(private_kernel_netlink_ipsec_t
*this,
1031 u_int32_t spi
, protocol_id_t protocol
,
1032 host_t
*src
, host_t
*dst
,
1033 host_t
*new_src
, host_t
*new_dst
, bool encap
)
1035 unsigned char request
[NETLINK_BUFFER_SIZE
], *pos
;
1036 struct nlmsghdr
*hdr
, *out
= NULL
;
1037 struct xfrm_usersa_id
*sa_id
;
1038 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1042 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1043 bool got_replay_state
;
1044 struct xfrm_replay_state replay
;
1046 memset(&request
, 0, sizeof(request
));
1048 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1050 /* query the existing SA first */
1051 hdr
= (struct nlmsghdr
*)request
;
1052 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1053 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1054 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1056 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1057 host2xfrm(dst
, &sa_id
->daddr
);
1059 sa_id
->proto
= proto_ike2kernel(protocol
);
1060 sa_id
->family
= dst
->get_family(dst
);
1062 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1065 while (NLMSG_OK(hdr
, len
))
1067 switch (hdr
->nlmsg_type
)
1069 case XFRM_MSG_NEWSA
:
1071 out_sa
= NLMSG_DATA(hdr
);
1076 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1077 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1078 strerror(-err
->error
), -err
->error
);
1082 hdr
= NLMSG_NEXT(hdr
, len
);
1092 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1097 /* try to get the replay state */
1098 got_replay_state
= (get_replay_state(
1099 this, spi
, protocol
, dst
, &replay
) == SUCCESS
);
1101 /* delete the old SA */
1102 if (this->public.interface
.del_sa(&this->public.interface
, dst
, spi
, protocol
) != SUCCESS
)
1104 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1109 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1110 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1112 /* copy over the SA from out to request */
1113 hdr
= (struct nlmsghdr
*)request
;
1114 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1115 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1116 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1117 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1118 sa
= NLMSG_DATA(hdr
);
1119 sa
->family
= new_dst
->get_family(new_dst
);
1121 if (!src
->ip_equals(src
, new_src
))
1123 host2xfrm(new_src
, &sa
->saddr
);
1125 if (!dst
->ip_equals(dst
, new_dst
))
1127 host2xfrm(new_dst
, &sa
->id
.daddr
);
1130 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1131 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1132 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1133 while(RTA_OK(rta
, rtasize
))
1135 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1136 if (rta
->rta_type
!= XFRMA_ENCAP
|| encap
)
1138 if (rta
->rta_type
== XFRMA_ENCAP
)
1139 { /* update encap tmpl */
1140 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1141 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1142 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1144 memcpy(pos
, rta
, rta
->rta_len
);
1145 pos
+= RTA_ALIGN(rta
->rta_len
);
1146 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1148 rta
= RTA_NEXT(rta
, rtasize
);
1151 rta
= (struct rtattr
*)pos
;
1152 if (tmpl
== NULL
&& encap
)
1153 { /* add tmpl if we are enabling it */
1154 rta
->rta_type
= XFRMA_ENCAP
;
1155 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1157 hdr
->nlmsg_len
+= rta
->rta_len
;
1158 if (hdr
->nlmsg_len
> sizeof(request
))
1163 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1164 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1165 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1166 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1167 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1169 rta
= XFRM_RTA_NEXT(rta
);
1172 if (got_replay_state
)
1173 { /* copy the replay data if available */
1174 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1175 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1177 hdr
->nlmsg_len
+= rta
->rta_len
;
1178 if (hdr
->nlmsg_len
> sizeof(request
))
1182 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
1184 rta
= XFRM_RTA_NEXT(rta
);
1187 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1189 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1199 * Implementation of kernel_interface_t.query_sa.
1201 static status_t
query_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1202 u_int32_t spi
, protocol_id_t protocol
,
1203 u_int32_t
*use_time
)
1205 unsigned char request
[NETLINK_BUFFER_SIZE
];
1206 struct nlmsghdr
*out
= NULL
, *hdr
;
1207 struct xfrm_usersa_id
*sa_id
;
1208 struct xfrm_usersa_info
*sa
= NULL
;
1211 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1212 memset(&request
, 0, sizeof(request
));
1214 hdr
= (struct nlmsghdr
*)request
;
1215 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1216 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1217 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1219 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1220 host2xfrm(dst
, &sa_id
->daddr
);
1222 sa_id
->proto
= proto_ike2kernel(protocol
);
1223 sa_id
->family
= dst
->get_family(dst
);
1225 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1228 while (NLMSG_OK(hdr
, len
))
1230 switch (hdr
->nlmsg_type
)
1232 case XFRM_MSG_NEWSA
:
1234 sa
= NLMSG_DATA(hdr
);
1239 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1240 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1241 strerror(-err
->error
), -err
->error
);
1245 hdr
= NLMSG_NEXT(hdr
, len
);
1256 DBG1(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1261 *use_time
= sa
->curlft
.use_time
;
1267 * Implementation of kernel_interface_t.del_sa.
1269 static status_t
del_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1270 u_int32_t spi
, protocol_id_t protocol
)
1272 unsigned char request
[NETLINK_BUFFER_SIZE
];
1273 struct nlmsghdr
*hdr
;
1274 struct xfrm_usersa_id
*sa_id
;
1276 memset(&request
, 0, sizeof(request
));
1278 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1280 hdr
= (struct nlmsghdr
*)request
;
1281 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1282 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1283 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1285 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1286 host2xfrm(dst
, &sa_id
->daddr
);
1288 sa_id
->proto
= proto_ike2kernel(protocol
);
1289 sa_id
->family
= dst
->get_family(dst
);
1291 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1293 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1296 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1301 * Implementation of kernel_interface_t.add_policy.
1303 static status_t
add_policy(private_kernel_netlink_ipsec_t
*this,
1304 host_t
*src
, host_t
*dst
,
1305 traffic_selector_t
*src_ts
,
1306 traffic_selector_t
*dst_ts
,
1307 policy_dir_t direction
, protocol_id_t protocol
,
1308 u_int32_t reqid
, bool high_prio
, mode_t mode
,
1311 iterator_t
*iterator
;
1312 policy_entry_t
*current
, *policy
;
1314 unsigned char request
[NETLINK_BUFFER_SIZE
];
1315 struct xfrm_userpolicy_info
*policy_info
;
1316 struct nlmsghdr
*hdr
;
1318 /* create a policy */
1319 policy
= malloc_thing(policy_entry_t
);
1320 memset(policy
, 0, sizeof(policy_entry_t
));
1321 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1322 policy
->direction
= direction
;
1324 /* find the policy, which matches EXACTLY */
1325 pthread_mutex_lock(&this->mutex
);
1326 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1327 while (iterator
->iterate(iterator
, (void**)¤t
))
1329 if (memeq(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) &&
1330 policy
->direction
== current
->direction
)
1332 /* use existing policy */
1333 current
->refcount
++;
1334 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1335 "refcount", src_ts
, dst_ts
,
1336 policy_dir_names
, direction
);
1343 iterator
->destroy(iterator
);
1345 { /* apply the new one, if we have no such policy */
1346 this->policies
->insert_last(this->policies
, policy
);
1347 policy
->refcount
= 1;
1350 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1351 policy_dir_names
, direction
);
1353 memset(&request
, 0, sizeof(request
));
1354 hdr
= (struct nlmsghdr
*)request
;
1355 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1356 hdr
->nlmsg_type
= found ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1357 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1359 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1360 policy_info
->sel
= policy
->sel
;
1361 policy_info
->dir
= policy
->direction
;
1362 /* calculate priority based on source selector size, small size = high prio */
1363 policy_info
->priority
= high_prio ? PRIO_HIGH
: PRIO_LOW
;
1364 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1365 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1366 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1367 policy_info
->action
= XFRM_POLICY_ALLOW
;
1368 policy_info
->share
= XFRM_SHARE_ANY
;
1369 pthread_mutex_unlock(&this->mutex
);
1371 /* policies don't expire */
1372 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1373 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1374 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1375 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1376 policy_info
->lft
.soft_add_expires_seconds
= 0;
1377 policy_info
->lft
.hard_add_expires_seconds
= 0;
1378 policy_info
->lft
.soft_use_expires_seconds
= 0;
1379 policy_info
->lft
.hard_use_expires_seconds
= 0;
1381 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1382 rthdr
->rta_type
= XFRMA_TMPL
;
1383 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1385 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1386 if (hdr
->nlmsg_len
> sizeof(request
))
1391 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1393 if (ipcomp
!= IPCOMP_NONE
)
1395 tmpl
->reqid
= reqid
;
1396 tmpl
->id
.proto
= IPPROTO_COMP
;
1397 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1399 tmpl
->optional
= direction
!= POLICY_OUT
;
1400 tmpl
->family
= src
->get_family(src
);
1402 host2xfrm(src
, &tmpl
->saddr
);
1403 host2xfrm(dst
, &tmpl
->id
.daddr
);
1405 /* add an additional xfrm_user_tmpl */
1406 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1407 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1408 if (hdr
->nlmsg_len
> sizeof(request
))
1416 tmpl
->reqid
= reqid
;
1417 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
1418 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1420 tmpl
->family
= src
->get_family(src
);
1422 host2xfrm(src
, &tmpl
->saddr
);
1423 host2xfrm(dst
, &tmpl
->id
.daddr
);
1425 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1427 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1428 policy_dir_names
, direction
);
1432 /* install a route, if:
1433 * - we are NOT updating a policy
1434 * - this is a forward policy (to just get one for each child)
1435 * - we are in tunnel mode
1436 * - we are not using IPv6 (does not work correctly yet!)
1437 * - routing is not disabled via strongswan.conf
1439 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1440 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1441 this->install_routes
)
1443 route_entry_t
*route
= malloc_thing(route_entry_t
);
1445 if (get_address_by_ts(this, dst_ts
, &route
->src_ip
) == SUCCESS
)
1447 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1448 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1449 charon
->kernel_interface
, src
);
1450 route
->if_name
= charon
->kernel_interface
->get_interface(
1451 charon
->kernel_interface
, dst
);
1452 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1453 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
1454 route
->prefixlen
= policy
->sel
.prefixlen_s
;
1456 switch (charon
->kernel_interface
->add_route(charon
->kernel_interface
,
1457 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1458 route
->src_ip
, route
->if_name
))
1461 DBG1(DBG_KNL
, "unable to install source route for %H",
1465 /* route exists, do not uninstall */
1466 route_entry_destroy(route
);
1469 /* cache the installed route */
1470 policy
->route
= route
;
1484 * Implementation of kernel_interface_t.query_policy.
1486 static status_t
query_policy(private_kernel_netlink_ipsec_t
*this,
1487 traffic_selector_t
*src_ts
,
1488 traffic_selector_t
*dst_ts
,
1489 policy_dir_t direction
, u_int32_t
*use_time
)
1491 unsigned char request
[NETLINK_BUFFER_SIZE
];
1492 struct nlmsghdr
*out
= NULL
, *hdr
;
1493 struct xfrm_userpolicy_id
*policy_id
;
1494 struct xfrm_userpolicy_info
*policy
= NULL
;
1497 memset(&request
, 0, sizeof(request
));
1499 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1500 policy_dir_names
, direction
);
1502 hdr
= (struct nlmsghdr
*)request
;
1503 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1504 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1505 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1507 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1508 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1509 policy_id
->dir
= direction
;
1511 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1514 while (NLMSG_OK(hdr
, len
))
1516 switch (hdr
->nlmsg_type
)
1518 case XFRM_MSG_NEWPOLICY
:
1520 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1525 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1526 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1527 strerror(-err
->error
), -err
->error
);
1531 hdr
= NLMSG_NEXT(hdr
, len
);
1542 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1543 policy_dir_names
, direction
);
1547 *use_time
= (time_t)policy
->curlft
.use_time
;
1554 * Implementation of kernel_interface_t.del_policy.
1556 static status_t
del_policy(private_kernel_netlink_ipsec_t
*this,
1557 traffic_selector_t
*src_ts
,
1558 traffic_selector_t
*dst_ts
,
1559 policy_dir_t direction
)
1561 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1562 route_entry_t
*route
;
1563 unsigned char request
[NETLINK_BUFFER_SIZE
];
1564 struct nlmsghdr
*hdr
;
1565 struct xfrm_userpolicy_id
*policy_id
;
1566 iterator_t
*iterator
;
1568 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1569 policy_dir_names
, direction
);
1571 /* create a policy */
1572 memset(&policy
, 0, sizeof(policy_entry_t
));
1573 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1574 policy
.direction
= direction
;
1576 /* find the policy */
1577 iterator
= this->policies
->create_iterator_locked(this->policies
, &this->mutex
);
1578 while (iterator
->iterate(iterator
, (void**)¤t
))
1580 if (memcmp(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) == 0 &&
1581 policy
.direction
== current
->direction
)
1583 to_delete
= current
;
1584 if (--to_delete
->refcount
> 0)
1586 /* is used by more SAs, keep in kernel */
1587 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1588 iterator
->destroy(iterator
);
1591 /* remove if last reference */
1592 iterator
->remove(iterator
);
1596 iterator
->destroy(iterator
);
1599 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1600 dst_ts
, policy_dir_names
, direction
);
1604 memset(&request
, 0, sizeof(request
));
1606 hdr
= (struct nlmsghdr
*)request
;
1607 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1608 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1609 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1611 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1612 policy_id
->sel
= to_delete
->sel
;
1613 policy_id
->dir
= direction
;
1615 route
= to_delete
->route
;
1618 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1620 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1621 policy_dir_names
, direction
);
1627 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1628 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1629 route
->src_ip
, route
->if_name
) != SUCCESS
)
1631 DBG1(DBG_KNL
, "error uninstalling route installed with "
1632 "policy %R === %R %N", src_ts
, dst_ts
,
1633 policy_dir_names
, direction
);
1635 route_entry_destroy(route
);
1641 * Implementation of kernel_interface_t.destroy.
1643 static void destroy(private_kernel_netlink_ipsec_t
*this)
1645 this->job
->cancel(this->job
);
1646 close(this->socket_xfrm_events
);
1647 this->socket_xfrm
->destroy(this->socket_xfrm
);
1648 this->policies
->destroy(this->policies
);
1653 * Described in header.
1655 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
1657 private_kernel_netlink_ipsec_t
*this = malloc_thing(private_kernel_netlink_ipsec_t
);
1658 struct sockaddr_nl addr
;
1660 /* public functions */
1661 this->public.interface
.get_spi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,protocol_id_t
,u_int32_t
,u_int32_t
*))get_spi
;
1662 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1663 this->public.interface
.add_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
,u_int64_t
,u_int64_t
,u_int16_t
,u_int16_t
,u_int16_t
,u_int16_t
,prf_plus_t
*,mode_t
,u_int16_t
,bool,bool))add_sa
;
1664 this->public.interface
.update_sa
= (status_t(*)(kernel_ipsec_t
*,u_int32_t
,protocol_id_t
,host_t
*,host_t
*,host_t
*,host_t
*,bool))update_sa
;
1665 this->public.interface
.query_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int32_t
*))query_sa
;
1666 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
))del_sa
;
1667 this->public.interface
.add_policy
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,protocol_id_t
,u_int32_t
,bool,mode_t
,u_int16_t
))add_policy
;
1668 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1669 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
))del_policy
;
1670 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1672 /* private members */
1673 this->policies
= linked_list_create();
1674 pthread_mutex_init(&this->mutex
, NULL
);
1675 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
1676 "charon.install_routes", TRUE
);
1678 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
1680 memset(&addr
, 0, sizeof(addr
));
1681 addr
.nl_family
= AF_NETLINK
;
1683 /* create and bind XFRM socket for ACQUIRE & EXPIRE */
1684 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1685 if (this->socket_xfrm_events
<= 0)
1687 charon
->kill(charon
, "unable to create XFRM event socket");
1689 addr
.nl_groups
= XFRMGRP_ACQUIRE
| XFRMGRP_EXPIRE
;
1690 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1692 charon
->kill(charon
, "unable to bind XFRM event socket");
1695 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1697 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1699 return &this->public;