2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2009 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
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <linux/ipsec.h>
25 #include <linux/netlink.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/xfrm.h>
28 #include <linux/udp.h>
35 #include "kernel_netlink_ipsec.h"
36 #include "kernel_netlink_shared.h"
39 #include <utils/debug.h>
40 #include <threading/thread.h>
41 #include <threading/mutex.h>
42 #include <collections/hashtable.h>
43 #include <collections/linked_list.h>
44 #include <processing/jobs/callback_job.h>
46 /** Required for Linux 2.6.26 kernel and later */
47 #ifndef XFRM_STATE_AF_UNSPEC
48 #define XFRM_STATE_AF_UNSPEC 32
51 /** From linux/in.h */
52 #ifndef IP_XFRM_POLICY
53 #define IP_XFRM_POLICY 17
56 /** Missing on uclibc */
57 #ifndef IPV6_XFRM_POLICY
58 #define IPV6_XFRM_POLICY 34
59 #endif /*IPV6_XFRM_POLICY*/
61 /* from linux/udp.h */
66 #ifndef UDP_ENCAP_ESPINUDP
67 #define UDP_ENCAP_ESPINUDP 2
70 /* this is not defined on some platforms */
72 #define SOL_UDP IPPROTO_UDP
75 /** Default priority of installed policies */
78 /** Default replay window size, if not set using charon.replay_window */
79 #define DEFAULT_REPLAY_WINDOW 32
82 * Map the limit for bytes and packets to XFRM_INF by default
84 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
87 * Create ORable bitfield of XFRM NL groups
89 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
92 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
93 * 'usual' netlink data x like 'struct xfrm_usersa_info'
95 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
96 NLMSG_ALIGN(sizeof(x))))
98 * Returns the total size of attached rta data
99 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
101 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
103 typedef struct kernel_algorithm_t kernel_algorithm_t
;
106 * Mapping of IKEv2 kernel identifier to linux crypto API names
108 struct kernel_algorithm_t
{
110 * Identifier specified in IKEv2
115 * Name of the algorithm in linux crypto API
120 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
124 "XFRM_MSG_NEWPOLICY",
125 "XFRM_MSG_DELPOLICY",
126 "XFRM_MSG_GETPOLICY",
130 "XFRM_MSG_UPDPOLICY",
132 "XFRM_MSG_POLEXPIRE",
134 "XFRM_MSG_FLUSHPOLICY",
139 "XFRM_MSG_NEWSADINFO",
140 "XFRM_MSG_GETSADINFO",
141 "XFRM_MSG_NEWSPDINFO",
142 "XFRM_MSG_GETSPDINFO",
146 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_REPLAY_ESN_VAL
,
158 "XFRMA_REPLAY_THRESH",
159 "XFRMA_ETIMER_THRESH",
167 "XFRMA_ALG_AUTH_TRUNC",
170 "XFRMA_REPLAY_ESN_VAL",
174 * Algorithms for encryption
176 static kernel_algorithm_t encryption_algs
[] = {
177 /* {ENCR_DES_IV64, "***" }, */
179 {ENCR_3DES
, "des3_ede" },
180 /* {ENCR_RC5, "***" }, */
181 /* {ENCR_IDEA, "***" }, */
182 {ENCR_CAST
, "cast128" },
183 {ENCR_BLOWFISH
, "blowfish" },
184 /* {ENCR_3IDEA, "***" }, */
185 /* {ENCR_DES_IV32, "***" }, */
186 {ENCR_NULL
, "cipher_null" },
187 {ENCR_AES_CBC
, "aes" },
188 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
189 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
190 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
191 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
192 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
193 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
194 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
195 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
196 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
197 /* {ENCR_CAMELLIA_CTR, "***" }, */
198 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
199 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
200 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
201 {ENCR_SERPENT_CBC
, "serpent" },
202 {ENCR_TWOFISH_CBC
, "twofish" },
206 * Algorithms for integrity protection
208 static kernel_algorithm_t integrity_algs
[] = {
209 {AUTH_HMAC_MD5_96
, "md5" },
210 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
211 {AUTH_HMAC_SHA1_96
, "sha1" },
212 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
213 {AUTH_HMAC_SHA2_256_96
, "sha256" },
214 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
215 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
216 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
217 /* {AUTH_DES_MAC, "***" }, */
218 /* {AUTH_KPDK_MD5, "***" }, */
219 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
223 * Algorithms for IPComp
225 static kernel_algorithm_t compression_algs
[] = {
226 /* {IPCOMP_OUI, "***" }, */
227 {IPCOMP_DEFLATE
, "deflate" },
228 {IPCOMP_LZS
, "lzs" },
229 {IPCOMP_LZJH
, "lzjh" },
233 * Look up a kernel algorithm name and its key size
235 static char* lookup_algorithm(transform_type_t type
, int ikev2
)
237 kernel_algorithm_t
*list
;
243 case ENCRYPTION_ALGORITHM
:
244 list
= encryption_algs
;
245 count
= countof(encryption_algs
);
247 case INTEGRITY_ALGORITHM
:
248 list
= integrity_algs
;
249 count
= countof(integrity_algs
);
251 case COMPRESSION_ALGORITHM
:
252 list
= compression_algs
;
253 count
= countof(compression_algs
);
258 for (i
= 0; i
< count
; i
++)
260 if (list
[i
].ikev2
== ikev2
)
265 if (hydra
->kernel_interface
->lookup_algorithm(hydra
->kernel_interface
,
266 ikev2
, type
, NULL
, &name
))
273 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
276 * Private variables and functions of kernel_netlink class.
278 struct private_kernel_netlink_ipsec_t
{
280 * Public part of the kernel_netlink_t object
282 kernel_netlink_ipsec_t
public;
285 * Mutex to lock access to installed policies
290 * Hash table of installed policies (policy_entry_t)
292 hashtable_t
*policies
;
295 * Hash table of IPsec SAs using policies (ipsec_sa_t)
300 * Netlink xfrm socket (IPsec)
302 netlink_socket_t
*socket_xfrm
;
305 * Netlink xfrm socket to receive acquire and expire events
307 int socket_xfrm_events
;
310 * Whether to install routes along policies
315 * Whether to track the history of a policy
320 * Size of the replay window, in packets (= bits)
322 u_int32_t replay_window
;
325 * Size of the replay window bitmap, in number of __u32 blocks
327 u_int32_t replay_bmp
;
330 typedef struct route_entry_t route_entry_t
;
333 * Installed routing entry
335 struct route_entry_t
{
336 /** Name of the interface the route is bound to */
339 /** Source ip of the route */
342 /** Gateway for this route */
345 /** Destination net */
348 /** Destination net prefixlen */
353 * Destroy a route_entry_t object
355 static void route_entry_destroy(route_entry_t
*this)
358 this->src_ip
->destroy(this->src_ip
);
359 DESTROY_IF(this->gateway
);
360 chunk_free(&this->dst_net
);
365 * Compare two route_entry_t objects
367 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
369 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
370 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
371 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
372 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
375 typedef struct ipsec_sa_t ipsec_sa_t
;
378 * IPsec SA assigned to a policy.
381 /** Source address of this SA */
384 /** Destination address of this SA */
390 /** Description of this SA */
393 /** Reference count for this SA */
398 * Hash function for ipsec_sa_t objects
400 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
402 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
403 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
404 chunk_hash_inc(chunk_from_thing(sa
->mark
),
405 chunk_hash(chunk_from_thing(sa
->cfg
)))));
409 * Equality function for ipsec_sa_t objects
411 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
413 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
414 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
415 memeq(&sa
->mark
, &other_sa
->mark
, sizeof(mark_t
)) &&
416 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
420 * Allocate or reference an IPsec SA object
422 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
423 host_t
*src
, host_t
*dst
, mark_t mark
,
426 ipsec_sa_t
*sa
, *found
;
433 found
= this->sas
->get(this->sas
, sa
);
436 sa
->src
= src
->clone(src
);
437 sa
->dst
= dst
->clone(dst
);
438 this->sas
->put(this->sas
, sa
, sa
);
445 ref_get(&sa
->refcount
);
450 * Release and destroy an IPsec SA object
452 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
455 if (ref_put(&sa
->refcount
))
457 this->sas
->remove(this->sas
, sa
);
464 typedef struct policy_sa_t policy_sa_t
;
465 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
468 * Mapping between a policy and an IPsec SA.
471 /** Priority assigned to the policy when installed with this SA */
474 /** Type of the policy */
482 * For forward policies we also cache the traffic selectors in order to install
485 struct policy_sa_fwd_t
{
486 /** Generic interface */
489 /** Source traffic selector of this policy */
490 traffic_selector_t
*src_ts
;
492 /** Destination traffic selector of this policy */
493 traffic_selector_t
*dst_ts
;
497 * Create a policy_sa(_fwd)_t object
499 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
500 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
501 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
506 if (dir
== POLICY_FWD
)
508 policy_sa_fwd_t
*fwd
;
510 .src_ts
= src_ts
->clone(src_ts
),
511 .dst_ts
= dst_ts
->clone(dst_ts
),
513 policy
= &fwd
->generic
;
517 INIT(policy
, .priority
= 0);
520 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
525 * Destroy a policy_sa(_fwd)_t object
527 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
528 private_kernel_netlink_ipsec_t
*this)
530 if (*dir
== POLICY_FWD
)
532 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
533 fwd
->src_ts
->destroy(fwd
->src_ts
);
534 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
536 ipsec_sa_destroy(this, policy
->sa
);
540 typedef struct policy_entry_t policy_entry_t
;
543 * Installed kernel policy.
545 struct policy_entry_t
{
547 /** Direction of this policy: in, out, forward */
550 /** Parameters of installed policy */
551 struct xfrm_selector sel
;
556 /** Associated route installed for this policy */
557 route_entry_t
*route
;
559 /** List of SAs this policy is used by, ordered by priority */
560 linked_list_t
*used_by
;
564 * Destroy a policy_entry_t object
566 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
567 policy_entry_t
*policy
)
571 route_entry_destroy(policy
->route
);
575 policy
->used_by
->invoke_function(policy
->used_by
,
576 (linked_list_invoke_t
)policy_sa_destroy
,
577 &policy
->direction
, this);
578 policy
->used_by
->destroy(policy
->used_by
);
584 * Hash function for policy_entry_t objects
586 static u_int
policy_hash(policy_entry_t
*key
)
588 chunk_t chunk
= chunk_from_thing(key
->sel
);
589 return chunk_hash_inc(chunk
, chunk_hash(chunk_from_thing(key
->mark
)));
593 * Equality function for policy_entry_t objects
595 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
597 return memeq(&key
->sel
, &other_key
->sel
, sizeof(struct xfrm_selector
)) &&
598 key
->mark
== other_key
->mark
&&
599 key
->direction
== other_key
->direction
;
603 * Calculate the priority of a policy
605 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
606 policy_priority_t prio
)
608 u_int32_t priority
= PRIO_BASE
;
611 case POLICY_PRIORITY_FALLBACK
:
614 case POLICY_PRIORITY_ROUTED
:
617 case POLICY_PRIORITY_DEFAULT
:
620 /* calculate priority based on selector size, small size = high prio */
621 priority
-= policy
->sel
.prefixlen_s
;
622 priority
-= policy
->sel
.prefixlen_d
;
623 priority
<<= 2; /* make some room for the two flags */
624 priority
+= policy
->sel
.sport_mask
|| policy
->sel
.dport_mask ?
0 : 2;
625 priority
+= policy
->sel
.proto ?
0 : 1;
630 * Convert the general ipsec mode to the one defined in xfrm.h
632 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
637 return XFRM_MODE_TRANSPORT
;
639 return XFRM_MODE_TUNNEL
;
641 return XFRM_MODE_BEET
;
648 * Convert a host_t to a struct xfrm_address
650 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
652 chunk_t chunk
= host
->get_address(host
);
653 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
657 * Convert a struct xfrm_address to a host_t
659 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
666 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
669 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
674 return host_create_from_chunk(family
, chunk
, ntohs(port
));
678 * Convert a traffic selector address range to subnet and its mask.
680 static void ts2subnet(traffic_selector_t
* ts
,
681 xfrm_address_t
*net
, u_int8_t
*mask
)
686 ts
->to_subnet(ts
, &net_host
, mask
);
687 net_chunk
= net_host
->get_address(net_host
);
688 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
689 net_host
->destroy(net_host
);
693 * Convert a traffic selector port range to port/portmask
695 static void ts2ports(traffic_selector_t
* ts
,
696 u_int16_t
*port
, u_int16_t
*mask
)
698 /* Linux does not seem to accept complex portmasks. Only
699 * any or a specific port is allowed. We set to any, if we have
700 * a port range, or to a specific, if we have one port only.
704 from
= ts
->get_from_port(ts
);
705 to
= ts
->get_to_port(ts
);
720 * Convert a pair of traffic_selectors to an xfrm_selector
722 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
723 traffic_selector_t
*dst
)
725 struct xfrm_selector sel
;
727 memset(&sel
, 0, sizeof(sel
));
728 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
729 /* src or dest proto may be "any" (0), use more restrictive one */
730 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
731 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
732 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
733 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
734 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
742 * Convert an xfrm_selector to a src|dst traffic_selector
744 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
753 addr
= (u_char
*)&sel
->saddr
;
754 prefixlen
= sel
->prefixlen_s
;
757 port
= htons(sel
->sport
);
762 addr
= (u_char
*)&sel
->daddr
;
763 prefixlen
= sel
->prefixlen_d
;
766 port
= htons(sel
->dport
);
770 /* The Linux 2.6 kernel does not set the selector's family field,
771 * so as a kludge we additionally test the prefix length.
773 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
775 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
777 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
779 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
784 return traffic_selector_create_from_subnet(host
, prefixlen
,
785 sel
->proto
, port
, port ?
: 65535);
791 * Process a XFRM_MSG_ACQUIRE from kernel
793 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
794 struct nlmsghdr
*hdr
)
796 struct xfrm_user_acquire
*acquire
;
799 traffic_selector_t
*src_ts
, *dst_ts
;
803 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
804 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
805 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
807 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
809 while (RTA_OK(rta
, rtasize
))
811 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
813 if (rta
->rta_type
== XFRMA_TMPL
)
815 struct xfrm_user_tmpl
* tmpl
;
816 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
818 proto
= tmpl
->id
.proto
;
820 rta
= RTA_NEXT(rta
, rtasize
);
829 /* acquire for AH/ESP only, not for IPCOMP */
832 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
833 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
835 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
840 * Process a XFRM_MSG_EXPIRE from kernel
842 static void process_expire(private_kernel_netlink_ipsec_t
*this,
843 struct nlmsghdr
*hdr
)
845 struct xfrm_user_expire
*expire
;
846 u_int32_t spi
, reqid
;
849 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
850 protocol
= expire
->state
.id
.proto
;
851 spi
= expire
->state
.id
.spi
;
852 reqid
= expire
->state
.reqid
;
854 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
856 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
858 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and "
859 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
863 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
864 spi
, expire
->hard
!= 0);
868 * Process a XFRM_MSG_MIGRATE from kernel
870 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
871 struct nlmsghdr
*hdr
)
873 struct xfrm_userpolicy_id
*policy_id
;
876 traffic_selector_t
*src_ts
, *dst_ts
;
877 host_t
*local
= NULL
, *remote
= NULL
;
878 host_t
*old_src
= NULL
, *old_dst
= NULL
;
879 host_t
*new_src
= NULL
, *new_dst
= NULL
;
883 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
884 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
885 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
887 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
889 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
890 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
891 dir
= (policy_dir_t
)policy_id
->dir
;
893 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
895 while (RTA_OK(rta
, rtasize
))
897 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
898 if (rta
->rta_type
== XFRMA_KMADDRESS
)
900 struct xfrm_user_kmaddress
*kmaddress
;
902 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
903 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
904 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
905 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
907 else if (rta
->rta_type
== XFRMA_MIGRATE
)
909 struct xfrm_user_migrate
*migrate
;
911 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
912 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
913 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
914 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
915 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
916 reqid
= migrate
->reqid
;
917 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
918 old_src
, old_dst
, new_src
, new_dst
, reqid
);
924 rta
= RTA_NEXT(rta
, rtasize
);
927 if (src_ts
&& dst_ts
&& local
&& remote
)
929 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
930 src_ts
, dst_ts
, dir
, local
, remote
);
942 * Process a XFRM_MSG_MAPPING from kernel
944 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
945 struct nlmsghdr
*hdr
)
947 struct xfrm_user_mapping
*mapping
;
948 u_int32_t spi
, reqid
;
950 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
951 spi
= mapping
->id
.spi
;
952 reqid
= mapping
->reqid
;
954 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
956 if (mapping
->id
.proto
== IPPROTO_ESP
)
959 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
963 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
970 * Receives events from kernel
972 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
975 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
976 struct sockaddr_nl addr
;
977 socklen_t addr_len
= sizeof(addr
);
981 oldstate
= thread_cancelability(TRUE
);
982 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
983 (struct sockaddr
*)&addr
, &addr_len
);
984 thread_cancelability(oldstate
);
991 /* interrupted, try again */
992 return JOB_REQUEUE_DIRECT
;
994 /* no data ready, select again */
995 return JOB_REQUEUE_DIRECT
;
997 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
999 return JOB_REQUEUE_FAIR
;
1003 if (addr
.nl_pid
!= 0)
1004 { /* not from kernel. not interested, try another one */
1005 return JOB_REQUEUE_DIRECT
;
1008 while (NLMSG_OK(hdr
, len
))
1010 switch (hdr
->nlmsg_type
)
1012 case XFRM_MSG_ACQUIRE
:
1013 process_acquire(this, hdr
);
1015 case XFRM_MSG_EXPIRE
:
1016 process_expire(this, hdr
);
1018 case XFRM_MSG_MIGRATE
:
1019 process_migrate(this, hdr
);
1021 case XFRM_MSG_MAPPING
:
1022 process_mapping(this, hdr
);
1025 DBG1(DBG_KNL
, "received unknown event from xfrm event "
1026 "socket: %d", hdr
->nlmsg_type
);
1029 hdr
= NLMSG_NEXT(hdr
, len
);
1031 return JOB_REQUEUE_DIRECT
;
1034 METHOD(kernel_ipsec_t
, get_features
, kernel_feature_t
,
1035 private_kernel_netlink_ipsec_t
*this)
1037 return KERNEL_ESP_V3_TFC
;
1041 * Get an SPI for a specific protocol from the kernel.
1043 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1044 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1045 u_int32_t reqid
, u_int32_t
*spi
)
1047 netlink_buf_t request
;
1048 struct nlmsghdr
*hdr
, *out
;
1049 struct xfrm_userspi_info
*userspi
;
1050 u_int32_t received_spi
= 0;
1053 memset(&request
, 0, sizeof(request
));
1055 hdr
= (struct nlmsghdr
*)request
;
1056 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1057 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1058 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1060 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1061 host2xfrm(src
, &userspi
->info
.saddr
);
1062 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1063 userspi
->info
.id
.proto
= proto
;
1064 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1065 userspi
->info
.reqid
= reqid
;
1066 userspi
->info
.family
= src
->get_family(src
);
1070 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1073 while (NLMSG_OK(hdr
, len
))
1075 switch (hdr
->nlmsg_type
)
1077 case XFRM_MSG_NEWSA
:
1079 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1080 received_spi
= usersa
->id
.spi
;
1085 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1086 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1087 strerror(-err
->error
), -err
->error
);
1091 hdr
= NLMSG_NEXT(hdr
, len
);
1101 if (received_spi
== 0)
1106 *spi
= received_spi
;
1110 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1111 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1112 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1114 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
1116 if (get_spi_internal(this, src
, dst
, protocol
,
1117 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1119 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
1123 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
1127 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1128 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1129 u_int32_t reqid
, u_int16_t
*cpi
)
1131 u_int32_t received_spi
= 0;
1133 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
1135 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1136 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
1138 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
1142 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
1144 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
1149 * Add a XFRM mark to message if required
1151 static bool add_mark(struct nlmsghdr
*hdr
, int buflen
, mark_t mark
)
1155 struct xfrm_mark
*xmrk
;
1157 xmrk
= netlink_reserve(hdr
, buflen
, XFRMA_MARK
, sizeof(*xmrk
));
1162 xmrk
->v
= mark
.value
;
1163 xmrk
->m
= mark
.mask
;
1168 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1169 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1170 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
1171 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1172 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
1173 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1174 traffic_selector_t
* src_ts
, traffic_selector_t
* dst_ts
)
1176 netlink_buf_t request
;
1178 struct nlmsghdr
*hdr
;
1179 struct xfrm_usersa_info
*sa
;
1180 u_int16_t icv_size
= 64;
1181 status_t status
= FAILED
;
1183 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1184 * we are in the recursive call below */
1185 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
1187 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1188 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, mark
,
1189 tfc
, &lft
, ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
,
1190 chunk_empty
, mode
, ipcomp
, 0, FALSE
, FALSE
, inbound
, NULL
, NULL
);
1191 ipcomp
= IPCOMP_NONE
;
1192 /* use transport mode ESP SA, IPComp uses tunnel mode */
1193 mode
= MODE_TRANSPORT
;
1196 memset(&request
, 0, sizeof(request
));
1198 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u} (mark "
1199 "%u/0x%08x)", ntohl(spi
), reqid
, mark
.value
, mark
.mask
);
1201 hdr
= (struct nlmsghdr
*)request
;
1202 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1203 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1204 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1206 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1207 host2xfrm(src
, &sa
->saddr
);
1208 host2xfrm(dst
, &sa
->id
.daddr
);
1210 sa
->id
.proto
= protocol
;
1211 sa
->family
= src
->get_family(src
);
1212 sa
->mode
= mode2kernel(mode
);
1216 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1219 case MODE_TRANSPORT
:
1220 if(src_ts
&& dst_ts
)
1222 sa
->sel
= ts2selector(src_ts
, dst_ts
);
1230 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.rekey
);
1231 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.life
);
1232 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(lifetime
->packets
.rekey
);
1233 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(lifetime
->packets
.life
);
1234 /* we use lifetimes since added, not since used */
1235 sa
->lft
.soft_add_expires_seconds
= lifetime
->time
.rekey
;
1236 sa
->lft
.hard_add_expires_seconds
= lifetime
->time
.life
;
1237 sa
->lft
.soft_use_expires_seconds
= 0;
1238 sa
->lft
.hard_use_expires_seconds
= 0;
1242 case ENCR_UNDEFINED
:
1245 case ENCR_AES_CCM_ICV16
:
1246 case ENCR_AES_GCM_ICV16
:
1247 case ENCR_NULL_AUTH_AES_GMAC
:
1248 case ENCR_CAMELLIA_CCM_ICV16
:
1251 case ENCR_AES_CCM_ICV12
:
1252 case ENCR_AES_GCM_ICV12
:
1253 case ENCR_CAMELLIA_CCM_ICV12
:
1256 case ENCR_AES_CCM_ICV8
:
1257 case ENCR_AES_GCM_ICV8
:
1258 case ENCR_CAMELLIA_CCM_ICV8
:
1260 struct xfrm_algo_aead
*algo
;
1262 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1263 if (alg_name
== NULL
)
1265 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1266 encryption_algorithm_names
, enc_alg
);
1269 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1270 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1272 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AEAD
,
1273 sizeof(*algo
) + enc_key
.len
);
1278 algo
->alg_key_len
= enc_key
.len
* 8;
1279 algo
->alg_icv_len
= icv_size
;
1280 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1281 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1282 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1287 struct xfrm_algo
*algo
;
1289 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1290 if (alg_name
== NULL
)
1292 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1293 encryption_algorithm_names
, enc_alg
);
1296 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1297 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1299 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_CRYPT
,
1300 sizeof(*algo
) + enc_key
.len
);
1305 algo
->alg_key_len
= enc_key
.len
* 8;
1306 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1307 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1308 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1312 if (int_alg
!= AUTH_UNDEFINED
)
1314 u_int trunc_len
= 0;
1316 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, int_alg
);
1317 if (alg_name
== NULL
)
1319 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1320 integrity_algorithm_names
, int_alg
);
1323 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1324 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1328 case AUTH_HMAC_MD5_128
:
1329 case AUTH_HMAC_SHA2_256_128
:
1332 case AUTH_HMAC_SHA1_160
:
1341 struct xfrm_algo_auth
* algo
;
1343 /* the kernel uses SHA256 with 96 bit truncation by default,
1344 * use specified truncation size supported by newer kernels.
1345 * also use this for untruncated MD5 and SHA1. */
1346 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH_TRUNC
,
1347 sizeof(*algo
) + int_key
.len
);
1352 algo
->alg_key_len
= int_key
.len
* 8;
1353 algo
->alg_trunc_len
= trunc_len
;
1354 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1355 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1356 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1360 struct xfrm_algo
* algo
;
1362 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_AUTH
,
1363 sizeof(*algo
) + int_key
.len
);
1368 algo
->alg_key_len
= int_key
.len
* 8;
1369 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1370 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1371 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1375 if (ipcomp
!= IPCOMP_NONE
)
1377 struct xfrm_algo
* algo
;
1379 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
1380 if (alg_name
== NULL
)
1382 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1383 ipcomp_transform_names
, ipcomp
);
1386 DBG2(DBG_KNL
, " using compression algorithm %N",
1387 ipcomp_transform_names
, ipcomp
);
1389 algo
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ALG_COMP
,
1395 algo
->alg_key_len
= 0;
1396 strncpy(algo
->alg_name
, alg_name
, sizeof(algo
->alg_name
));
1397 algo
->alg_name
[sizeof(algo
->alg_name
) - 1] = '\0';
1402 struct xfrm_encap_tmpl
*tmpl
;
1404 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1409 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1410 tmpl
->encap_sport
= htons(src
->get_port(src
));
1411 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1412 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1413 /* encap_oa could probably be derived from the
1414 * traffic selectors [rfc4306, p39]. In the netlink kernel
1415 * implementation pluto does the same as we do here but it uses
1416 * encap_oa in the pfkey implementation.
1417 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1419 * -> does that mean that NAT-T encap doesn't work in transport mode?
1420 * No. The reason the kernel ignores NAT-OA is that it recomputes
1421 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1422 * checks it marks them "checksum ok" so OA isn't needed. */
1425 if (!add_mark(hdr
, sizeof(request
), mark
))
1434 tfcpad
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TFCPAD
,
1443 if (protocol
!= IPPROTO_COMP
)
1445 if (esn
|| this->replay_window
> DEFAULT_REPLAY_WINDOW
)
1447 /* for ESN or larger replay windows we need the new
1448 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1449 struct xfrm_replay_state_esn
*replay
;
1451 replay
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1452 sizeof(*replay
) + (this->replay_window
+ 7) / 8);
1457 /* bmp_len contains number uf __u32's */
1458 replay
->bmp_len
= this->replay_bmp
;
1459 replay
->replay_window
= this->replay_window
;
1460 DBG2(DBG_KNL
, " using replay window of %u packets",
1461 this->replay_window
);
1465 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1466 sa
->flags
|= XFRM_STATE_ESN
;
1471 DBG2(DBG_KNL
, " using replay window of %u packets",
1472 this->replay_window
);
1473 sa
->replay_window
= this->replay_window
;
1477 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1481 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x "
1482 "(mark %u/0x%08x)", ntohl(spi
), mark
.value
, mark
.mask
);
1486 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1494 memwipe(request
, sizeof(request
));
1499 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1501 * Allocates into one the replay state structure we get from the kernel.
1503 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1504 u_int32_t spi
, u_int8_t protocol
,
1505 host_t
*dst
, mark_t mark
,
1506 struct xfrm_replay_state_esn
**replay_esn
,
1507 struct xfrm_replay_state
**replay
)
1509 netlink_buf_t request
;
1510 struct nlmsghdr
*hdr
, *out
= NULL
;
1511 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1516 memset(&request
, 0, sizeof(request
));
1518 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1521 hdr
= (struct nlmsghdr
*)request
;
1522 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1523 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1524 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1526 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1527 aevent_id
->flags
= XFRM_AE_RVAL
;
1529 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1530 aevent_id
->sa_id
.spi
= spi
;
1531 aevent_id
->sa_id
.proto
= protocol
;
1532 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1534 if (!add_mark(hdr
, sizeof(request
), mark
))
1539 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1542 while (NLMSG_OK(hdr
, len
))
1544 switch (hdr
->nlmsg_type
)
1546 case XFRM_MSG_NEWAE
:
1548 out_aevent
= NLMSG_DATA(hdr
);
1553 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1554 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1555 "failed: %s (%d)", strerror(-err
->error
),
1560 hdr
= NLMSG_NEXT(hdr
, len
);
1571 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1572 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1573 while (RTA_OK(rta
, rtasize
))
1575 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1576 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1578 *replay
= malloc(RTA_PAYLOAD(rta
));
1579 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1582 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1583 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
) + this->replay_bmp
)
1585 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1586 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1589 rta
= RTA_NEXT(rta
, rtasize
);
1595 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1596 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1597 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1599 netlink_buf_t request
;
1600 struct nlmsghdr
*out
= NULL
, *hdr
;
1601 struct xfrm_usersa_id
*sa_id
;
1602 struct xfrm_usersa_info
*sa
= NULL
;
1603 status_t status
= FAILED
;
1606 memset(&request
, 0, sizeof(request
));
1608 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%08x)",
1609 ntohl(spi
), mark
.value
, mark
.mask
);
1611 hdr
= (struct nlmsghdr
*)request
;
1612 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1613 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1614 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1616 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1617 host2xfrm(dst
, &sa_id
->daddr
);
1619 sa_id
->proto
= protocol
;
1620 sa_id
->family
= dst
->get_family(dst
);
1622 if (!add_mark(hdr
, sizeof(request
), mark
))
1627 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1630 while (NLMSG_OK(hdr
, len
))
1632 switch (hdr
->nlmsg_type
)
1634 case XFRM_MSG_NEWSA
:
1636 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1641 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1645 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1646 "(mark %u/0x%08x) failed: %s (%d)",
1647 ntohl(spi
), mark
.value
, mark
.mask
,
1648 strerror(-err
->error
), -err
->error
);
1652 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1653 "failed: %s (%d)", ntohl(spi
),
1654 strerror(-err
->error
), -err
->error
);
1659 hdr
= NLMSG_NEXT(hdr
, len
);
1670 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1674 *bytes
= sa
->curlft
.bytes
;
1682 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1683 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1684 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1686 netlink_buf_t request
;
1687 struct nlmsghdr
*hdr
;
1688 struct xfrm_usersa_id
*sa_id
;
1690 /* if IPComp was used, we first delete the additional IPComp SA */
1693 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1696 memset(&request
, 0, sizeof(request
));
1698 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%08x)",
1699 ntohl(spi
), mark
.value
, mark
.mask
);
1701 hdr
= (struct nlmsghdr
*)request
;
1702 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1703 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1704 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1706 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1707 host2xfrm(dst
, &sa_id
->daddr
);
1709 sa_id
->proto
= protocol
;
1710 sa_id
->family
= dst
->get_family(dst
);
1712 if (!add_mark(hdr
, sizeof(request
), mark
))
1717 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
1720 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%08x)",
1721 ntohl(spi
), mark
.value
, mark
.mask
);
1728 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1729 "(mark %u/0x%08x)", ntohl(spi
), mark
.value
, mark
.mask
);
1733 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1740 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1741 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1742 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1743 bool old_encap
, bool new_encap
, mark_t mark
)
1745 netlink_buf_t request
;
1746 struct nlmsghdr
*hdr
, *out
= NULL
;
1747 struct xfrm_usersa_id
*sa_id
;
1748 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1752 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1753 struct xfrm_replay_state
*replay
= NULL
;
1754 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1755 status_t status
= FAILED
;
1757 /* if IPComp is used, we first update the IPComp SA */
1760 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1761 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1764 memset(&request
, 0, sizeof(request
));
1766 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1768 /* query the existing SA first */
1769 hdr
= (struct nlmsghdr
*)request
;
1770 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1771 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1772 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1774 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1775 host2xfrm(dst
, &sa_id
->daddr
);
1777 sa_id
->proto
= protocol
;
1778 sa_id
->family
= dst
->get_family(dst
);
1780 if (!add_mark(hdr
, sizeof(request
), mark
))
1785 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1788 while (NLMSG_OK(hdr
, len
))
1790 switch (hdr
->nlmsg_type
)
1792 case XFRM_MSG_NEWSA
:
1794 out_sa
= NLMSG_DATA(hdr
);
1799 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1800 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1801 strerror(-err
->error
), -err
->error
);
1805 hdr
= NLMSG_NEXT(hdr
, len
);
1815 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1819 get_replay_state(this, spi
, protocol
, dst
, mark
, &replay_esn
, &replay
);
1821 /* delete the old SA (without affecting the IPComp SA) */
1822 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1824 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1829 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1830 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1831 /* copy over the SA from out to request */
1832 hdr
= (struct nlmsghdr
*)request
;
1833 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1834 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1835 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1836 sa
= NLMSG_DATA(hdr
);
1837 memcpy(sa
, NLMSG_DATA(out
), sizeof(struct xfrm_usersa_info
));
1838 sa
->family
= new_dst
->get_family(new_dst
);
1840 if (!src
->ip_equals(src
, new_src
))
1842 host2xfrm(new_src
, &sa
->saddr
);
1844 if (!dst
->ip_equals(dst
, new_dst
))
1846 host2xfrm(new_dst
, &sa
->id
.daddr
);
1849 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1850 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1851 while (RTA_OK(rta
, rtasize
))
1853 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1854 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1856 if (rta
->rta_type
== XFRMA_ENCAP
)
1857 { /* update encap tmpl */
1858 tmpl
= RTA_DATA(rta
);
1859 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1860 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1862 netlink_add_attribute(hdr
, rta
->rta_type
,
1863 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
1866 rta
= RTA_NEXT(rta
, rtasize
);
1869 if (tmpl
== NULL
&& new_encap
)
1870 { /* add tmpl if we are enabling it */
1871 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1876 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1877 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1878 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1879 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1884 struct xfrm_replay_state_esn
*state
;
1886 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1887 sizeof(*state
) + this->replay_bmp
);
1892 memcpy(state
, replay_esn
, sizeof(*state
) + this->replay_bmp
);
1896 struct xfrm_replay_state
*state
;
1898 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
1904 memcpy(state
, replay
, sizeof(*state
));
1908 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1909 "with SPI %.8x", ntohl(spi
));
1912 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1914 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1923 memwipe(request
, sizeof(request
));
1929 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1930 private_kernel_netlink_ipsec_t
*this)
1932 netlink_buf_t request
;
1933 struct nlmsghdr
*hdr
;
1934 struct xfrm_usersa_flush
*flush
;
1936 memset(&request
, 0, sizeof(request
));
1938 DBG2(DBG_KNL
, "flushing all SAD entries");
1940 hdr
= (struct nlmsghdr
*)request
;
1941 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1942 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
1943 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1945 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
1946 flush
->proto
= IPSEC_PROTO_ANY
;
1948 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1950 DBG1(DBG_KNL
, "unable to flush SAD entries");
1957 * Add or update a policy in the kernel.
1959 * Note: The mutex has to be locked when entering this function
1960 * and is unlocked here in any case.
1962 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
1963 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
1965 netlink_buf_t request
;
1966 policy_entry_t clone
;
1967 ipsec_sa_t
*ipsec
= mapping
->sa
;
1968 struct xfrm_userpolicy_info
*policy_info
;
1969 struct nlmsghdr
*hdr
;
1972 /* clone the policy so we are able to check it out again later */
1973 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
1975 memset(&request
, 0, sizeof(request
));
1976 hdr
= (struct nlmsghdr
*)request
;
1977 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1978 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1979 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1981 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1982 policy_info
->sel
= policy
->sel
;
1983 policy_info
->dir
= policy
->direction
;
1985 /* calculate priority based on selector size, small size = high prio */
1986 policy_info
->priority
= mapping
->priority
;
1987 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
1988 : XFRM_POLICY_BLOCK
;
1989 policy_info
->share
= XFRM_SHARE_ANY
;
1991 /* policies don't expire */
1992 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
1993 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
1994 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
1995 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
1996 policy_info
->lft
.soft_add_expires_seconds
= 0;
1997 policy_info
->lft
.hard_add_expires_seconds
= 0;
1998 policy_info
->lft
.soft_use_expires_seconds
= 0;
1999 policy_info
->lft
.hard_use_expires_seconds
= 0;
2001 if (mapping
->type
== POLICY_IPSEC
)
2003 struct xfrm_user_tmpl
*tmpl
;
2008 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2009 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2010 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2012 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2015 for (i
= 0; i
< countof(protos
); i
++)
2022 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
2023 count
* sizeof(*tmpl
));
2026 this->mutex
->unlock(this->mutex
);
2030 for (i
= 0; i
< countof(protos
); i
++)
2036 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2037 tmpl
->id
.proto
= protos
[i
].proto
;
2038 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2039 tmpl
->mode
= mode2kernel(proto_mode
);
2040 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2041 policy
->direction
!= POLICY_OUT
;
2042 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2044 if (proto_mode
== MODE_TUNNEL
)
2045 { /* only for tunnel mode */
2046 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2047 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2052 /* use transport mode for other SAs */
2053 proto_mode
= MODE_TRANSPORT
;
2057 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
2059 this->mutex
->unlock(this->mutex
);
2062 this->mutex
->unlock(this->mutex
);
2064 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2069 /* find the policy again */
2070 this->mutex
->lock(this->mutex
);
2071 policy
= this->policies
->get(this->policies
, &clone
);
2073 policy
->used_by
->find_first(policy
->used_by
,
2074 NULL
, (void**)&mapping
) != SUCCESS
)
2075 { /* policy or mapping is already gone, ignore */
2076 this->mutex
->unlock(this->mutex
);
2080 /* install a route, if:
2081 * - this is a forward policy (to just get one for each child)
2082 * - we are in tunnel/BEET mode or install a bypass policy
2083 * - routing is not disabled via strongswan.conf
2085 if (policy
->direction
== POLICY_FWD
&& this->install_routes
&&
2086 (mapping
->type
!= POLICY_IPSEC
|| ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2088 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2089 route_entry_t
*route
;
2093 .prefixlen
= policy
->sel
.prefixlen_s
,
2096 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2097 fwd
->dst_ts
, &route
->src_ip
) == SUCCESS
)
2099 /* get the nexthop to src (src as we are in POLICY_FWD) */
2100 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2101 hydra
->kernel_interface
, ipsec
->src
,
2103 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2104 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2106 /* get the interface to install the route for. If we have a local
2107 * address, use it. Otherwise (for shunt policies) use the
2108 * routes source address. */
2110 if (iface
->is_anyaddr(iface
))
2112 iface
= route
->src_ip
;
2114 /* install route via outgoing interface */
2115 if (!hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
2116 iface
, &route
->if_name
))
2118 this->mutex
->unlock(this->mutex
);
2119 route_entry_destroy(route
);
2125 route_entry_t
*old
= policy
->route
;
2126 if (route_entry_equals(old
, route
))
2128 this->mutex
->unlock(this->mutex
);
2129 route_entry_destroy(route
);
2132 /* uninstall previously installed route */
2133 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2134 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2135 old
->src_ip
, old
->if_name
) != SUCCESS
)
2137 DBG1(DBG_KNL
, "error uninstalling route installed with "
2138 "policy %R === %R %N", fwd
->src_ts
,
2139 fwd
->dst_ts
, policy_dir_names
,
2142 route_entry_destroy(old
);
2143 policy
->route
= NULL
;
2146 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2147 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2148 switch (hydra
->kernel_interface
->add_route(
2149 hydra
->kernel_interface
, route
->dst_net
,
2150 route
->prefixlen
, route
->gateway
,
2151 route
->src_ip
, route
->if_name
))
2154 DBG1(DBG_KNL
, "unable to install source route for %H",
2158 /* route exists, do not uninstall */
2159 route_entry_destroy(route
);
2162 /* cache the installed route */
2163 policy
->route
= route
;
2172 this->mutex
->unlock(this->mutex
);
2176 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2177 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2178 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2179 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2180 mark_t mark
, policy_priority_t priority
)
2182 policy_entry_t
*policy
, *current
;
2183 policy_sa_t
*assigned_sa
, *current_sa
;
2184 enumerator_t
*enumerator
;
2185 bool found
= FALSE
, update
= TRUE
;
2187 /* create a policy */
2189 .sel
= ts2selector(src_ts
, dst_ts
),
2190 .mark
= mark
.value
& mark
.mask
,
2191 .direction
= direction
,
2194 /* find the policy, which matches EXACTLY */
2195 this->mutex
->lock(this->mutex
);
2196 current
= this->policies
->get(this->policies
, policy
);
2199 /* use existing policy */
2200 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%08x) "
2201 "already exists, increasing refcount",
2202 src_ts
, dst_ts
, policy_dir_names
, direction
,
2203 mark
.value
, mark
.mask
);
2204 policy_entry_destroy(this, policy
);
2209 { /* use the new one, if we have no such policy */
2210 policy
->used_by
= linked_list_create();
2211 this->policies
->put(this->policies
, policy
, policy
);
2214 /* cache the assigned IPsec SA */
2215 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2217 assigned_sa
->priority
= get_priority(policy
, priority
);
2219 if (this->policy_history
)
2220 { /* insert the SA according to its priority */
2221 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2222 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2224 if (current_sa
->priority
>= assigned_sa
->priority
)
2230 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2232 enumerator
->destroy(enumerator
);
2235 { /* simply insert it last and only update if it is not installed yet */
2236 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2241 { /* we don't update the policy if the priority is lower than that of
2242 * the currently installed one */
2243 this->mutex
->unlock(this->mutex
);
2247 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%08x)",
2248 found ?
"updating" : "adding", src_ts
, dst_ts
,
2249 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2251 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2253 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2254 found ?
"update" : "add", src_ts
, dst_ts
,
2255 policy_dir_names
, direction
);
2261 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2262 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2263 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2264 u_int32_t
*use_time
)
2266 netlink_buf_t request
;
2267 struct nlmsghdr
*out
= NULL
, *hdr
;
2268 struct xfrm_userpolicy_id
*policy_id
;
2269 struct xfrm_userpolicy_info
*policy
= NULL
;
2272 memset(&request
, 0, sizeof(request
));
2274 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%08x)",
2275 src_ts
, dst_ts
, policy_dir_names
, direction
,
2276 mark
.value
, mark
.mask
);
2278 hdr
= (struct nlmsghdr
*)request
;
2279 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2280 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2281 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2283 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2284 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2285 policy_id
->dir
= direction
;
2287 if (!add_mark(hdr
, sizeof(request
), mark
))
2292 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2295 while (NLMSG_OK(hdr
, len
))
2297 switch (hdr
->nlmsg_type
)
2299 case XFRM_MSG_NEWPOLICY
:
2301 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2306 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2307 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2308 strerror(-err
->error
), -err
->error
);
2312 hdr
= NLMSG_NEXT(hdr
, len
);
2323 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2324 policy_dir_names
, direction
);
2329 if (policy
->curlft
.use_time
)
2331 /* we need the monotonic time, but the kernel returns system time. */
2332 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2343 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2344 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2345 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2346 mark_t mark
, policy_priority_t prio
)
2348 policy_entry_t
*current
, policy
;
2349 enumerator_t
*enumerator
;
2350 policy_sa_t
*mapping
;
2351 netlink_buf_t request
;
2352 struct nlmsghdr
*hdr
;
2353 struct xfrm_userpolicy_id
*policy_id
;
2354 bool is_installed
= TRUE
;
2357 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x)",
2358 src_ts
, dst_ts
, policy_dir_names
, direction
,
2359 mark
.value
, mark
.mask
);
2361 /* create a policy */
2362 memset(&policy
, 0, sizeof(policy_entry_t
));
2363 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2364 policy
.mark
= mark
.value
& mark
.mask
;
2365 policy
.direction
= direction
;
2367 /* find the policy */
2368 this->mutex
->lock(this->mutex
);
2369 current
= this->policies
->get(this->policies
, &policy
);
2374 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x) "
2375 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2376 direction
, mark
.value
, mark
.mask
);
2380 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2381 src_ts
, dst_ts
, policy_dir_names
, direction
);
2383 this->mutex
->unlock(this->mutex
);
2387 if (this->policy_history
)
2388 { /* remove mapping to SA by reqid and priority */
2389 priority
= get_priority(current
, prio
);
2390 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2391 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2393 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2394 priority
== mapping
->priority
)
2396 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2397 policy_sa_destroy(mapping
, &direction
, this);
2400 is_installed
= FALSE
;
2402 enumerator
->destroy(enumerator
);
2405 { /* remove one of the SAs but don't update the policy */
2406 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2407 policy_sa_destroy(mapping
, &direction
, this);
2408 is_installed
= FALSE
;
2411 if (current
->used_by
->get_count(current
->used_by
) > 0)
2412 { /* policy is used by more SAs, keep in kernel */
2413 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2415 { /* no need to update as the policy was not installed for this SA */
2416 this->mutex
->unlock(this->mutex
);
2420 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%08x)",
2421 src_ts
, dst_ts
, policy_dir_names
, direction
,
2422 mark
.value
, mark
.mask
);
2424 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2425 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2427 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2428 src_ts
, dst_ts
, policy_dir_names
, direction
);
2434 memset(&request
, 0, sizeof(request
));
2436 hdr
= (struct nlmsghdr
*)request
;
2437 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2438 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2439 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2441 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2442 policy_id
->sel
= current
->sel
;
2443 policy_id
->dir
= direction
;
2445 if (!add_mark(hdr
, sizeof(request
), mark
))
2452 route_entry_t
*route
= current
->route
;
2453 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2454 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2455 route
->src_ip
, route
->if_name
) != SUCCESS
)
2457 DBG1(DBG_KNL
, "error uninstalling route installed with "
2458 "policy %R === %R %N", src_ts
, dst_ts
,
2459 policy_dir_names
, direction
);
2463 this->policies
->remove(this->policies
, current
);
2464 policy_entry_destroy(this, current
);
2465 this->mutex
->unlock(this->mutex
);
2467 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2471 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2472 "(mark %u/0x%08x)", src_ts
, dst_ts
, policy_dir_names
,
2473 direction
, mark
.value
, mark
.mask
);
2477 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2478 src_ts
, dst_ts
, policy_dir_names
, direction
);
2485 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2486 private_kernel_netlink_ipsec_t
*this)
2488 netlink_buf_t request
;
2489 struct nlmsghdr
*hdr
;
2491 memset(&request
, 0, sizeof(request
));
2493 DBG2(DBG_KNL
, "flushing all policies from SPD");
2495 hdr
= (struct nlmsghdr
*)request
;
2496 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2497 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2498 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2500 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2501 * to main or sub policies (default is main) */
2503 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2505 DBG1(DBG_KNL
, "unable to flush SPD entries");
2512 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2513 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2515 struct xfrm_userpolicy_info policy
;
2516 u_int sol
, ipsec_policy
;
2522 ipsec_policy
= IP_XFRM_POLICY
;
2526 ipsec_policy
= IPV6_XFRM_POLICY
;
2532 memset(&policy
, 0, sizeof(policy
));
2533 policy
.action
= XFRM_POLICY_ALLOW
;
2534 policy
.sel
.family
= family
;
2536 policy
.dir
= XFRM_POLICY_OUT
;
2537 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2539 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2543 policy
.dir
= XFRM_POLICY_IN
;
2544 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2546 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2553 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
2554 private_kernel_netlink_ipsec_t
*this, int fd
, int family
, u_int16_t port
)
2556 int type
= UDP_ENCAP_ESPINUDP
;
2558 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
2560 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
2566 METHOD(kernel_ipsec_t
, destroy
, void,
2567 private_kernel_netlink_ipsec_t
*this)
2569 enumerator_t
*enumerator
;
2570 policy_entry_t
*policy
;
2572 if (this->socket_xfrm_events
> 0)
2574 close(this->socket_xfrm_events
);
2576 DESTROY_IF(this->socket_xfrm
);
2577 enumerator
= this->policies
->create_enumerator(this->policies
);
2578 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2580 policy_entry_destroy(this, policy
);
2582 enumerator
->destroy(enumerator
);
2583 this->policies
->destroy(this->policies
);
2584 this->sas
->destroy(this->sas
);
2585 this->mutex
->destroy(this->mutex
);
2590 * Described in header.
2592 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2594 private_kernel_netlink_ipsec_t
*this;
2595 bool register_for_events
= TRUE
;
2601 .get_features
= _get_features
,
2602 .get_spi
= _get_spi
,
2603 .get_cpi
= _get_cpi
,
2605 .update_sa
= _update_sa
,
2606 .query_sa
= _query_sa
,
2608 .flush_sas
= _flush_sas
,
2609 .add_policy
= _add_policy
,
2610 .query_policy
= _query_policy
,
2611 .del_policy
= _del_policy
,
2612 .flush_policies
= _flush_policies
,
2613 .bypass_socket
= _bypass_socket
,
2614 .enable_udp_decap
= _enable_udp_decap
,
2615 .destroy
= _destroy
,
2618 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2619 (hashtable_equals_t
)policy_equals
, 32),
2620 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2621 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2622 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2623 .policy_history
= TRUE
,
2624 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2625 "%s.install_routes", TRUE
, hydra
->daemon
),
2626 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2627 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2630 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2631 (sizeof(u_int32_t
) * 8);
2633 if (streq(hydra
->daemon
, "pluto"))
2634 { /* no routes for pluto, they are installed via updown script */
2635 this->install_routes
= FALSE
;
2636 /* no policy history for pluto */
2637 this->policy_history
= FALSE
;
2639 else if (streq(hydra
->daemon
, "starter"))
2640 { /* starter has no threads, so we do not register for kernel events */
2641 register_for_events
= FALSE
;
2644 /* disable lifetimes for allocated SPIs in kernel */
2645 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2648 ignore_result(write(fd
, "165", 3));
2652 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2653 if (!this->socket_xfrm
)
2659 if (register_for_events
)
2661 struct sockaddr_nl addr
;
2663 memset(&addr
, 0, sizeof(addr
));
2664 addr
.nl_family
= AF_NETLINK
;
2666 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2667 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2668 if (this->socket_xfrm_events
<= 0)
2670 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2674 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2675 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2676 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2678 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2682 lib
->processor
->queue_job(lib
->processor
,
2683 (job_t
*)callback_job_create_with_prio(
2684 (callback_job_cb_t
)receive_events
, this, NULL
,
2685 (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
2688 return &this->public;