2 * Copyright (C) 2006-2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2008 Andreas Steffen
5 * Copyright (C) 2006-2007 Fabian Hartmann, Noah Heusser
6 * Copyright (C) 2006 Daniel Roethlisberger
7 * Copyright (C) 2005 Jan Hutter
8 * Hochschule fuer Technik Rapperswil
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <linux/ipsec.h>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/xfrm.h>
31 #include <linux/udp.h>
37 #include "kernel_netlink_ipsec.h"
38 #include "kernel_netlink_shared.h"
41 #include <utils/mutex.h>
42 #include <utils/linked_list.h>
43 #include <processing/jobs/callback_job.h>
44 #include <processing/jobs/acquire_job.h>
45 #include <processing/jobs/migrate_job.h>
46 #include <processing/jobs/rekey_child_sa_job.h>
47 #include <processing/jobs/delete_child_sa_job.h>
48 #include <processing/jobs/update_sa_job.h>
50 /** required for Linux 2.6.26 kernel and later */
51 #ifndef XFRM_STATE_AF_UNSPEC
52 #define XFRM_STATE_AF_UNSPEC 32
55 /** from linux/in.h */
56 #ifndef IP_IPSEC_POLICY
57 #define IP_IPSEC_POLICY 16
60 /* missing on uclibc */
61 #ifndef IPV6_IPSEC_POLICY
62 #define IPV6_IPSEC_POLICY 34
63 #endif /*IPV6_IPSEC_POLICY*/
65 /** default priority of installed policies */
67 #define PRIO_HIGH 2000
70 * Create ORable bitfield of XFRM NL groups
72 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
75 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
76 * 'usual' netlink data x like 'struct xfrm_usersa_info'
78 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
80 * returns a pointer to the next rtattr following rta.
81 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
83 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
85 * returns the total size of attached rta data
86 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
88 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
90 typedef struct kernel_algorithm_t kernel_algorithm_t
;
93 * Mapping of IKEv2 kernel identifier to linux crypto API names
95 struct kernel_algorithm_t
{
97 * Identifier specified in IKEv2
102 * Name of the algorithm in linux crypto API
107 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_KMADDRESS
,
119 "XFRMA_REPLAY_THRESH",
120 "XFRMA_ETIMER_THRESH",
130 #define END_OF_LIST -1
133 * Algorithms for encryption
135 static kernel_algorithm_t encryption_algs
[] = {
136 /* {ENCR_DES_IV64, "***" }, */
138 {ENCR_3DES
, "des3_ede" },
139 /* {ENCR_RC5, "***" }, */
140 /* {ENCR_IDEA, "***" }, */
141 {ENCR_CAST
, "cast128" },
142 {ENCR_BLOWFISH
, "blowfish" },
143 /* {ENCR_3IDEA, "***" }, */
144 /* {ENCR_DES_IV32, "***" }, */
145 {ENCR_NULL
, "cipher_null" },
146 {ENCR_AES_CBC
, "aes" },
147 /* {ENCR_AES_CTR, "***" }, */
148 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
149 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
150 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
151 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
152 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
153 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
154 {END_OF_LIST
, NULL
},
158 * Algorithms for integrity protection
160 static kernel_algorithm_t integrity_algs
[] = {
161 {AUTH_HMAC_MD5_96
, "md5" },
162 {AUTH_HMAC_SHA1_96
, "sha1" },
163 {AUTH_HMAC_SHA2_256_128
, "sha256" },
164 {AUTH_HMAC_SHA2_384_192
, "sha384" },
165 {AUTH_HMAC_SHA2_512_256
, "sha512" },
166 /* {AUTH_DES_MAC, "***" }, */
167 /* {AUTH_KPDK_MD5, "***" }, */
168 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
169 {END_OF_LIST
, NULL
},
173 * Algorithms for IPComp
175 static kernel_algorithm_t compression_algs
[] = {
176 /* {IPCOMP_OUI, "***" }, */
177 {IPCOMP_DEFLATE
, "deflate" },
178 {IPCOMP_LZS
, "lzs" },
179 {IPCOMP_LZJH
, "lzjh" },
180 {END_OF_LIST
, NULL
},
184 * Look up a kernel algorithm name and its key size
186 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
188 while (list
->ikev2
!= END_OF_LIST
)
190 if (list
->ikev2
== ikev2
)
199 typedef struct route_entry_t route_entry_t
;
202 * installed routing entry
204 struct route_entry_t
{
205 /** Name of the interface the route is bound to */
208 /** Source ip of the route */
211 /** gateway for this route */
214 /** Destination net */
217 /** Destination net prefixlen */
222 * destroy an route_entry_t object
224 static void route_entry_destroy(route_entry_t
*this)
227 this->src_ip
->destroy(this->src_ip
);
228 this->gateway
->destroy(this->gateway
);
229 chunk_free(&this->dst_net
);
233 typedef struct policy_entry_t policy_entry_t
;
236 * installed kernel policy.
238 struct policy_entry_t
{
240 /** direction of this policy: in, out, forward */
243 /** parameters of installed policy */
244 struct xfrm_selector sel
;
246 /** associated route installed for this policy */
247 route_entry_t
*route
;
249 /** by how many CHILD_SA's this policy is used */
253 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
256 * Private variables and functions of kernel_netlink class.
258 struct private_kernel_netlink_ipsec_t
{
260 * Public part of the kernel_netlink_t object.
262 kernel_netlink_ipsec_t
public;
265 * mutex to lock access to various lists
270 * List of installed policies (policy_entry_t)
272 linked_list_t
*policies
;
275 * job receiving netlink events
280 * Netlink xfrm socket (IPsec)
282 netlink_socket_t
*socket_xfrm
;
285 * netlink xfrm socket to receive acquire and expire events
287 int socket_xfrm_events
;
290 * whether to install routes along policies
296 * convert a IKEv2 specific protocol identifier to the kernel one
298 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
312 * reverse of ike2kernel
314 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
328 * convert a host_t to a struct xfrm_address
330 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
332 chunk_t chunk
= host
->get_address(host
);
333 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
337 * convert a struct xfrm_address to a host_t
339 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
346 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
349 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
354 return host_create_from_chunk(family
, chunk
, ntohs(port
));
358 * convert a traffic selector address range to subnet and its mask.
360 static void ts2subnet(traffic_selector_t
* ts
,
361 xfrm_address_t
*net
, u_int8_t
*mask
)
366 ts
->to_subnet(ts
, &net_host
, mask
);
367 net_chunk
= net_host
->get_address(net_host
);
368 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
369 net_host
->destroy(net_host
);
373 * convert a traffic selector port range to port/portmask
375 static void ts2ports(traffic_selector_t
* ts
,
376 u_int16_t
*port
, u_int16_t
*mask
)
378 /* linux does not seem to accept complex portmasks. Only
379 * any or a specific port is allowed. We set to any, if we have
380 * a port range, or to a specific, if we have one port only.
384 from
= ts
->get_from_port(ts
);
385 to
= ts
->get_to_port(ts
);
400 * convert a pair of traffic_selectors to a xfrm_selector
402 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
403 traffic_selector_t
*dst
)
405 struct xfrm_selector sel
;
407 memset(&sel
, 0, sizeof(sel
));
408 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
409 /* src or dest proto may be "any" (0), use more restrictive one */
410 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
411 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
412 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
413 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
414 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
422 * convert a xfrm_selector to a src|dst traffic_selector
424 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
433 addr
= (u_char
*)&sel
->saddr
;
434 prefixlen
= sel
->prefixlen_s
;
437 port
= htons(sel
->sport
);
442 addr
= (u_char
*)&sel
->daddr
;
443 prefixlen
= sel
->prefixlen_d
;
446 port
= htons(sel
->dport
);
450 /* The Linux 2.6 kernel does not set the selector's family field,
451 * so as a kludge we additionally test the prefix length.
453 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
455 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
457 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
459 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
464 return traffic_selector_create_from_subnet(host
, prefixlen
,
471 * process a XFRM_MSG_ACQUIRE from kernel
473 static void process_acquire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
477 traffic_selector_t
*src_ts
, *dst_ts
;
478 struct xfrm_user_acquire
*acquire
;
483 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
484 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
485 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
487 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
489 while (RTA_OK(rta
, rtasize
))
491 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
493 if (rta
->rta_type
== XFRMA_TMPL
)
495 struct xfrm_user_tmpl
* tmpl
;
497 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
499 proto
= tmpl
->id
.proto
;
501 rta
= RTA_NEXT(rta
, rtasize
);
510 /* acquire for AH/ESP only, not for IPCOMP */
513 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
514 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
515 DBG1(DBG_KNL
, "creating acquire job for policy %R === %R with reqid {%u}",
516 src_ts
, dst_ts
, reqid
);
517 job
= (job_t
*)acquire_job_create(reqid
, src_ts
, dst_ts
);
518 charon
->processor
->queue_job(charon
->processor
, job
);
522 * process a XFRM_MSG_EXPIRE from kernel
524 static void process_expire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
527 protocol_id_t protocol
;
528 u_int32_t spi
, reqid
;
529 struct xfrm_user_expire
*expire
;
531 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
532 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
533 spi
= expire
->state
.id
.spi
;
534 reqid
= expire
->state
.reqid
;
536 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
538 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
540 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%u} "
541 "which is not a CHILD_SA", ntohl(spi
), reqid
);
545 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
546 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
547 protocol
, ntohl(spi
), reqid
);
550 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
554 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
556 charon
->processor
->queue_job(charon
->processor
, job
);
560 * process a XFRM_MSG_MIGRATE from kernel
562 static void process_migrate(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
564 traffic_selector_t
*src_ts
, *dst_ts
;
565 host_t
*local
= NULL
, *remote
= NULL
;
566 host_t
*old_src
= NULL
, *old_dst
= NULL
;
567 host_t
*new_src
= NULL
, *new_dst
= NULL
;
568 struct xfrm_userpolicy_id
*policy_id
;
575 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
576 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
577 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
579 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
581 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
582 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
583 dir
= (policy_dir_t
)policy_id
->dir
;
585 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
587 while (RTA_OK(rta
, rtasize
))
589 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
590 if (rta
->rta_type
== XFRMA_KMADDRESS
)
592 struct xfrm_user_kmaddress
*kmaddress
;
594 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
595 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
596 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
597 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
599 else if (rta
->rta_type
== XFRMA_MIGRATE
)
601 struct xfrm_user_migrate
*migrate
;
604 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
605 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
606 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
607 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
608 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
609 proto
= proto_kernel2ike(migrate
->proto
);
610 reqid
= migrate
->reqid
;
611 DBG2(DBG_KNL
, " migrate %N %H...%H to %H...%H, reqid {%u}",
612 protocol_id_names
, proto
, old_src
, old_dst
,
613 new_src
, new_dst
, reqid
);
619 rta
= RTA_NEXT(rta
, rtasize
);
622 if (src_ts
&& dst_ts
&& local
&& remote
)
624 DBG1(DBG_KNL
, "creating migrate job for policy %R === %R %N with reqid {%u}",
625 src_ts
, dst_ts
, policy_dir_names
, dir
, reqid
, local
);
626 job
= (job_t
*)migrate_job_create(reqid
, src_ts
, dst_ts
, dir
,
628 charon
->processor
->queue_job(charon
->processor
, job
);
640 * process a XFRM_MSG_MAPPING from kernel
642 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
643 struct nlmsghdr
*hdr
)
646 u_int32_t spi
, reqid
;
647 struct xfrm_user_mapping
*mapping
;
650 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
651 spi
= mapping
->id
.spi
;
652 reqid
= mapping
->reqid
;
654 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
656 if (proto_kernel2ike(mapping
->id
.proto
) == PROTO_ESP
)
658 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
662 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
663 "reqid {%u} changed, queuing update job", ntohl(spi
), reqid
);
664 job
= (job_t
*)update_sa_job_create(reqid
, host
);
665 charon
->processor
->queue_job(charon
->processor
, job
);
671 * Receives events from kernel
673 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
676 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
677 struct sockaddr_nl addr
;
678 socklen_t addr_len
= sizeof(addr
);
681 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
682 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
683 (struct sockaddr
*)&addr
, &addr_len
);
684 pthread_setcancelstate(oldstate
, NULL
);
691 /* interrupted, try again */
692 return JOB_REQUEUE_DIRECT
;
694 /* no data ready, select again */
695 return JOB_REQUEUE_DIRECT
;
697 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
699 return JOB_REQUEUE_FAIR
;
703 if (addr
.nl_pid
!= 0)
704 { /* not from kernel. not interested, try another one */
705 return JOB_REQUEUE_DIRECT
;
708 while (NLMSG_OK(hdr
, len
))
710 switch (hdr
->nlmsg_type
)
712 case XFRM_MSG_ACQUIRE
:
713 process_acquire(this, hdr
);
715 case XFRM_MSG_EXPIRE
:
716 process_expire(this, hdr
);
718 case XFRM_MSG_MIGRATE
:
719 process_migrate(this, hdr
);
721 case XFRM_MSG_MAPPING
:
722 process_mapping(this, hdr
);
727 hdr
= NLMSG_NEXT(hdr
, len
);
729 return JOB_REQUEUE_DIRECT
;
733 * Get an SPI for a specific protocol from the kernel.
735 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
736 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
737 u_int32_t reqid
, u_int32_t
*spi
)
739 netlink_buf_t request
;
740 struct nlmsghdr
*hdr
, *out
;
741 struct xfrm_userspi_info
*userspi
;
742 u_int32_t received_spi
= 0;
745 memset(&request
, 0, sizeof(request
));
747 hdr
= (struct nlmsghdr
*)request
;
748 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
749 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
750 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
752 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
753 host2xfrm(src
, &userspi
->info
.saddr
);
754 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
755 userspi
->info
.id
.proto
= proto
;
756 userspi
->info
.mode
= TRUE
; /* tunnel mode */
757 userspi
->info
.reqid
= reqid
;
758 userspi
->info
.family
= src
->get_family(src
);
762 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
765 while (NLMSG_OK(hdr
, len
))
767 switch (hdr
->nlmsg_type
)
771 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
772 received_spi
= usersa
->id
.spi
;
777 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
779 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
780 strerror(-err
->error
), -err
->error
);
784 hdr
= NLMSG_NEXT(hdr
, len
);
794 if (received_spi
== 0)
804 * Implementation of kernel_interface_t.get_spi.
806 static status_t
get_spi(private_kernel_netlink_ipsec_t
*this,
807 host_t
*src
, host_t
*dst
,
808 protocol_id_t protocol
, u_int32_t reqid
,
811 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
813 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
814 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
816 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
820 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
826 * Implementation of kernel_interface_t.get_cpi.
828 static status_t
get_cpi(private_kernel_netlink_ipsec_t
*this,
829 host_t
*src
, host_t
*dst
,
830 u_int32_t reqid
, u_int16_t
*cpi
)
832 u_int32_t received_spi
= 0;
834 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
836 if (get_spi_internal(this, src
, dst
,
837 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
839 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
843 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
845 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
851 * Implementation of kernel_interface_t.add_sa.
853 static status_t
add_sa(private_kernel_netlink_ipsec_t
*this,
854 host_t
*src
, host_t
*dst
, u_int32_t spi
,
855 protocol_id_t protocol
, u_int32_t reqid
,
856 u_int64_t expire_soft
, u_int64_t expire_hard
,
857 u_int16_t enc_alg
, chunk_t enc_key
,
858 u_int16_t int_alg
, chunk_t int_key
,
859 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
860 bool encap
, bool inbound
)
862 netlink_buf_t request
;
864 struct nlmsghdr
*hdr
;
865 struct xfrm_usersa_info
*sa
;
866 u_int16_t icv_size
= 64;
868 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
869 * we are in the recursive call below */
870 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
872 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, 0, 0,
873 ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
, chunk_empty
,
874 mode
, ipcomp
, 0, FALSE
, inbound
);
875 ipcomp
= IPCOMP_NONE
;
878 memset(&request
, 0, sizeof(request
));
880 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
883 hdr
= (struct nlmsghdr
*)request
;
884 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
885 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
886 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
888 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
889 host2xfrm(src
, &sa
->saddr
);
890 host2xfrm(dst
, &sa
->id
.daddr
);
892 sa
->id
.proto
= proto_ike2kernel(protocol
);
893 sa
->family
= src
->get_family(src
);
895 if (mode
== MODE_TUNNEL
)
897 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
899 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
901 /* we currently do not expire SAs by volume/packet count */
902 sa
->lft
.soft_byte_limit
= XFRM_INF
;
903 sa
->lft
.hard_byte_limit
= XFRM_INF
;
904 sa
->lft
.soft_packet_limit
= XFRM_INF
;
905 sa
->lft
.hard_packet_limit
= XFRM_INF
;
906 /* we use lifetimes since added, not since used */
907 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
908 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
909 sa
->lft
.soft_use_expires_seconds
= 0;
910 sa
->lft
.hard_use_expires_seconds
= 0;
912 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
919 case ENCR_AES_CCM_ICV16
:
920 case ENCR_AES_GCM_ICV16
:
923 case ENCR_AES_CCM_ICV12
:
924 case ENCR_AES_GCM_ICV12
:
927 case ENCR_AES_CCM_ICV8
:
928 case ENCR_AES_GCM_ICV8
:
930 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
931 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
932 if (alg_name
== NULL
)
934 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
935 encryption_algorithm_names
, enc_alg
);
938 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
939 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
941 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_key
.len
);
942 hdr
->nlmsg_len
+= rthdr
->rta_len
;
943 if (hdr
->nlmsg_len
> sizeof(request
))
948 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
949 algo
->alg_key_len
= enc_key
.len
* 8;
950 algo
->alg_icv_len
= icv_size
;
951 strcpy(algo
->alg_name
, alg_name
);
952 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
954 rthdr
= XFRM_RTA_NEXT(rthdr
);
959 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
960 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
961 if (alg_name
== NULL
)
963 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
964 encryption_algorithm_names
, enc_alg
);
967 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
968 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
970 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
971 hdr
->nlmsg_len
+= rthdr
->rta_len
;
972 if (hdr
->nlmsg_len
> sizeof(request
))
977 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
978 algo
->alg_key_len
= enc_key
.len
* 8;
979 strcpy(algo
->alg_name
, alg_name
);
980 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
982 rthdr
= XFRM_RTA_NEXT(rthdr
);
987 if (int_alg
!= AUTH_UNDEFINED
)
989 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
990 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
991 if (alg_name
== NULL
)
993 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
994 integrity_algorithm_names
, int_alg
);
997 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
998 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1000 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1001 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1002 if (hdr
->nlmsg_len
> sizeof(request
))
1007 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1008 algo
->alg_key_len
= int_key
.len
* 8;
1009 strcpy(algo
->alg_name
, alg_name
);
1010 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1012 rthdr
= XFRM_RTA_NEXT(rthdr
);
1015 if (ipcomp
!= IPCOMP_NONE
)
1017 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1018 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1019 if (alg_name
== NULL
)
1021 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1022 ipcomp_transform_names
, ipcomp
);
1025 DBG2(DBG_KNL
, " using compression algorithm %N",
1026 ipcomp_transform_names
, ipcomp
);
1028 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1029 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1030 if (hdr
->nlmsg_len
> sizeof(request
))
1035 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1036 algo
->alg_key_len
= 0;
1037 strcpy(algo
->alg_name
, alg_name
);
1039 rthdr
= XFRM_RTA_NEXT(rthdr
);
1044 rthdr
->rta_type
= XFRMA_ENCAP
;
1045 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1047 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1048 if (hdr
->nlmsg_len
> sizeof(request
))
1053 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1054 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1055 tmpl
->encap_sport
= htons(src
->get_port(src
));
1056 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1057 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1058 /* encap_oa could probably be derived from the
1059 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1060 * pluto does the same as we do here but it uses encap_oa in the
1061 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1062 * the kernel ignores it anyway
1063 * -> does that mean that NAT-T encap doesn't work in transport mode?
1064 * No. The reason the kernel ignores NAT-OA is that it recomputes
1065 * (or, rather, just ignores) the checksum. If packets pass
1066 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1067 rthdr
= XFRM_RTA_NEXT(rthdr
);
1070 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1072 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1079 * Get the replay state (i.e. sequence numbers) of an SA.
1081 static status_t
get_replay_state(private_kernel_netlink_ipsec_t
*this,
1082 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
1083 struct xfrm_replay_state
*replay
)
1085 netlink_buf_t request
;
1086 struct nlmsghdr
*hdr
, *out
= NULL
;
1087 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1092 memset(&request
, 0, sizeof(request
));
1094 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x", ntohl(spi
));
1096 hdr
= (struct nlmsghdr
*)request
;
1097 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1098 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1099 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1101 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1102 aevent_id
->flags
= XFRM_AE_RVAL
;
1104 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1105 aevent_id
->sa_id
.spi
= spi
;
1106 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
1107 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1109 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1112 while (NLMSG_OK(hdr
, len
))
1114 switch (hdr
->nlmsg_type
)
1116 case XFRM_MSG_NEWAE
:
1118 out_aevent
= NLMSG_DATA(hdr
);
1123 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1124 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
1125 strerror(-err
->error
), -err
->error
);
1129 hdr
= NLMSG_NEXT(hdr
, len
);
1138 if (out_aevent
== NULL
)
1140 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1146 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1147 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1148 while(RTA_OK(rta
, rtasize
))
1150 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1151 RTA_PAYLOAD(rta
) == sizeof(struct xfrm_replay_state
))
1153 memcpy(replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1157 rta
= RTA_NEXT(rta
, rtasize
);
1160 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1167 * Implementation of kernel_interface_t.del_sa.
1169 static status_t
del_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1170 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
)
1172 netlink_buf_t request
;
1173 struct nlmsghdr
*hdr
;
1174 struct xfrm_usersa_id
*sa_id
;
1176 /* if IPComp was used, we first delete the additional IPComp SA */
1179 del_sa(this, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0);
1182 memset(&request
, 0, sizeof(request
));
1184 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1186 hdr
= (struct nlmsghdr
*)request
;
1187 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1188 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1189 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1191 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1192 host2xfrm(dst
, &sa_id
->daddr
);
1194 sa_id
->proto
= proto_ike2kernel(protocol
);
1195 sa_id
->family
= dst
->get_family(dst
);
1197 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1199 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1202 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1207 * Implementation of kernel_interface_t.update_sa.
1209 static status_t
update_sa(private_kernel_netlink_ipsec_t
*this,
1210 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
,
1211 host_t
*src
, host_t
*dst
,
1212 host_t
*new_src
, host_t
*new_dst
,
1213 bool old_encap
, bool new_encap
)
1215 netlink_buf_t request
;
1217 struct nlmsghdr
*hdr
, *out
= NULL
;
1218 struct xfrm_usersa_id
*sa_id
;
1219 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1223 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1224 bool got_replay_state
= FALSE
;
1225 struct xfrm_replay_state replay
;
1227 /* if IPComp is used, we first update the IPComp SA */
1230 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1231 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
);
1234 memset(&request
, 0, sizeof(request
));
1236 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1238 /* query the existing SA first */
1239 hdr
= (struct nlmsghdr
*)request
;
1240 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1241 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1242 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1244 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1245 host2xfrm(dst
, &sa_id
->daddr
);
1247 sa_id
->proto
= proto_ike2kernel(protocol
);
1248 sa_id
->family
= dst
->get_family(dst
);
1250 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1253 while (NLMSG_OK(hdr
, len
))
1255 switch (hdr
->nlmsg_type
)
1257 case XFRM_MSG_NEWSA
:
1259 out_sa
= NLMSG_DATA(hdr
);
1264 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1265 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1266 strerror(-err
->error
), -err
->error
);
1270 hdr
= NLMSG_NEXT(hdr
, len
);
1280 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1285 /* try to get the replay state */
1286 if (get_replay_state(this, spi
, protocol
, dst
, &replay
) == SUCCESS
)
1288 got_replay_state
= TRUE
;
1291 /* delete the old SA (without affecting the IPComp SA) */
1292 if (del_sa(this, dst
, spi
, protocol
, 0) != SUCCESS
)
1294 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1299 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1300 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1301 /* copy over the SA from out to request */
1302 hdr
= (struct nlmsghdr
*)request
;
1303 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1304 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1305 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1306 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1307 sa
= NLMSG_DATA(hdr
);
1308 sa
->family
= new_dst
->get_family(new_dst
);
1310 if (!src
->ip_equals(src
, new_src
))
1312 host2xfrm(new_src
, &sa
->saddr
);
1314 if (!dst
->ip_equals(dst
, new_dst
))
1316 host2xfrm(new_dst
, &sa
->id
.daddr
);
1319 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1320 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1321 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1322 while(RTA_OK(rta
, rtasize
))
1324 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1325 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1327 if (rta
->rta_type
== XFRMA_ENCAP
)
1328 { /* update encap tmpl */
1329 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1330 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1331 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1333 memcpy(pos
, rta
, rta
->rta_len
);
1334 pos
+= RTA_ALIGN(rta
->rta_len
);
1335 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1337 rta
= RTA_NEXT(rta
, rtasize
);
1340 rta
= (struct rtattr
*)pos
;
1341 if (tmpl
== NULL
&& new_encap
)
1342 { /* add tmpl if we are enabling it */
1343 rta
->rta_type
= XFRMA_ENCAP
;
1344 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1346 hdr
->nlmsg_len
+= rta
->rta_len
;
1347 if (hdr
->nlmsg_len
> sizeof(request
))
1352 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1353 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1354 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1355 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1356 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1358 rta
= XFRM_RTA_NEXT(rta
);
1361 if (got_replay_state
)
1362 { /* copy the replay data if available */
1363 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1364 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1366 hdr
->nlmsg_len
+= rta
->rta_len
;
1367 if (hdr
->nlmsg_len
> sizeof(request
))
1371 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
1373 rta
= XFRM_RTA_NEXT(rta
);
1376 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1378 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1388 * Implementation of kernel_interface_t.add_policy.
1390 static status_t
add_policy(private_kernel_netlink_ipsec_t
*this,
1391 host_t
*src
, host_t
*dst
,
1392 traffic_selector_t
*src_ts
,
1393 traffic_selector_t
*dst_ts
,
1394 policy_dir_t direction
, u_int32_t spi
,
1395 protocol_id_t protocol
, u_int32_t reqid
,
1396 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
1399 iterator_t
*iterator
;
1400 policy_entry_t
*current
, *policy
;
1402 netlink_buf_t request
;
1403 struct xfrm_userpolicy_info
*policy_info
;
1404 struct nlmsghdr
*hdr
;
1406 /* create a policy */
1407 policy
= malloc_thing(policy_entry_t
);
1408 memset(policy
, 0, sizeof(policy_entry_t
));
1409 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1410 policy
->direction
= direction
;
1412 /* find the policy, which matches EXACTLY */
1413 this->mutex
->lock(this->mutex
);
1414 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1415 while (iterator
->iterate(iterator
, (void**)¤t
))
1417 if (memeq(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) &&
1418 policy
->direction
== current
->direction
)
1420 /* use existing policy */
1421 current
->refcount
++;
1422 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1423 "refcount", src_ts
, dst_ts
,
1424 policy_dir_names
, direction
);
1431 iterator
->destroy(iterator
);
1433 { /* apply the new one, if we have no such policy */
1434 this->policies
->insert_last(this->policies
, policy
);
1435 policy
->refcount
= 1;
1438 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1439 policy_dir_names
, direction
);
1441 memset(&request
, 0, sizeof(request
));
1442 hdr
= (struct nlmsghdr
*)request
;
1443 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1444 hdr
->nlmsg_type
= found ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1445 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1447 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1448 policy_info
->sel
= policy
->sel
;
1449 policy_info
->dir
= policy
->direction
;
1450 /* calculate priority based on source selector size, small size = high prio */
1451 policy_info
->priority
= routed ? PRIO_LOW
: PRIO_HIGH
;
1452 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1453 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1454 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1455 policy_info
->action
= XFRM_POLICY_ALLOW
;
1456 policy_info
->share
= XFRM_SHARE_ANY
;
1457 this->mutex
->unlock(this->mutex
);
1459 /* policies don't expire */
1460 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1461 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1462 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1463 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1464 policy_info
->lft
.soft_add_expires_seconds
= 0;
1465 policy_info
->lft
.hard_add_expires_seconds
= 0;
1466 policy_info
->lft
.soft_use_expires_seconds
= 0;
1467 policy_info
->lft
.hard_use_expires_seconds
= 0;
1469 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1470 rthdr
->rta_type
= XFRMA_TMPL
;
1471 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1473 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1474 if (hdr
->nlmsg_len
> sizeof(request
))
1479 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1481 if (ipcomp
!= IPCOMP_NONE
)
1483 tmpl
->reqid
= reqid
;
1484 tmpl
->id
.proto
= IPPROTO_COMP
;
1485 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1487 tmpl
->optional
= direction
!= POLICY_OUT
;
1488 tmpl
->family
= src
->get_family(src
);
1490 host2xfrm(src
, &tmpl
->saddr
);
1491 host2xfrm(dst
, &tmpl
->id
.daddr
);
1493 /* add an additional xfrm_user_tmpl */
1494 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1495 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1496 if (hdr
->nlmsg_len
> sizeof(request
))
1504 tmpl
->reqid
= reqid
;
1505 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
1506 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1508 tmpl
->family
= src
->get_family(src
);
1510 host2xfrm(src
, &tmpl
->saddr
);
1511 host2xfrm(dst
, &tmpl
->id
.daddr
);
1513 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1515 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1516 policy_dir_names
, direction
);
1520 /* install a route, if:
1521 * - we are NOT updating a policy
1522 * - this is a forward policy (to just get one for each child)
1523 * - we are in tunnel mode
1524 * - we are not using IPv6 (does not work correctly yet!)
1525 * - routing is not disabled via strongswan.conf
1527 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1528 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1529 this->install_routes
)
1531 route_entry_t
*route
= malloc_thing(route_entry_t
);
1533 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1534 dst_ts
, &route
->src_ip
) == SUCCESS
)
1536 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1537 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1538 charon
->kernel_interface
, src
);
1539 route
->if_name
= charon
->kernel_interface
->get_interface(
1540 charon
->kernel_interface
, dst
);
1541 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1542 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
1543 route
->prefixlen
= policy
->sel
.prefixlen_s
;
1547 switch (charon
->kernel_interface
->add_route(
1548 charon
->kernel_interface
, route
->dst_net
,
1549 route
->prefixlen
, route
->gateway
,
1550 route
->src_ip
, route
->if_name
))
1553 DBG1(DBG_KNL
, "unable to install source route for %H",
1557 /* route exists, do not uninstall */
1558 route_entry_destroy(route
);
1561 /* cache the installed route */
1562 policy
->route
= route
;
1568 route_entry_destroy(route
);
1580 * Implementation of kernel_interface_t.query_policy.
1582 static status_t
query_policy(private_kernel_netlink_ipsec_t
*this,
1583 traffic_selector_t
*src_ts
,
1584 traffic_selector_t
*dst_ts
,
1585 policy_dir_t direction
, u_int32_t
*use_time
)
1587 netlink_buf_t request
;
1588 struct nlmsghdr
*out
= NULL
, *hdr
;
1589 struct xfrm_userpolicy_id
*policy_id
;
1590 struct xfrm_userpolicy_info
*policy
= NULL
;
1593 memset(&request
, 0, sizeof(request
));
1595 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1596 policy_dir_names
, direction
);
1598 hdr
= (struct nlmsghdr
*)request
;
1599 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1600 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1601 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1603 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1604 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1605 policy_id
->dir
= direction
;
1607 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1610 while (NLMSG_OK(hdr
, len
))
1612 switch (hdr
->nlmsg_type
)
1614 case XFRM_MSG_NEWPOLICY
:
1616 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1621 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1622 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1623 strerror(-err
->error
), -err
->error
);
1627 hdr
= NLMSG_NEXT(hdr
, len
);
1638 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1639 policy_dir_names
, direction
);
1643 *use_time
= (time_t)policy
->curlft
.use_time
;
1650 * Implementation of kernel_interface_t.del_policy.
1652 static status_t
del_policy(private_kernel_netlink_ipsec_t
*this,
1653 traffic_selector_t
*src_ts
,
1654 traffic_selector_t
*dst_ts
,
1655 policy_dir_t direction
, bool unrouted
)
1657 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1658 route_entry_t
*route
;
1659 netlink_buf_t request
;
1660 struct nlmsghdr
*hdr
;
1661 struct xfrm_userpolicy_id
*policy_id
;
1662 enumerator_t
*enumerator
;
1664 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1665 policy_dir_names
, direction
);
1667 /* create a policy */
1668 memset(&policy
, 0, sizeof(policy_entry_t
));
1669 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1670 policy
.direction
= direction
;
1672 /* find the policy */
1673 this->mutex
->lock(this->mutex
);
1674 enumerator
= this->policies
->create_enumerator(this->policies
);
1675 while (enumerator
->enumerate(enumerator
, ¤t
))
1677 if (memeq(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) &&
1678 policy
.direction
== current
->direction
)
1680 to_delete
= current
;
1681 if (--to_delete
->refcount
> 0)
1683 /* is used by more SAs, keep in kernel */
1684 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1685 this->mutex
->unlock(this->mutex
);
1686 enumerator
->destroy(enumerator
);
1689 /* remove if last reference */
1690 this->policies
->remove_at(this->policies
, enumerator
);
1694 this->mutex
->unlock(this->mutex
);
1695 enumerator
->destroy(enumerator
);
1698 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1699 dst_ts
, policy_dir_names
, direction
);
1703 memset(&request
, 0, sizeof(request
));
1705 hdr
= (struct nlmsghdr
*)request
;
1706 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1707 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1708 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1710 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1711 policy_id
->sel
= to_delete
->sel
;
1712 policy_id
->dir
= direction
;
1714 route
= to_delete
->route
;
1717 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1719 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1720 policy_dir_names
, direction
);
1726 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1727 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1728 route
->src_ip
, route
->if_name
) != SUCCESS
)
1730 DBG1(DBG_KNL
, "error uninstalling route installed with "
1731 "policy %R === %R %N", src_ts
, dst_ts
,
1732 policy_dir_names
, direction
);
1734 route_entry_destroy(route
);
1740 * Implementation of kernel_interface_t.destroy.
1742 static void destroy(private_kernel_netlink_ipsec_t
*this)
1744 this->job
->cancel(this->job
);
1745 close(this->socket_xfrm_events
);
1746 this->socket_xfrm
->destroy(this->socket_xfrm
);
1747 this->policies
->destroy(this->policies
);
1748 this->mutex
->destroy(this->mutex
);
1753 * Add bypass policies for IKE on the sockets used by charon
1755 static bool add_bypass_policies()
1757 int fd
, family
, port
;
1758 enumerator_t
*sockets
;
1761 /* we open an AF_KEY socket to autoload the af_key module. Otherwise
1762 * setsockopt(IPSEC_POLICY) won't work. */
1763 fd
= socket(AF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1766 DBG1(DBG_KNL
, "could not open AF_KEY socket");
1771 sockets
= charon
->socket
->create_enumerator(charon
->socket
);
1772 while (sockets
->enumerate(sockets
, &fd
, &family
, &port
))
1774 struct sadb_x_policy policy
;
1775 u_int sol
, ipsec_policy
;
1781 ipsec_policy
= IP_IPSEC_POLICY
;
1785 ipsec_policy
= IPV6_IPSEC_POLICY
;
1791 memset(&policy
, 0, sizeof(policy
));
1792 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(u_int64_t
);
1793 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1794 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
1796 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
1797 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1799 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1804 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
1805 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1807 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1813 sockets
->destroy(sockets
);
1818 * Described in header.
1820 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
1822 private_kernel_netlink_ipsec_t
*this = malloc_thing(private_kernel_netlink_ipsec_t
);
1823 struct sockaddr_nl addr
;
1825 /* public functions */
1826 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
;
1827 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1828 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
,chunk_t
,u_int16_t
,chunk_t
,ipsec_mode_t
,u_int16_t
,u_int16_t
,bool,bool))add_sa
;
1829 this->public.interface
.update_sa
= (status_t(*)(kernel_ipsec_t
*,u_int32_t
,protocol_id_t
,u_int16_t
,host_t
*,host_t
*,host_t
*,host_t
*,bool,bool))update_sa
;
1830 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int16_t
))del_sa
;
1831 this->public.interface
.add_policy
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
,protocol_id_t
,u_int32_t
,ipsec_mode_t
,u_int16_t
,u_int16_t
,bool))add_policy
;
1832 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1833 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,bool))del_policy
;
1834 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1836 /* private members */
1837 this->policies
= linked_list_create();
1838 this->mutex
= mutex_create(MUTEX_DEFAULT
);
1839 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
1840 "charon.install_routes", TRUE
);
1842 /* add bypass policies on the sockets used by charon */
1843 if (!add_bypass_policies())
1845 charon
->kill(charon
, "unable to add bypass policies on sockets");
1848 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
1850 memset(&addr
, 0, sizeof(addr
));
1851 addr
.nl_family
= AF_NETLINK
;
1853 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1854 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1855 if (this->socket_xfrm_events
<= 0)
1857 charon
->kill(charon
, "unable to create XFRM event socket");
1859 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
1860 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
1861 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1863 charon
->kill(charon
, "unable to bind XFRM event socket");
1866 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1868 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1870 return &this->public;