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/hashtable.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 */
254 * Hash function for policy_entry_t objects
256 static u_int
policy_hash(policy_entry_t
*key
)
258 chunk_t chunk
= chunk_create((void*)&key
->sel
, sizeof(struct xfrm_selector
));
259 return chunk_hash(chunk
);
263 * Equality function for policy_entry_t objects
265 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
267 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
268 key
->direction
== other_key
->direction
;
271 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
274 * Private variables and functions of kernel_netlink class.
276 struct private_kernel_netlink_ipsec_t
{
278 * Public part of the kernel_netlink_t object.
280 kernel_netlink_ipsec_t
public;
283 * mutex to lock access to various lists
288 * Hash table of installed policies (policy_entry_t)
290 hashtable_t
*policies
;
293 * job receiving netlink events
298 * Netlink xfrm socket (IPsec)
300 netlink_socket_t
*socket_xfrm
;
303 * netlink xfrm socket to receive acquire and expire events
305 int socket_xfrm_events
;
308 * whether to install routes along policies
314 * convert a IKEv2 specific protocol identifier to the kernel one
316 static u_int8_t
proto_ike2kernel(protocol_id_t proto
)
330 * reverse of ike2kernel
332 static protocol_id_t
proto_kernel2ike(u_int8_t proto
)
346 * convert a host_t to a struct xfrm_address
348 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
350 chunk_t chunk
= host
->get_address(host
);
351 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
355 * convert a struct xfrm_address to a host_t
357 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
364 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
367 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
372 return host_create_from_chunk(family
, chunk
, ntohs(port
));
376 * convert a traffic selector address range to subnet and its mask.
378 static void ts2subnet(traffic_selector_t
* ts
,
379 xfrm_address_t
*net
, u_int8_t
*mask
)
384 ts
->to_subnet(ts
, &net_host
, mask
);
385 net_chunk
= net_host
->get_address(net_host
);
386 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
387 net_host
->destroy(net_host
);
391 * convert a traffic selector port range to port/portmask
393 static void ts2ports(traffic_selector_t
* ts
,
394 u_int16_t
*port
, u_int16_t
*mask
)
396 /* linux does not seem to accept complex portmasks. Only
397 * any or a specific port is allowed. We set to any, if we have
398 * a port range, or to a specific, if we have one port only.
402 from
= ts
->get_from_port(ts
);
403 to
= ts
->get_to_port(ts
);
418 * convert a pair of traffic_selectors to a xfrm_selector
420 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
421 traffic_selector_t
*dst
)
423 struct xfrm_selector sel
;
425 memset(&sel
, 0, sizeof(sel
));
426 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
427 /* src or dest proto may be "any" (0), use more restrictive one */
428 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
429 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
430 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
431 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
432 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
440 * convert a xfrm_selector to a src|dst traffic_selector
442 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
451 addr
= (u_char
*)&sel
->saddr
;
452 prefixlen
= sel
->prefixlen_s
;
455 port
= htons(sel
->sport
);
460 addr
= (u_char
*)&sel
->daddr
;
461 prefixlen
= sel
->prefixlen_d
;
464 port
= htons(sel
->dport
);
468 /* The Linux 2.6 kernel does not set the selector's family field,
469 * so as a kludge we additionally test the prefix length.
471 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
473 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
475 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
477 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
482 return traffic_selector_create_from_subnet(host
, prefixlen
,
489 * process a XFRM_MSG_ACQUIRE from kernel
491 static void process_acquire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
495 traffic_selector_t
*src_ts
, *dst_ts
;
496 struct xfrm_user_acquire
*acquire
;
501 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
502 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
503 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
505 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
507 while (RTA_OK(rta
, rtasize
))
509 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
511 if (rta
->rta_type
== XFRMA_TMPL
)
513 struct xfrm_user_tmpl
* tmpl
;
515 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
517 proto
= tmpl
->id
.proto
;
519 rta
= RTA_NEXT(rta
, rtasize
);
528 /* acquire for AH/ESP only, not for IPCOMP */
531 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
532 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
533 DBG1(DBG_KNL
, "creating acquire job for policy %R === %R with reqid {%u}",
534 src_ts
, dst_ts
, reqid
);
535 job
= (job_t
*)acquire_job_create(reqid
, src_ts
, dst_ts
);
536 charon
->processor
->queue_job(charon
->processor
, job
);
540 * process a XFRM_MSG_EXPIRE from kernel
542 static void process_expire(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
545 protocol_id_t protocol
;
546 u_int32_t spi
, reqid
;
547 struct xfrm_user_expire
*expire
;
549 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
550 protocol
= proto_kernel2ike(expire
->state
.id
.proto
);
551 spi
= expire
->state
.id
.spi
;
552 reqid
= expire
->state
.reqid
;
554 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
556 if (protocol
!= PROTO_ESP
&& protocol
!= PROTO_AH
)
558 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and reqid {%u} "
559 "which is not a CHILD_SA", ntohl(spi
), reqid
);
563 DBG1(DBG_KNL
, "creating %s job for %N CHILD_SA with SPI %.8x and reqid {%d}",
564 expire
->hard ?
"delete" : "rekey", protocol_id_names
,
565 protocol
, ntohl(spi
), reqid
);
568 job
= (job_t
*)delete_child_sa_job_create(reqid
, protocol
, spi
);
572 job
= (job_t
*)rekey_child_sa_job_create(reqid
, protocol
, spi
);
574 charon
->processor
->queue_job(charon
->processor
, job
);
578 * process a XFRM_MSG_MIGRATE from kernel
580 static void process_migrate(private_kernel_netlink_ipsec_t
*this, struct nlmsghdr
*hdr
)
582 traffic_selector_t
*src_ts
, *dst_ts
;
583 host_t
*local
= NULL
, *remote
= NULL
;
584 host_t
*old_src
= NULL
, *old_dst
= NULL
;
585 host_t
*new_src
= NULL
, *new_dst
= NULL
;
586 struct xfrm_userpolicy_id
*policy_id
;
593 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
594 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
595 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
597 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
599 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
600 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
601 dir
= (policy_dir_t
)policy_id
->dir
;
603 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
605 while (RTA_OK(rta
, rtasize
))
607 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
608 if (rta
->rta_type
== XFRMA_KMADDRESS
)
610 struct xfrm_user_kmaddress
*kmaddress
;
612 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
613 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
614 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
615 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
617 else if (rta
->rta_type
== XFRMA_MIGRATE
)
619 struct xfrm_user_migrate
*migrate
;
622 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
623 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
624 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
625 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
626 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
627 proto
= proto_kernel2ike(migrate
->proto
);
628 reqid
= migrate
->reqid
;
629 DBG2(DBG_KNL
, " migrate %N %H...%H to %H...%H, reqid {%u}",
630 protocol_id_names
, proto
, old_src
, old_dst
,
631 new_src
, new_dst
, reqid
);
637 rta
= RTA_NEXT(rta
, rtasize
);
640 if (src_ts
&& dst_ts
&& local
&& remote
)
642 DBG1(DBG_KNL
, "creating migrate job for policy %R === %R %N with reqid {%u}",
643 src_ts
, dst_ts
, policy_dir_names
, dir
, reqid
, local
);
644 job
= (job_t
*)migrate_job_create(reqid
, src_ts
, dst_ts
, dir
,
646 charon
->processor
->queue_job(charon
->processor
, job
);
658 * process a XFRM_MSG_MAPPING from kernel
660 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
661 struct nlmsghdr
*hdr
)
664 u_int32_t spi
, reqid
;
665 struct xfrm_user_mapping
*mapping
;
668 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
669 spi
= mapping
->id
.spi
;
670 reqid
= mapping
->reqid
;
672 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
674 if (proto_kernel2ike(mapping
->id
.proto
) == PROTO_ESP
)
676 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
680 DBG1(DBG_KNL
, "NAT mappings of ESP CHILD_SA with SPI %.8x and "
681 "reqid {%u} changed, queuing update job", ntohl(spi
), reqid
);
682 job
= (job_t
*)update_sa_job_create(reqid
, host
);
683 charon
->processor
->queue_job(charon
->processor
, job
);
689 * Receives events from kernel
691 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
694 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
695 struct sockaddr_nl addr
;
696 socklen_t addr_len
= sizeof(addr
);
699 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
700 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
701 (struct sockaddr
*)&addr
, &addr_len
);
702 pthread_setcancelstate(oldstate
, NULL
);
709 /* interrupted, try again */
710 return JOB_REQUEUE_DIRECT
;
712 /* no data ready, select again */
713 return JOB_REQUEUE_DIRECT
;
715 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
717 return JOB_REQUEUE_FAIR
;
721 if (addr
.nl_pid
!= 0)
722 { /* not from kernel. not interested, try another one */
723 return JOB_REQUEUE_DIRECT
;
726 while (NLMSG_OK(hdr
, len
))
728 switch (hdr
->nlmsg_type
)
730 case XFRM_MSG_ACQUIRE
:
731 process_acquire(this, hdr
);
733 case XFRM_MSG_EXPIRE
:
734 process_expire(this, hdr
);
736 case XFRM_MSG_MIGRATE
:
737 process_migrate(this, hdr
);
739 case XFRM_MSG_MAPPING
:
740 process_mapping(this, hdr
);
745 hdr
= NLMSG_NEXT(hdr
, len
);
747 return JOB_REQUEUE_DIRECT
;
751 * Get an SPI for a specific protocol from the kernel.
753 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
754 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
755 u_int32_t reqid
, u_int32_t
*spi
)
757 netlink_buf_t request
;
758 struct nlmsghdr
*hdr
, *out
;
759 struct xfrm_userspi_info
*userspi
;
760 u_int32_t received_spi
= 0;
763 memset(&request
, 0, sizeof(request
));
765 hdr
= (struct nlmsghdr
*)request
;
766 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
767 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
768 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
770 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
771 host2xfrm(src
, &userspi
->info
.saddr
);
772 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
773 userspi
->info
.id
.proto
= proto
;
774 userspi
->info
.mode
= TRUE
; /* tunnel mode */
775 userspi
->info
.reqid
= reqid
;
776 userspi
->info
.family
= src
->get_family(src
);
780 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
783 while (NLMSG_OK(hdr
, len
))
785 switch (hdr
->nlmsg_type
)
789 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
790 received_spi
= usersa
->id
.spi
;
795 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
797 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
798 strerror(-err
->error
), -err
->error
);
802 hdr
= NLMSG_NEXT(hdr
, len
);
812 if (received_spi
== 0)
822 * Implementation of kernel_interface_t.get_spi.
824 static status_t
get_spi(private_kernel_netlink_ipsec_t
*this,
825 host_t
*src
, host_t
*dst
,
826 protocol_id_t protocol
, u_int32_t reqid
,
829 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
831 if (get_spi_internal(this, src
, dst
, proto_ike2kernel(protocol
),
832 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
834 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
838 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
844 * Implementation of kernel_interface_t.get_cpi.
846 static status_t
get_cpi(private_kernel_netlink_ipsec_t
*this,
847 host_t
*src
, host_t
*dst
,
848 u_int32_t reqid
, u_int16_t
*cpi
)
850 u_int32_t received_spi
= 0;
852 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
854 if (get_spi_internal(this, src
, dst
,
855 IPPROTO_COMP
, 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
857 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
861 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
863 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
869 * Implementation of kernel_interface_t.add_sa.
871 static status_t
add_sa(private_kernel_netlink_ipsec_t
*this,
872 host_t
*src
, host_t
*dst
, u_int32_t spi
,
873 protocol_id_t protocol
, u_int32_t reqid
,
874 u_int64_t expire_soft
, u_int64_t expire_hard
,
875 u_int16_t enc_alg
, chunk_t enc_key
,
876 u_int16_t int_alg
, chunk_t int_key
,
877 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
878 bool encap
, bool inbound
)
880 netlink_buf_t request
;
882 struct nlmsghdr
*hdr
;
883 struct xfrm_usersa_info
*sa
;
884 u_int16_t icv_size
= 64;
886 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
887 * we are in the recursive call below */
888 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
890 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, 0, 0,
891 ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
, chunk_empty
,
892 mode
, ipcomp
, 0, FALSE
, inbound
);
893 ipcomp
= IPCOMP_NONE
;
896 memset(&request
, 0, sizeof(request
));
898 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
901 hdr
= (struct nlmsghdr
*)request
;
902 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
903 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
904 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
906 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
907 host2xfrm(src
, &sa
->saddr
);
908 host2xfrm(dst
, &sa
->id
.daddr
);
910 sa
->id
.proto
= proto_ike2kernel(protocol
);
911 sa
->family
= src
->get_family(src
);
913 if (mode
== MODE_TUNNEL
)
915 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
917 sa
->replay_window
= (protocol
== IPPROTO_COMP
) ?
0 : 32;
919 /* we currently do not expire SAs by volume/packet count */
920 sa
->lft
.soft_byte_limit
= XFRM_INF
;
921 sa
->lft
.hard_byte_limit
= XFRM_INF
;
922 sa
->lft
.soft_packet_limit
= XFRM_INF
;
923 sa
->lft
.hard_packet_limit
= XFRM_INF
;
924 /* we use lifetimes since added, not since used */
925 sa
->lft
.soft_add_expires_seconds
= expire_soft
;
926 sa
->lft
.hard_add_expires_seconds
= expire_hard
;
927 sa
->lft
.soft_use_expires_seconds
= 0;
928 sa
->lft
.hard_use_expires_seconds
= 0;
930 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
937 case ENCR_AES_CCM_ICV16
:
938 case ENCR_AES_GCM_ICV16
:
941 case ENCR_AES_CCM_ICV12
:
942 case ENCR_AES_GCM_ICV12
:
945 case ENCR_AES_CCM_ICV8
:
946 case ENCR_AES_GCM_ICV8
:
948 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
949 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
950 if (alg_name
== NULL
)
952 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
953 encryption_algorithm_names
, enc_alg
);
956 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
957 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
959 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) + enc_key
.len
);
960 hdr
->nlmsg_len
+= rthdr
->rta_len
;
961 if (hdr
->nlmsg_len
> sizeof(request
))
966 struct xfrm_algo_aead
* algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
967 algo
->alg_key_len
= enc_key
.len
* 8;
968 algo
->alg_icv_len
= icv_size
;
969 strcpy(algo
->alg_name
, alg_name
);
970 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
972 rthdr
= XFRM_RTA_NEXT(rthdr
);
977 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
978 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
979 if (alg_name
== NULL
)
981 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
982 encryption_algorithm_names
, enc_alg
);
985 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
986 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
988 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
989 hdr
->nlmsg_len
+= rthdr
->rta_len
;
990 if (hdr
->nlmsg_len
> sizeof(request
))
995 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
996 algo
->alg_key_len
= enc_key
.len
* 8;
997 strcpy(algo
->alg_name
, alg_name
);
998 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1000 rthdr
= XFRM_RTA_NEXT(rthdr
);
1005 if (int_alg
!= AUTH_UNDEFINED
)
1007 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1008 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
1009 if (alg_name
== NULL
)
1011 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1012 integrity_algorithm_names
, int_alg
);
1015 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1016 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1018 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1019 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1020 if (hdr
->nlmsg_len
> sizeof(request
))
1025 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1026 algo
->alg_key_len
= int_key
.len
* 8;
1027 strcpy(algo
->alg_name
, alg_name
);
1028 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1030 rthdr
= XFRM_RTA_NEXT(rthdr
);
1033 if (ipcomp
!= IPCOMP_NONE
)
1035 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1036 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1037 if (alg_name
== NULL
)
1039 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1040 ipcomp_transform_names
, ipcomp
);
1043 DBG2(DBG_KNL
, " using compression algorithm %N",
1044 ipcomp_transform_names
, ipcomp
);
1046 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1047 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1048 if (hdr
->nlmsg_len
> sizeof(request
))
1053 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1054 algo
->alg_key_len
= 0;
1055 strcpy(algo
->alg_name
, alg_name
);
1057 rthdr
= XFRM_RTA_NEXT(rthdr
);
1062 rthdr
->rta_type
= XFRMA_ENCAP
;
1063 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1065 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1066 if (hdr
->nlmsg_len
> sizeof(request
))
1071 struct xfrm_encap_tmpl
* tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1072 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1073 tmpl
->encap_sport
= htons(src
->get_port(src
));
1074 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1075 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1076 /* encap_oa could probably be derived from the
1077 * traffic selectors [rfc4306, p39]. In the netlink kernel implementation
1078 * pluto does the same as we do here but it uses encap_oa in the
1079 * pfkey implementation. BUT as /usr/src/linux/net/key/af_key.c indicates
1080 * the kernel ignores it anyway
1081 * -> does that mean that NAT-T encap doesn't work in transport mode?
1082 * No. The reason the kernel ignores NAT-OA is that it recomputes
1083 * (or, rather, just ignores) the checksum. If packets pass
1084 * the IPsec checks it marks them "checksum ok" so OA isn't needed. */
1085 rthdr
= XFRM_RTA_NEXT(rthdr
);
1088 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1090 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1097 * Get the replay state (i.e. sequence numbers) of an SA.
1099 static status_t
get_replay_state(private_kernel_netlink_ipsec_t
*this,
1100 u_int32_t spi
, protocol_id_t protocol
, host_t
*dst
,
1101 struct xfrm_replay_state
*replay
)
1103 netlink_buf_t request
;
1104 struct nlmsghdr
*hdr
, *out
= NULL
;
1105 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1110 memset(&request
, 0, sizeof(request
));
1112 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x", ntohl(spi
));
1114 hdr
= (struct nlmsghdr
*)request
;
1115 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1116 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1117 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1119 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1120 aevent_id
->flags
= XFRM_AE_RVAL
;
1122 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1123 aevent_id
->sa_id
.spi
= spi
;
1124 aevent_id
->sa_id
.proto
= proto_ike2kernel(protocol
);
1125 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1127 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1130 while (NLMSG_OK(hdr
, len
))
1132 switch (hdr
->nlmsg_type
)
1134 case XFRM_MSG_NEWAE
:
1136 out_aevent
= NLMSG_DATA(hdr
);
1141 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1142 DBG1(DBG_KNL
, "querying replay state from SAD entry failed: %s (%d)",
1143 strerror(-err
->error
), -err
->error
);
1147 hdr
= NLMSG_NEXT(hdr
, len
);
1156 if (out_aevent
== NULL
)
1158 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1164 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1165 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1166 while(RTA_OK(rta
, rtasize
))
1168 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1169 RTA_PAYLOAD(rta
) == sizeof(struct xfrm_replay_state
))
1171 memcpy(replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1175 rta
= RTA_NEXT(rta
, rtasize
);
1178 DBG1(DBG_KNL
, "unable to query replay state from SAD entry with SPI %.8x",
1185 * Implementation of kernel_interface_t.del_sa.
1187 static status_t
del_sa(private_kernel_netlink_ipsec_t
*this, host_t
*dst
,
1188 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
)
1190 netlink_buf_t request
;
1191 struct nlmsghdr
*hdr
;
1192 struct xfrm_usersa_id
*sa_id
;
1194 /* if IPComp was used, we first delete the additional IPComp SA */
1197 del_sa(this, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0);
1200 memset(&request
, 0, sizeof(request
));
1202 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1204 hdr
= (struct nlmsghdr
*)request
;
1205 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1206 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1207 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1209 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1210 host2xfrm(dst
, &sa_id
->daddr
);
1212 sa_id
->proto
= proto_ike2kernel(protocol
);
1213 sa_id
->family
= dst
->get_family(dst
);
1215 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1217 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x", ntohl(spi
));
1220 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1225 * Implementation of kernel_interface_t.update_sa.
1227 static status_t
update_sa(private_kernel_netlink_ipsec_t
*this,
1228 u_int32_t spi
, protocol_id_t protocol
, u_int16_t cpi
,
1229 host_t
*src
, host_t
*dst
,
1230 host_t
*new_src
, host_t
*new_dst
,
1231 bool old_encap
, bool new_encap
)
1233 netlink_buf_t request
;
1235 struct nlmsghdr
*hdr
, *out
= NULL
;
1236 struct xfrm_usersa_id
*sa_id
;
1237 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1241 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1242 bool got_replay_state
= FALSE
;
1243 struct xfrm_replay_state replay
;
1245 /* if IPComp is used, we first update the IPComp SA */
1248 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1249 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
);
1252 memset(&request
, 0, sizeof(request
));
1254 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1256 /* query the existing SA first */
1257 hdr
= (struct nlmsghdr
*)request
;
1258 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1259 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1260 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1262 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1263 host2xfrm(dst
, &sa_id
->daddr
);
1265 sa_id
->proto
= proto_ike2kernel(protocol
);
1266 sa_id
->family
= dst
->get_family(dst
);
1268 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1271 while (NLMSG_OK(hdr
, len
))
1273 switch (hdr
->nlmsg_type
)
1275 case XFRM_MSG_NEWSA
:
1277 out_sa
= NLMSG_DATA(hdr
);
1282 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1283 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1284 strerror(-err
->error
), -err
->error
);
1288 hdr
= NLMSG_NEXT(hdr
, len
);
1298 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1303 /* try to get the replay state */
1304 if (get_replay_state(this, spi
, protocol
, dst
, &replay
) == SUCCESS
)
1306 got_replay_state
= TRUE
;
1309 /* delete the old SA (without affecting the IPComp SA) */
1310 if (del_sa(this, dst
, spi
, protocol
, 0) != SUCCESS
)
1312 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x", ntohl(spi
));
1317 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1318 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1319 /* copy over the SA from out to request */
1320 hdr
= (struct nlmsghdr
*)request
;
1321 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1322 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1323 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1324 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1325 sa
= NLMSG_DATA(hdr
);
1326 sa
->family
= new_dst
->get_family(new_dst
);
1328 if (!src
->ip_equals(src
, new_src
))
1330 host2xfrm(new_src
, &sa
->saddr
);
1332 if (!dst
->ip_equals(dst
, new_dst
))
1334 host2xfrm(new_dst
, &sa
->id
.daddr
);
1337 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1338 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1339 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1340 while(RTA_OK(rta
, rtasize
))
1342 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1343 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1345 if (rta
->rta_type
== XFRMA_ENCAP
)
1346 { /* update encap tmpl */
1347 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1348 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1349 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1351 memcpy(pos
, rta
, rta
->rta_len
);
1352 pos
+= RTA_ALIGN(rta
->rta_len
);
1353 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1355 rta
= RTA_NEXT(rta
, rtasize
);
1358 rta
= (struct rtattr
*)pos
;
1359 if (tmpl
== NULL
&& new_encap
)
1360 { /* add tmpl if we are enabling it */
1361 rta
->rta_type
= XFRMA_ENCAP
;
1362 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1364 hdr
->nlmsg_len
+= rta
->rta_len
;
1365 if (hdr
->nlmsg_len
> sizeof(request
))
1370 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1371 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1372 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1373 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1374 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1376 rta
= XFRM_RTA_NEXT(rta
);
1379 if (got_replay_state
)
1380 { /* copy the replay data if available */
1381 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1382 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1384 hdr
->nlmsg_len
+= rta
->rta_len
;
1385 if (hdr
->nlmsg_len
> sizeof(request
))
1389 memcpy(RTA_DATA(rta
), &replay
, sizeof(replay
));
1391 rta
= XFRM_RTA_NEXT(rta
);
1394 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1396 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1406 * Implementation of kernel_interface_t.add_policy.
1408 static status_t
add_policy(private_kernel_netlink_ipsec_t
*this,
1409 host_t
*src
, host_t
*dst
,
1410 traffic_selector_t
*src_ts
,
1411 traffic_selector_t
*dst_ts
,
1412 policy_dir_t direction
, u_int32_t spi
,
1413 protocol_id_t protocol
, u_int32_t reqid
,
1414 ipsec_mode_t mode
, u_int16_t ipcomp
, u_int16_t cpi
,
1417 policy_entry_t
*current
, *policy
;
1419 netlink_buf_t request
;
1420 struct xfrm_userpolicy_info
*policy_info
;
1421 struct nlmsghdr
*hdr
;
1423 /* create a policy */
1424 policy
= malloc_thing(policy_entry_t
);
1425 memset(policy
, 0, sizeof(policy_entry_t
));
1426 policy
->sel
= ts2selector(src_ts
, dst_ts
);
1427 policy
->direction
= direction
;
1429 /* find the policy, which matches EXACTLY */
1430 this->mutex
->lock(this->mutex
);
1431 current
= this->policies
->get(this->policies
, policy
);
1434 /* use existing policy */
1435 current
->refcount
++;
1436 DBG2(DBG_KNL
, "policy %R === %R %N already exists, increasing "
1437 "refcount", src_ts
, dst_ts
,
1438 policy_dir_names
, direction
);
1444 { /* apply the new one, if we have no such policy */
1445 this->policies
->put(this->policies
, policy
, policy
);
1446 policy
->refcount
= 1;
1449 DBG2(DBG_KNL
, "adding policy %R === %R %N", src_ts
, dst_ts
,
1450 policy_dir_names
, direction
);
1452 memset(&request
, 0, sizeof(request
));
1453 hdr
= (struct nlmsghdr
*)request
;
1454 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1455 hdr
->nlmsg_type
= found ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1456 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1458 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1459 policy_info
->sel
= policy
->sel
;
1460 policy_info
->dir
= policy
->direction
;
1461 /* calculate priority based on source selector size, small size = high prio */
1462 policy_info
->priority
= routed ? PRIO_LOW
: PRIO_HIGH
;
1463 policy_info
->priority
-= policy
->sel
.prefixlen_s
* 10;
1464 policy_info
->priority
-= policy
->sel
.proto ?
2 : 0;
1465 policy_info
->priority
-= policy
->sel
.sport_mask ?
1 : 0;
1466 policy_info
->action
= XFRM_POLICY_ALLOW
;
1467 policy_info
->share
= XFRM_SHARE_ANY
;
1468 this->mutex
->unlock(this->mutex
);
1470 /* policies don't expire */
1471 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1472 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1473 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1474 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1475 policy_info
->lft
.soft_add_expires_seconds
= 0;
1476 policy_info
->lft
.hard_add_expires_seconds
= 0;
1477 policy_info
->lft
.soft_use_expires_seconds
= 0;
1478 policy_info
->lft
.hard_use_expires_seconds
= 0;
1480 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
1481 rthdr
->rta_type
= XFRMA_TMPL
;
1482 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1484 hdr
->nlmsg_len
+= rthdr
->rta_len
;
1485 if (hdr
->nlmsg_len
> sizeof(request
))
1490 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
1492 if (ipcomp
!= IPCOMP_NONE
)
1494 tmpl
->reqid
= reqid
;
1495 tmpl
->id
.proto
= IPPROTO_COMP
;
1496 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1498 tmpl
->optional
= direction
!= POLICY_OUT
;
1499 tmpl
->family
= src
->get_family(src
);
1501 host2xfrm(src
, &tmpl
->saddr
);
1502 host2xfrm(dst
, &tmpl
->id
.daddr
);
1504 /* add an additional xfrm_user_tmpl */
1505 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1506 hdr
->nlmsg_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
1507 if (hdr
->nlmsg_len
> sizeof(request
))
1515 tmpl
->reqid
= reqid
;
1516 tmpl
->id
.proto
= proto_ike2kernel(protocol
);
1517 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
1519 tmpl
->family
= src
->get_family(src
);
1521 host2xfrm(src
, &tmpl
->saddr
);
1522 host2xfrm(dst
, &tmpl
->id
.daddr
);
1524 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1526 DBG1(DBG_KNL
, "unable to add policy %R === %R %N", src_ts
, dst_ts
,
1527 policy_dir_names
, direction
);
1531 /* install a route, if:
1532 * - we are NOT updating a policy
1533 * - this is a forward policy (to just get one for each child)
1534 * - we are in tunnel mode
1535 * - we are not using IPv6 (does not work correctly yet!)
1536 * - routing is not disabled via strongswan.conf
1538 if (policy
->route
== NULL
&& direction
== POLICY_FWD
&&
1539 mode
!= MODE_TRANSPORT
&& src
->get_family(src
) != AF_INET6
&&
1540 this->install_routes
)
1542 route_entry_t
*route
= malloc_thing(route_entry_t
);
1544 if (charon
->kernel_interface
->get_address_by_ts(charon
->kernel_interface
,
1545 dst_ts
, &route
->src_ip
) == SUCCESS
)
1547 /* get the nexthop to src (src as we are in POLICY_FWD).*/
1548 route
->gateway
= charon
->kernel_interface
->get_nexthop(
1549 charon
->kernel_interface
, src
);
1550 route
->if_name
= charon
->kernel_interface
->get_interface(
1551 charon
->kernel_interface
, dst
);
1552 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
1553 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
1554 route
->prefixlen
= policy
->sel
.prefixlen_s
;
1558 switch (charon
->kernel_interface
->add_route(
1559 charon
->kernel_interface
, route
->dst_net
,
1560 route
->prefixlen
, route
->gateway
,
1561 route
->src_ip
, route
->if_name
))
1564 DBG1(DBG_KNL
, "unable to install source route for %H",
1568 /* route exists, do not uninstall */
1569 route_entry_destroy(route
);
1572 /* cache the installed route */
1573 policy
->route
= route
;
1579 route_entry_destroy(route
);
1591 * Implementation of kernel_interface_t.query_policy.
1593 static status_t
query_policy(private_kernel_netlink_ipsec_t
*this,
1594 traffic_selector_t
*src_ts
,
1595 traffic_selector_t
*dst_ts
,
1596 policy_dir_t direction
, u_int32_t
*use_time
)
1598 netlink_buf_t request
;
1599 struct nlmsghdr
*out
= NULL
, *hdr
;
1600 struct xfrm_userpolicy_id
*policy_id
;
1601 struct xfrm_userpolicy_info
*policy
= NULL
;
1604 memset(&request
, 0, sizeof(request
));
1606 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
1607 policy_dir_names
, direction
);
1609 hdr
= (struct nlmsghdr
*)request
;
1610 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1611 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
1612 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1614 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1615 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
1616 policy_id
->dir
= direction
;
1618 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1621 while (NLMSG_OK(hdr
, len
))
1623 switch (hdr
->nlmsg_type
)
1625 case XFRM_MSG_NEWPOLICY
:
1627 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1632 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1633 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
1634 strerror(-err
->error
), -err
->error
);
1638 hdr
= NLMSG_NEXT(hdr
, len
);
1649 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
1650 policy_dir_names
, direction
);
1654 *use_time
= (time_t)policy
->curlft
.use_time
;
1661 * Implementation of kernel_interface_t.del_policy.
1663 static status_t
del_policy(private_kernel_netlink_ipsec_t
*this,
1664 traffic_selector_t
*src_ts
,
1665 traffic_selector_t
*dst_ts
,
1666 policy_dir_t direction
, bool unrouted
)
1668 policy_entry_t
*current
, policy
, *to_delete
= NULL
;
1669 route_entry_t
*route
;
1670 netlink_buf_t request
;
1671 struct nlmsghdr
*hdr
;
1672 struct xfrm_userpolicy_id
*policy_id
;
1674 DBG2(DBG_KNL
, "deleting policy %R === %R %N", src_ts
, dst_ts
,
1675 policy_dir_names
, direction
);
1677 /* create a policy */
1678 memset(&policy
, 0, sizeof(policy_entry_t
));
1679 policy
.sel
= ts2selector(src_ts
, dst_ts
);
1680 policy
.direction
= direction
;
1682 /* find the policy */
1683 this->mutex
->lock(this->mutex
);
1684 current
= this->policies
->get(this->policies
, &policy
);
1687 to_delete
= current
;
1688 if (--to_delete
->refcount
> 0)
1690 /* is used by more SAs, keep in kernel */
1691 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
1692 this->mutex
->unlock(this->mutex
);
1695 /* remove if last reference */
1696 this->policies
->remove(this->policies
, to_delete
);
1698 this->mutex
->unlock(this->mutex
);
1701 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found", src_ts
,
1702 dst_ts
, policy_dir_names
, direction
);
1706 memset(&request
, 0, sizeof(request
));
1708 hdr
= (struct nlmsghdr
*)request
;
1709 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1710 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
1711 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
1713 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
1714 policy_id
->sel
= to_delete
->sel
;
1715 policy_id
->dir
= direction
;
1717 route
= to_delete
->route
;
1720 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1722 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N", src_ts
, dst_ts
,
1723 policy_dir_names
, direction
);
1729 if (charon
->kernel_interface
->del_route(charon
->kernel_interface
,
1730 route
->dst_net
, route
->prefixlen
, route
->gateway
,
1731 route
->src_ip
, route
->if_name
) != SUCCESS
)
1733 DBG1(DBG_KNL
, "error uninstalling route installed with "
1734 "policy %R === %R %N", src_ts
, dst_ts
,
1735 policy_dir_names
, direction
);
1737 route_entry_destroy(route
);
1743 * Implementation of kernel_interface_t.destroy.
1745 static void destroy(private_kernel_netlink_ipsec_t
*this)
1747 enumerator_t
*enumerator
;
1748 policy_entry_t
*policy
;
1750 this->job
->cancel(this->job
);
1751 close(this->socket_xfrm_events
);
1752 this->socket_xfrm
->destroy(this->socket_xfrm
);
1753 enumerator
= this->policies
->create_enumerator(this->policies
);
1754 while (enumerator
->enumerate(enumerator
, (void**)&policy
))
1758 enumerator
->destroy(enumerator
);
1759 this->policies
->destroy(this->policies
);
1760 this->mutex
->destroy(this->mutex
);
1765 * Add bypass policies for IKE on the sockets used by charon
1767 static bool add_bypass_policies()
1769 int fd
, family
, port
;
1770 enumerator_t
*sockets
;
1773 /* we open an AF_KEY socket to autoload the af_key module. Otherwise
1774 * setsockopt(IPSEC_POLICY) won't work. */
1775 fd
= socket(AF_KEY
, SOCK_RAW
, PF_KEY_V2
);
1778 DBG1(DBG_KNL
, "could not open AF_KEY socket");
1783 sockets
= charon
->socket
->create_enumerator(charon
->socket
);
1784 while (sockets
->enumerate(sockets
, &fd
, &family
, &port
))
1786 struct sadb_x_policy policy
;
1787 u_int sol
, ipsec_policy
;
1793 ipsec_policy
= IP_IPSEC_POLICY
;
1797 ipsec_policy
= IPV6_IPSEC_POLICY
;
1803 memset(&policy
, 0, sizeof(policy
));
1804 policy
.sadb_x_policy_len
= sizeof(policy
) / sizeof(u_int64_t
);
1805 policy
.sadb_x_policy_exttype
= SADB_X_EXT_POLICY
;
1806 policy
.sadb_x_policy_type
= IPSEC_POLICY_BYPASS
;
1808 policy
.sadb_x_policy_dir
= IPSEC_DIR_OUTBOUND
;
1809 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1811 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1816 policy
.sadb_x_policy_dir
= IPSEC_DIR_INBOUND
;
1817 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
1819 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
1825 sockets
->destroy(sockets
);
1830 * Described in header.
1832 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
1834 private_kernel_netlink_ipsec_t
*this = malloc_thing(private_kernel_netlink_ipsec_t
);
1835 struct sockaddr_nl addr
;
1837 /* public functions */
1838 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
;
1839 this->public.interface
.get_cpi
= (status_t(*)(kernel_ipsec_t
*,host_t
*,host_t
*,u_int32_t
,u_int16_t
*))get_cpi
;
1840 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
;
1841 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
;
1842 this->public.interface
.del_sa
= (status_t(*)(kernel_ipsec_t
*,host_t
*,u_int32_t
,protocol_id_t
,u_int16_t
))del_sa
;
1843 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
;
1844 this->public.interface
.query_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,u_int32_t
*))query_policy
;
1845 this->public.interface
.del_policy
= (status_t(*)(kernel_ipsec_t
*,traffic_selector_t
*,traffic_selector_t
*,policy_dir_t
,bool))del_policy
;
1846 this->public.interface
.destroy
= (void(*)(kernel_ipsec_t
*)) destroy
;
1848 /* private members */
1849 this->policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
1850 (hashtable_equals_t
)policy_equals
, 1);
1851 this->mutex
= mutex_create(MUTEX_DEFAULT
);
1852 this->install_routes
= lib
->settings
->get_bool(lib
->settings
,
1853 "charon.install_routes", TRUE
);
1855 /* add bypass policies on the sockets used by charon */
1856 if (!add_bypass_policies())
1858 charon
->kill(charon
, "unable to add bypass policies on sockets");
1861 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
1863 memset(&addr
, 0, sizeof(addr
));
1864 addr
.nl_family
= AF_NETLINK
;
1866 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
1867 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
1868 if (this->socket_xfrm_events
<= 0)
1870 charon
->kill(charon
, "unable to create XFRM event socket");
1872 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
1873 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
1874 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1876 charon
->kill(charon
, "unable to bind XFRM event socket");
1879 this->job
= callback_job_create((callback_job_cb_t
)receive_events
,
1881 charon
->processor
->queue_job(charon
->processor
, (job_t
*)this->job
);
1883 return &this->public;