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 /** default priority of installed policies */
62 #define PRIO_HIGH 2000
65 * Create ORable bitfield of XFRM NL groups
67 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
70 * returns a pointer to the first rtattr following the nlmsghdr *nlh and the
71 * 'usual' netlink data x like 'struct xfrm_usersa_info'
73 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + NLMSG_ALIGN(sizeof(x))))
75 * returns a pointer to the next rtattr following rta.
76 * !!! do not use this to parse messages. use RTA_NEXT and RTA_OK instead !!!
78 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
80 * returns the total size of attached rta data
81 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
83 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
85 typedef struct kernel_algorithm_t kernel_algorithm_t
;
88 * Mapping of IKEv2 kernel identifier to linux crypto API names
90 struct kernel_algorithm_t
{
92 * Identifier specified in IKEv2
97 * Name of the algorithm in linux crypto API
102 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_KMADDRESS
,
114 "XFRMA_REPLAY_THRESH",
115 "XFRMA_ETIMER_THRESH",
125 #define END_OF_LIST -1
128 * Algorithms for encryption
130 static kernel_algorithm_t encryption_algs
[] = {
131 /* {ENCR_DES_IV64, "***" }, */
133 {ENCR_3DES
, "des3_ede" },
134 /* {ENCR_RC5, "***" }, */
135 /* {ENCR_IDEA, "***" }, */
136 {ENCR_CAST
, "cast128" },
137 {ENCR_BLOWFISH
, "blowfish" },
138 /* {ENCR_3IDEA, "***" }, */
139 /* {ENCR_DES_IV32, "***" }, */
140 {ENCR_NULL
, "cipher_null" },
141 {ENCR_AES_CBC
, "aes" },
142 /* {ENCR_AES_CTR, "***" }, */
143 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
144 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
145 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
146 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
147 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
148 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
149 {END_OF_LIST
, NULL
},
153 * Algorithms for integrity protection
155 static kernel_algorithm_t integrity_algs
[] = {
156 {AUTH_HMAC_MD5_96
, "md5" },
157 {AUTH_HMAC_SHA1_96
, "sha1" },
158 {AUTH_HMAC_SHA2_256_128
, "sha256" },
159 {AUTH_HMAC_SHA2_384_192
, "sha384" },
160 {AUTH_HMAC_SHA2_512_256
, "sha512" },
161 /* {AUTH_DES_MAC, "***" }, */
162 /* {AUTH_KPDK_MD5, "***" }, */
163 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
164 {END_OF_LIST
, NULL
},
168 * Algorithms for IPComp
170 static kernel_algorithm_t compression_algs
[] = {
171 /* {IPCOMP_OUI, "***" }, */
172 {IPCOMP_DEFLATE
, "deflate" },
173 {IPCOMP_LZS
, "lzs" },
174 {IPCOMP_LZJH
, "lzjh" },
175 {END_OF_LIST
, NULL
},
179 * Look up a kernel algorithm name and its key size
181 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
183 while (list
->ikev2
!= END_OF_LIST
)
185 if (list
->ikev2
== ikev2
)
194 typedef struct route_entry_t route_entry_t
;
197 * installed routing entry
199 struct route_entry_t
{
200 /** Name of the interface the route is bound to */
203 /** Source ip of the route */
206 /** gateway for this route */
209 /** Destination net */
212 /** Destination net prefixlen */
217 * destroy an route_entry_t object
219 static void route_entry_destroy(route_entry_t
*this)
222 this->src_ip
->destroy(this->src_ip
);
223 this->gateway
->destroy(this->gateway
);
224 chunk_free(&this->dst_net
);
228 typedef struct policy_entry_t policy_entry_t
;
231 * installed kernel policy.
233 struct policy_entry_t
{
235 /** direction of this policy: in, out, forward */
238 /** parameters of installed policy */
239 struct xfrm_selector sel
;
241 /** associated route installed for this policy */
242 route_entry_t
*route
;
244 /** by how many CHILD_SA's this policy is used */
248 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
251 * Private variables and functions of kernel_netlink class.
253 struct private_kernel_netlink_ipsec_t
{
255 * Public part of the kernel_netlink_t object.
257 kernel_netlink_ipsec_t
public;
260 * mutex to lock access to various lists
265 * List of installed policies (policy_entry_t)
267 linked_list_t
*policies
;
270 * job receiving netlink events
275 * Netlink xfrm socket (IPsec)
277 netlink_socket_t
*socket_xfrm
;
280 * netlink xfrm socket to receive acquire and expire events
282 int socket_xfrm_events
;
285 * whether to install routes along policies
291 * convert a IKEv2 specific protocol identifier to the kernel one
293 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
307 * reverse of ike2kernel
309 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
323 * convert a host_t to a struct xfrm_address
325 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
327 chunk_t chunk
= host
->get_address(host
);
328 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
332 * convert a struct xfrm_address to a host_t
334 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
341 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
344 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
349 return host_create_from_chunk(family
, chunk
, ntohs(port
));
353 * convert a traffic selector address range to subnet and its mask.
355 static void ts2subnet(traffic_selector_t
* ts
,
356 xfrm_address_t
*net
, u_int8_t
*mask
)
361 ts
->to_subnet(ts
, &net_host
, mask
);
362 net_chunk
= net_host
->get_address(net_host
);
363 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
364 net_host
->destroy(net_host
);
368 * convert a traffic selector port range to port/portmask
370 static void ts2ports(traffic_selector_t
* ts
,
371 u_int16_t
*port
, u_int16_t
*mask
)
373 /* linux does not seem to accept complex portmasks. Only
374 * any or a specific port is allowed. We set to any, if we have
375 * a port range, or to a specific, if we have one port only.
379 from
= ts
->get_from_port(ts
);
380 to
= ts
->get_to_port(ts
);
395 * convert a pair of traffic_selectors to a xfrm_selector
397 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
398 traffic_selector_t
*dst
)
400 struct xfrm_selector sel
;
402 memset(&sel
, 0, sizeof(sel
));
403 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
404 /* src or dest proto may be "any" (0), use more restrictive one */
405 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
406 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
407 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
408 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
409 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
417 * convert a xfrm_selector to a src|dst traffic_selector
419 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
424 u_int16_t port
, port_mask
;
426 traffic_selector_t
*ts
;
430 addr
.ptr
= (u_char
*)&sel
->saddr
;
431 prefixlen
= sel
->prefixlen_s
;
433 port_mask
= sel
->sport_mask
;
437 addr
.ptr
= (u_char
*)&sel
->daddr
;
438 prefixlen
= sel
->prefixlen_d
;
440 port_mask
= sel
->dport_mask
;
443 /* The Linux 2.6 kernel does not set the selector's family field,
444 * so as a kludge we additionally test the prefix length.
446 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
451 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
460 host
= host_create_from_chunk(family
, addr
, 0);
461 port
= (port_mask
== 0) ?
0 : ntohs(port
);
463 ts
= traffic_selector_create_from_subnet(host
, prefixlen
, sel
->proto
, port
);
469 * process a XFRM_MSG_ACQUIRE from kernel
471 static void process_acquire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
475 traffic_selector_t
*src_ts
, *dst_ts
;
476 struct xfrm_user_acquire
*acquire
;
481 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
482 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
483 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
485 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
487 while (RTA_OK(rta
, rtasize
))
489 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
491 if (rta
->rta_type
== XFRMA_TMPL
)
493 struct xfrm_user_tmpl
* tmpl
;
495 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
497 proto
= tmpl
->id
.proto
;
499 rta
= RTA_NEXT(rta
, rtasize
);
508 /* acquire for AH/ESP only, not for IPCOMP */
511 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
512 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
513 DBG1(DBG_KNL
, "creating acquire job for policy %R === %R with reqid {%u}",
514 src_ts
, dst_ts
, reqid
);
515 job
= (job_t
*)acquire_job_create(reqid
, src_ts
, dst_ts
);
516 charon
->processor
->queue_job(charon
->processor
, job
);
520 * process a XFRM_MSG_EXPIRE from kernel
522 static void process_expire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
525 protocol_id_t protocol
;
526 u_int32_t spi
, reqid
;
527 struct xfrm_user_expire
*expire
;
529 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
530 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
531 spi
= expire
->state
.id
.spi
;
532 reqid
= expire
->state
.reqid
;
534 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
536 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
538 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%u} "
539 "which is not a CHILD_SA", ntohl(spi
), reqid
);
543 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
544 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
545 protocol
, ntohl(spi
), reqid
);
548 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
552 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
554 charon
->processor
->queue_job(charon
->processor
, job
);
558 * process a XFRM_MSG_MIGRATE from kernel
560 static void process_migrate(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
562 traffic_selector_t
*src_ts
, *dst_ts
;
563 host_t
*local
= NULL
, *remote
= NULL
;
564 host_t
*old_src
= NULL
, *old_dst
= NULL
;
565 host_t
*new_src
= NULL
, *new_dst
= NULL
;
566 struct xfrm_userpolicy_id
*policy_id
;
573 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
574 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
575 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
577 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
579 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
580 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
581 dir
= (policy_dir_t
)policy_id
->dir
;
583 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
585 while (RTA_OK(rta
, rtasize
))
587 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
588 if (rta
->rta_type
== XFRMA_KMADDRESS
)
590 struct xfrm_user_kmaddress
*kmaddress
;
592 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
593 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
594 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
595 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
597 else if (rta
->rta_type
== XFRMA_MIGRATE
)
599 struct xfrm_user_migrate
*migrate
;
602 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
603 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
604 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
605 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
606 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
607 proto
= proto_kernel2ike(migrate
->proto
);
608 reqid
= migrate
->reqid
;
609 DBG2(DBG_KNL
, " migrate %N %H...%H to %H...%H, reqid {%u}",
610 protocol_id_names
, proto
, old_src
, old_dst
,
611 new_src
, new_dst
, reqid
);
617 rta
= RTA_NEXT(rta
, rtasize
);
620 if (src_ts
&& dst_ts
&& local
&& remote
)
622 DBG1(DBG_KNL
, "creating migrate job for policy %R === %R %N with reqid {%u}",
623 src_ts
, dst_ts
, policy_dir_names
, dir
, reqid
, local
);
624 job
= (job_t
*)migrate_job_create(reqid
, src_ts
, dst_ts
, dir
,
626 charon
->processor
->queue_job(charon
->processor
, job
);
638 * process a XFRM_MSG_MAPPING from kernel
640 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
641 struct nlmsghdr
*hdr
)
644 u_int32_t spi
, reqid
;
645 struct xfrm_user_mapping
*mapping
;
648 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
649 spi
= mapping
->id
.spi
;
650 reqid
= mapping
->reqid
;
652 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
654 if (proto_kernel2ike(mapping
->id
.proto
) == PROTO_ESP
)
656 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
660 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
661 "reqid {%u} changed, queuing update job", ntohl(spi
), reqid
);
662 job
= (job_t
*)update_sa_job_create(reqid
, host
);
663 charon
->processor
->queue_job(charon
->processor
, job
);
669 * Receives events from kernel
671 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
674 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
675 struct sockaddr_nl addr
;
676 socklen_t addr_len
= sizeof(addr
);
679 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
680 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
681 (struct sockaddr
*)&addr
, &addr_len
);
682 pthread_setcancelstate(oldstate
, NULL
);
689 /* interrupted, try again */
690 return JOB_REQUEUE_DIRECT
;
692 /* no data ready, select again */
693 return JOB_REQUEUE_DIRECT
;
695 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
697 return JOB_REQUEUE_FAIR
;
701 if (addr
.nl_pid
!= 0)
702 { /* not from kernel. not interested, try another one */
703 return JOB_REQUEUE_DIRECT
;
706 while (NLMSG_OK(hdr
, len
))
708 switch (hdr
->nlmsg_type
)
710 case XFRM_MSG_ACQUIRE
:
711 process_acquire(this, hdr
);
713 case XFRM_MSG_EXPIRE
:
714 process_expire(this, hdr
);
716 case XFRM_MSG_MIGRATE
:
717 process_migrate(this, hdr
);
719 case XFRM_MSG_MAPPING
:
720 process_mapping(this, hdr
);
725 hdr
= NLMSG_NEXT(hdr
, len
);
727 return JOB_REQUEUE_DIRECT
;
731 * Get an SPI for a specific protocol from the kernel.
733 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
734 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
735 u_int32_t reqid
, u_int32_t
*spi
)
737 netlink_buf_t request
;
738 struct nlmsghdr
*hdr
, *out
;
739 struct xfrm_userspi_info
*userspi
;
740 u_int32_t received_spi
= 0;
743 memset(&request
, 0, sizeof(request
));
745 hdr
= (struct nlmsghdr
*)request
;
746 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
747 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
748 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
750 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
751 host2xfrm(src
, &userspi
->info
.saddr
);
752 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
753 userspi
->info
.id
.proto
= proto
;
754 userspi
->info
.mode
= TRUE
; /* tunnel mode */
755 userspi
->info
.reqid
= reqid
;
756 userspi
->info
.family
= src
->get_family(src
);
760 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
763 while (NLMSG_OK(hdr
, len
))
765 switch (hdr
->nlmsg_type
)
769 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
770 received_spi
= usersa
->id
.spi
;
775 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
777 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
778 strerror(-err
->error
), -err
->error
);
782 hdr
= NLMSG_NEXT(hdr
, len
);
792 if (received_spi
== 0)
802 * Implementation of kernel_interface_t.get_spi.
804 static status_t
get_spi(private_kernel_netlink_ipsec_t
*this,
805 host_t
*src
, host_t
*dst
,
806 protocol_id_t protocol
, u_int32_t reqid
,
809 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
811 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
812 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
814 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
818 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
824 * Implementation of kernel_interface_t.get_cpi.
826 static status_t
get_cpi(private_kernel_netlink_ipsec_t
*this,
827 host_t
*src
, host_t
*dst
,
828 u_int32_t reqid
, u_int16_t
*cpi
)
830 u_int32_t received_spi
= 0;
832 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
834 if (get_spi_internal(this, src
, dst
,
835 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
837 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
841 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
843 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
849 * Implementation of kernel_interface_t.add_sa.
851 static status_t
add_sa(private_kernel_netlink_ipsec_t
*this,
852 host_t
*src
, host_t
*dst
, u_int32_t spi
,
853 protocol_id_t protocol
, u_int32_t reqid
,
854 u_int64_t expire_soft
, u_int64_t expire_hard
,
855 u_int16_t enc_alg
, chunk_t enc_key
,
856 u_int16_t int_alg
, chunk_t int_key
,
857 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
858 bool encap
, bool inbound
)
860 netlink_buf_t request
;
862 struct nlmsghdr
*hdr
;
863 struct xfrm_usersa_info
*sa
;
864 u_int16_t icv_size
= 64;
866 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
867 * we are in the recursive call below */
868 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
870 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, 0, 0,
871 ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
, chunk_empty
,
872 mode
, ipcomp
, 0, FALSE
, inbound
);
873 ipcomp
= IPCOMP_NONE
;
876 memset(&request
, 0, sizeof(request
));
878 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
881 hdr
= (struct nlmsghdr
*)request
;
882 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
883 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
884 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
886 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
887 host2xfrm(src
, &sa
->saddr
);
888 host2xfrm(dst
, &sa
->id
.daddr
);
890 sa
->id
.proto
= proto_ike2kernel(protocol
);
891 sa
->family
= src
->get_family(src
);
893 if (mode
== MODE_TUNNEL
)
895 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
897 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
899 /* we currently do not expire SAs by volume/packet count */
900 sa
->lft
.soft_byte_limit
= XFRM_INF
;
901 sa
->lft
.hard_byte_limit
= XFRM_INF
;
902 sa
->lft
.soft_packet_limit
= XFRM_INF
;
903 sa
->lft
.hard_packet_limit
= XFRM_INF
;
904 /* we use lifetimes since added, not since used */
905 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
906 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
907 sa
->lft
.soft_use_expires_seconds
= 0;
908 sa
->lft
.hard_use_expires_seconds
= 0;
910 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
917 case ENCR_AES_CCM_ICV16
:
918 case ENCR_AES_GCM_ICV16
:
921 case ENCR_AES_CCM_ICV12
:
922 case ENCR_AES_GCM_ICV12
:
925 case ENCR_AES_CCM_ICV8
:
926 case ENCR_AES_GCM_ICV8
:
928 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
929 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
930 if (alg_name
== NULL
)
932 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
933 encryption_algorithm_names
, enc_alg
);
936 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
937 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
939 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_key
.len
);
940 hdr
->nlmsg_len
+= rthdr
->rta_len
;
941 if (hdr
->nlmsg_len
> sizeof(request
))
946 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
947 algo
->alg_key_len
= enc_key
.len
* 8;
948 algo
->alg_icv_len
= icv_size
;
949 strcpy(algo
->alg_name
, alg_name
);
950 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
952 rthdr
= XFRM_RTA_NEXT(rthdr
);
957 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
958 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
959 if (alg_name
== NULL
)
961 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
962 encryption_algorithm_names
, enc_alg
);
965 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
966 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
968 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
969 hdr
->nlmsg_len
+= rthdr
->rta_len
;
970 if (hdr
->nlmsg_len
> sizeof(request
))
975 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
976 algo
->alg_key_len
= enc_key
.len
* 8;
977 strcpy(algo
->alg_name
, alg_name
);
978 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
980 rthdr
= XFRM_RTA_NEXT(rthdr
);
985 if (int_alg
!= AUTH_UNDEFINED
)
987 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
988 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
989 if (alg_name
== NULL
)
991 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
992 integrity_algorithm_names
, int_alg
);
995 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
996 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
998 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
999 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1000 if (hdr
->nlmsg_len
> sizeof(request
))
1005 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1006 algo
->alg_key_len
= int_key
.len
* 8;
1007 strcpy(algo
->alg_name
, alg_name
);
1008 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1010 rthdr
= XFRM_RTA_NEXT(rthdr
);
1013 if (ipcomp
!= IPCOMP_NONE
)
1015 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1016 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1017 if (alg_name
== NULL
)
1019 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1020 ipcomp_transform_names
, ipcomp
);
1023 DBG2(DBG_KNL
, " using compression algorithm %N",
1024 ipcomp_transform_names
, ipcomp
);
1026 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1027 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1028 if (hdr
->nlmsg_len
> sizeof(request
))
1033 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1034 algo
->alg_key_len
= 0;
1035 strcpy(algo
->alg_name
, alg_name
);
1037 rthdr
= XFRM_RTA_NEXT(rthdr
);
1042 rthdr
->rta_type
= XFRMA_ENCAP
;
1043 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1045 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1046 if (hdr
->nlmsg_len
> sizeof(request
))
1051 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1052 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1053 tmpl
->encap_sport
= htons(src
->get_port(src
));
1054 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1055 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1056 /* encap_oa could probably be derived from the
1057 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1058 * pluto does the same as we do here but it uses encap_oa in the
1059 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1060 * the kernel ignores it anyway
1061 * -> does that mean that NAT-T encap doesn't work in transport mode?
1062 * No. The reason the kernel ignores NAT-OA is that it recomputes
1063 * (or, rather, just ignores) the checksum. If packets pass
1064 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1065 rthdr
= XFRM_RTA_NEXT(rthdr
);
1068 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1070 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1077 * Get the replay state (i.e. sequence numbers) of an SA.
1079 static status_t
get_replay_state(private_kernel_netlink_ipsec_t
*this,
1080 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
1081 struct xfrm_replay_state
*replay
)
1083 netlink_buf_t request
;
1084 struct nlmsghdr
*hdr
, *out
= NULL
;
1085 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1090 memset(&request
, 0, sizeof(request
));
1092 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x", ntohl(spi
));
1094 hdr
= (struct nlmsghdr
*)request
;
1095 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1096 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1097 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1099 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1100 aevent_id
->flags
= XFRM_AE_RVAL
;
1102 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1103 aevent_id
->sa_id
.spi
= spi
;
1104 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
1105 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1107 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1110 while (NLMSG_OK(hdr
, len
))
1112 switch (hdr
->nlmsg_type
)
1114 case XFRM_MSG_NEWAE
:
1116 out_aevent
= NLMSG_DATA(hdr
);
1121 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1122 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
1123 strerror(-err
->error
), -err
->error
);
1127 hdr
= NLMSG_NEXT(hdr
, len
);
1136 if (out_aevent
== NULL
)
1138 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1144 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1145 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1146 while(RTA_OK(rta
, rtasize
))
1148 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1149 RTA_PAYLOAD(rta
) == sizeof(struct xfrm_replay_state
))
1151 memcpy(replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1155 rta
= RTA_NEXT(rta
, rtasize
);
1158 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1165 * Implementation of kernel_interface_t.del_sa.
1167 static status_t
del_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1168 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
)
1170 netlink_buf_t request
;
1171 struct nlmsghdr
*hdr
;
1172 struct xfrm_usersa_id
*sa_id
;
1174 /* if IPComp was used, we first delete the additional IPComp SA */
1177 del_sa(this, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0);
1180 memset(&request
, 0, sizeof(request
));
1182 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1184 hdr
= (struct nlmsghdr
*)request
;
1185 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1186 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1187 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1189 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1190 host2xfrm(dst
, &sa_id
->daddr
);
1192 sa_id
->proto
= proto_ike2kernel(protocol
);
1193 sa_id
->family
= dst
->get_family(dst
);
1195 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1197 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1200 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1205 * Implementation of kernel_interface_t.update_sa.
1207 static status_t
update_sa(private_kernel_netlink_ipsec_t
*this,
1208 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
,
1209 host_t
*src
, host_t
*dst
,
1210 host_t
*new_src
, host_t
*new_dst
,
1211 bool old_encap
, bool new_encap
)
1213 netlink_buf_t request
;
1215 struct nlmsghdr
*hdr
, *out
= NULL
;
1216 struct xfrm_usersa_id
*sa_id
;
1217 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1221 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1222 bool got_replay_state
= FALSE
;
1223 struct xfrm_replay_state replay
;
1225 /* if IPComp is used, we first update the IPComp SA */
1228 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1229 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
);
1232 memset(&request
, 0, sizeof(request
));
1234 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1236 /* query the existing SA first */
1237 hdr
= (struct nlmsghdr
*)request
;
1238 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1239 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1240 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1242 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1243 host2xfrm(dst
, &sa_id
->daddr
);
1245 sa_id
->proto
= proto_ike2kernel(protocol
);
1246 sa_id
->family
= dst
->get_family(dst
);
1248 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1251 while (NLMSG_OK(hdr
, len
))
1253 switch (hdr
->nlmsg_type
)
1255 case XFRM_MSG_NEWSA
:
1257 out_sa
= NLMSG_DATA(hdr
);
1262 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1263 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1264 strerror(-err
->error
), -err
->error
);
1268 hdr
= NLMSG_NEXT(hdr
, len
);
1278 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1283 /* try to get the replay state */
1284 if (get_replay_state(this, spi
, protocol
, dst
, &replay
) == SUCCESS
)
1286 got_replay_state
= TRUE
;
1289 /* delete the old SA (without affecting the IPComp SA) */
1290 if (del_sa(this, dst
, spi
, protocol
, 0) != SUCCESS
)
1292 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1297 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1298 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1299 /* copy over the SA from out to request */
1300 hdr
= (struct nlmsghdr
*)request
;
1301 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1302 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1303 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1304 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1305 sa
= NLMSG_DATA(hdr
);
1306 sa
->family
= new_dst
->get_family(new_dst
);
1308 if (!src
->ip_equals(src
, new_src
))
1310 host2xfrm(new_src
, &sa
->saddr
);
1312 if (!dst
->ip_equals(dst
, new_dst
))
1314 host2xfrm(new_dst
, &sa
->id
.daddr
);
1317 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1318 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1319 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1320 while(RTA_OK(rta
, rtasize
))
1322 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1323 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1325 if (rta
->rta_type
== XFRMA_ENCAP
)
1326 { /* update encap tmpl */
1327 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1328 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1329 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1331 memcpy(pos
, rta
, rta
->rta_len
);
1332 pos
+= RTA_ALIGN(rta
->rta_len
);
1333 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1335 rta
= RTA_NEXT(rta
, rtasize
);
1338 rta
= (struct rtattr
*)pos
;
1339 if (tmpl
== NULL
&& new_encap
)
1340 { /* add tmpl if we are enabling it */
1341 rta
->rta_type
= XFRMA_ENCAP
;
1342 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1344 hdr
->nlmsg_len
+= rta
->rta_len
;
1345 if (hdr
->nlmsg_len
> sizeof(request
))
1350 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1351 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1352 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1353 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1354 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1356 rta
= XFRM_RTA_NEXT(rta
);
1359 if (got_replay_state
)
1360 { /* copy the replay data if available */
1361 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1362 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1364 hdr
->nlmsg_len
+= rta
->rta_len
;
1365 if (hdr
->nlmsg_len
> sizeof(request
))
1369 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
1371 rta
= XFRM_RTA_NEXT(rta
);
1374 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1376 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1386 * Implementation of kernel_interface_t.add_policy.
1388 static status_t
add_policy(private_kernel_netlink_ipsec_t
*this,
1389 host_t
*src
, host_t
*dst
,
1390 traffic_selector_t
*src_ts
,
1391 traffic_selector_t
*dst_ts
,
1392 policy_dir_t direction
, u_int32_t spi
,
1393 protocol_id_t protocol
, u_int32_t reqid
,
1394 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
1397 iterator_t
*iterator
;
1398 policy_entry_t
*current
, *policy
;
1400 netlink_buf_t request
;
1401 struct xfrm_userpolicy_info
*policy_info
;
1402 struct nlmsghdr
*hdr
;
1404 /* create a policy */
1405 policy
= malloc_thing(policy_entry_t
);
1406 memset(policy
, 0, sizeof(policy_entry_t
));
1407 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1408 policy
->direction
= direction
;
1410 /* find the policy, which matches EXACTLY */
1411 this->mutex
->lock(this->mutex
);
1412 iterator
= this->policies
->create_iterator(this->policies
, TRUE
);
1413 while (iterator
->iterate(iterator
, (void**)¤t
))
1415 if (memeq(¤t
->sel
, &policy
->sel
, sizeof(struct xfrm_selector
)) &&
1416 policy
->direction
== current
->direction
)
1418 /* use existing policy */
1419 current
->refcount
++;
1420 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1421 "refcount", src_ts
, dst_ts
,
1422 policy_dir_names
, direction
);
1429 iterator
->destroy(iterator
);
1431 { /* apply the new one, if we have no such policy */
1432 this->policies
->insert_last(this->policies
, policy
);
1433 policy
->refcount
= 1;
1436 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1437 policy_dir_names
, direction
);
1439 memset(&request
, 0, sizeof(request
));
1440 hdr
= (struct nlmsghdr
*)request
;
1441 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1442 hdr
->nlmsg_type
= found ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1443 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1445 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1446 policy_info
->sel
= policy
->sel
;
1447 policy_info
->dir
= policy
->direction
;
1448 /* calculate priority based on source selector size, small size = high prio */
1449 policy_info
->priority
= routed ? PRIO_LOW
: PRIO_HIGH
;
1450 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1451 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1452 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1453 policy_info
->action
= XFRM_POLICY_ALLOW
;
1454 policy_info
->share
= XFRM_SHARE_ANY
;
1455 this->mutex
->unlock(this->mutex
);
1457 /* policies don't expire */
1458 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1459 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1460 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1461 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1462 policy_info
->lft
.soft_add_expires_seconds
= 0;
1463 policy_info
->lft
.hard_add_expires_seconds
= 0;
1464 policy_info
->lft
.soft_use_expires_seconds
= 0;
1465 policy_info
->lft
.hard_use_expires_seconds
= 0;
1467 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1468 rthdr
->rta_type
= XFRMA_TMPL
;
1469 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1471 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1472 if (hdr
->nlmsg_len
> sizeof(request
))
1477 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1479 if (ipcomp
!= IPCOMP_NONE
)
1481 tmpl
->reqid
= reqid
;
1482 tmpl
->id
.proto
= IPPROTO_COMP
;
1483 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1485 tmpl
->optional
= direction
!= POLICY_OUT
;
1486 tmpl
->family
= src
->get_family(src
);
1488 host2xfrm(src
, &tmpl
->saddr
);
1489 host2xfrm(dst
, &tmpl
->id
.daddr
);
1491 /* add an additional xfrm_user_tmpl */
1492 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1493 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1494 if (hdr
->nlmsg_len
> sizeof(request
))
1502 tmpl
->reqid
= reqid
;
1503 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
1504 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1506 tmpl
->family
= src
->get_family(src
);
1508 host2xfrm(src
, &tmpl
->saddr
);
1509 host2xfrm(dst
, &tmpl
->id
.daddr
);
1511 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1513 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1514 policy_dir_names
, direction
);
1518 /* install a route, if:
1519 * - we are NOT updating a policy
1520 * - this is a forward policy (to just get one for each child)
1521 * - we are in tunnel mode
1522 * - we are not using IPv6 (does not work correctly yet!)
1523 * - routing is not disabled via strongswan.conf
1525 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1526 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1527 this->install_routes
)
1529 route_entry_t
*route
= malloc_thing(route_entry_t
);
1531 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1532 dst_ts
, &route
->src_ip
) == SUCCESS
)
1534 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1535 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1536 charon
->kernel_interface
, src
);
1537 route
->if_name
= charon
->kernel_interface
->get_interface(
1538 charon
->kernel_interface
, dst
);
1539 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1540 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
1541 route
->prefixlen
= policy
->sel
.prefixlen_s
;
1545 switch (charon
->kernel_interface
->add_route(
1546 charon
->kernel_interface
, route
->dst_net
,
1547 route
->prefixlen
, route
->gateway
,
1548 route
->src_ip
, route
->if_name
))
1551 DBG1(DBG_KNL
, "unable to install source route for %H",
1555 /* route exists, do not uninstall */
1556 route_entry_destroy(route
);
1559 /* cache the installed route */
1560 policy
->route
= route
;
1566 route_entry_destroy(route
);
1578 * Implementation of kernel_interface_t.query_policy.
1580 static status_t
query_policy(private_kernel_netlink_ipsec_t
*this,
1581 traffic_selector_t
*src_ts
,
1582 traffic_selector_t
*dst_ts
,
1583 policy_dir_t direction
, u_int32_t
*use_time
)
1585 netlink_buf_t request
;
1586 struct nlmsghdr
*out
= NULL
, *hdr
;
1587 struct xfrm_userpolicy_id
*policy_id
;
1588 struct xfrm_userpolicy_info
*policy
= NULL
;
1591 memset(&request
, 0, sizeof(request
));
1593 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1594 policy_dir_names
, direction
);
1596 hdr
= (struct nlmsghdr
*)request
;
1597 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1598 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1599 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1601 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1602 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1603 policy_id
->dir
= direction
;
1605 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1608 while (NLMSG_OK(hdr
, len
))
1610 switch (hdr
->nlmsg_type
)
1612 case XFRM_MSG_NEWPOLICY
:
1614 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1619 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1620 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1621 strerror(-err
->error
), -err
->error
);
1625 hdr
= NLMSG_NEXT(hdr
, len
);
1636 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1637 policy_dir_names
, direction
);
1641 *use_time
= (time_t)policy
->curlft
.use_time
;
1648 * Implementation of kernel_interface_t.del_policy.
1650 static status_t
del_policy(private_kernel_netlink_ipsec_t
*this,
1651 traffic_selector_t
*src_ts
,
1652 traffic_selector_t
*dst_ts
,
1653 policy_dir_t direction
, bool unrouted
)
1655 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1656 route_entry_t
*route
;
1657 netlink_buf_t request
;
1658 struct nlmsghdr
*hdr
;
1659 struct xfrm_userpolicy_id
*policy_id
;
1660 enumerator_t
*enumerator
;
1662 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1663 policy_dir_names
, direction
);
1665 /* create a policy */
1666 memset(&policy
, 0, sizeof(policy_entry_t
));
1667 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1668 policy
.direction
= direction
;
1670 /* find the policy */
1671 this->mutex
->lock(this->mutex
);
1672 enumerator
= this->policies
->create_enumerator(this->policies
);
1673 while (enumerator
->enumerate(enumerator
, ¤t
))
1675 if (memeq(¤t
->sel
, &policy
.sel
, sizeof(struct xfrm_selector
)) &&
1676 policy
.direction
== current
->direction
)
1678 to_delete
= current
;
1679 if (--to_delete
->refcount
> 0)
1681 /* is used by more SAs, keep in kernel */
1682 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1683 this->mutex
->unlock(this->mutex
);
1684 enumerator
->destroy(enumerator
);
1687 /* remove if last reference */
1688 this->policies
->remove_at(this->policies
, enumerator
);
1692 this->mutex
->unlock(this->mutex
);
1693 enumerator
->destroy(enumerator
);
1696 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1697 dst_ts
, policy_dir_names
, direction
);
1701 memset(&request
, 0, sizeof(request
));
1703 hdr
= (struct nlmsghdr
*)request
;
1704 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1705 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1706 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1708 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1709 policy_id
->sel
= to_delete
->sel
;
1710 policy_id
->dir
= direction
;
1712 route
= to_delete
->route
;
1715 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1717 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1718 policy_dir_names
, direction
);
1724 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1725 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1726 route
->src_ip
, route
->if_name
) != SUCCESS
)
1728 DBG1(DBG_KNL
, "error uninstalling route installed with "
1729 "policy %R === %R %N", src_ts
, dst_ts
,
1730 policy_dir_names
, direction
);
1732 route_entry_destroy(route
);
1738 * Implementation of kernel_interface_t.destroy.
1740 static void destroy(private_kernel_netlink_ipsec_t
*this)
1742 this->job
->cancel(this->job
);
1743 close(this->socket_xfrm_events
);
1744 this->socket_xfrm
->destroy(this->socket_xfrm
);
1745 this->policies
->destroy(this->policies
);
1746 this->mutex
->destroy(this->mutex
);
1751 * Add bypass policies for IKE on the sockets used by charon
1753 static bool add_bypass_policies()
1755 int fd
, family
, port
;
1756 enumerator_t
*sockets
;
1759 /* we open an AF_KEY socket to autoload the af_key module. Otherwise
1760 * setsockopt(IPSEC_POLICY) won't work. */
1761 fd
= socket(AF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1764 DBG1(DBG_KNL
, "could not open AF_KEY socket");
1769 sockets
= charon
->socket
->create_enumerator(charon
->socket
);
1770 while (sockets
->enumerate(sockets
, &fd
, &family
, &port
))
1772 struct sadb_x_policy policy
;
1773 u_int sol
, ipsec_policy
;
1779 ipsec_policy
= IP_IPSEC_POLICY
;
1783 ipsec_policy
= IPV6_IPSEC_POLICY
;
1789 memset(&policy
, 0, sizeof(policy
));
1790 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(u_int64_t
);
1791 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1792 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
1794 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
1795 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1797 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1802 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
1803 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1805 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1811 sockets
->destroy(sockets
);
1816 * Described in header.
1818 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
1820 private_kernel_netlink_ipsec_t
*this = malloc_thing(private_kernel_netlink_ipsec_t
);
1821 struct sockaddr_nl addr
;
1823 /* public functions */
1824 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
;
1825 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1826 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
;
1827 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
;
1828 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int16_t
))del_sa
;
1829 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
;
1830 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1831 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,bool))del_policy
;
1832 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1834 /* private members */
1835 this->policies
= linked_list_create();
1836 this->mutex
= mutex_create(MUTEX_DEFAULT
);
1837 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
1838 "charon.install_routes", TRUE
);
1840 /* add bypass policies on the sockets used by charon */
1841 if (!add_bypass_policies())
1843 charon
->kill(charon
, "unable to add bypass policies on sockets");
1846 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
1848 memset(&addr
, 0, sizeof(addr
));
1849 addr
.nl_family
= AF_NETLINK
;
1851 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1852 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1853 if (this->socket_xfrm_events
<= 0)
1855 charon
->kill(charon
, "unable to create XFRM event socket");
1857 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
1858 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
1859 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1861 charon
->kill(charon
, "unable to bind XFRM event socket");
1864 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1866 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1868 return &this->public;