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
,
1598 u_int64_t
*bytes
, u_int64_t
*packets
, u_int32_t
*time
)
1600 netlink_buf_t request
;
1601 struct nlmsghdr
*out
= NULL
, *hdr
;
1602 struct xfrm_usersa_id
*sa_id
;
1603 struct xfrm_usersa_info
*sa
= NULL
;
1604 status_t status
= FAILED
;
1607 memset(&request
, 0, sizeof(request
));
1609 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%08x)",
1610 ntohl(spi
), mark
.value
, mark
.mask
);
1612 hdr
= (struct nlmsghdr
*)request
;
1613 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1614 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1615 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1617 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1618 host2xfrm(dst
, &sa_id
->daddr
);
1620 sa_id
->proto
= protocol
;
1621 sa_id
->family
= dst
->get_family(dst
);
1623 if (!add_mark(hdr
, sizeof(request
), mark
))
1628 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1631 while (NLMSG_OK(hdr
, len
))
1633 switch (hdr
->nlmsg_type
)
1635 case XFRM_MSG_NEWSA
:
1637 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1642 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1646 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1647 "(mark %u/0x%08x) failed: %s (%d)",
1648 ntohl(spi
), mark
.value
, mark
.mask
,
1649 strerror(-err
->error
), -err
->error
);
1653 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1654 "failed: %s (%d)", ntohl(spi
),
1655 strerror(-err
->error
), -err
->error
);
1660 hdr
= NLMSG_NEXT(hdr
, len
);
1671 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1677 *bytes
= sa
->curlft
.bytes
;
1681 *packets
= sa
->curlft
.packets
;
1684 { /* curlft contains an "use" time, but that contains a timestamp
1685 * of the first use, not the last. Last use time must be queried
1686 * on the policy on Linux */
1696 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1697 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1698 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1700 netlink_buf_t request
;
1701 struct nlmsghdr
*hdr
;
1702 struct xfrm_usersa_id
*sa_id
;
1704 /* if IPComp was used, we first delete the additional IPComp SA */
1707 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1710 memset(&request
, 0, sizeof(request
));
1712 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%08x)",
1713 ntohl(spi
), mark
.value
, mark
.mask
);
1715 hdr
= (struct nlmsghdr
*)request
;
1716 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1717 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1718 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1720 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1721 host2xfrm(dst
, &sa_id
->daddr
);
1723 sa_id
->proto
= protocol
;
1724 sa_id
->family
= dst
->get_family(dst
);
1726 if (!add_mark(hdr
, sizeof(request
), mark
))
1731 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
1734 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%08x)",
1735 ntohl(spi
), mark
.value
, mark
.mask
);
1742 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1743 "(mark %u/0x%08x)", ntohl(spi
), mark
.value
, mark
.mask
);
1747 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1754 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1755 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1756 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1757 bool old_encap
, bool new_encap
, mark_t mark
)
1759 netlink_buf_t request
;
1760 struct nlmsghdr
*hdr
, *out
= NULL
;
1761 struct xfrm_usersa_id
*sa_id
;
1762 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1766 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1767 struct xfrm_replay_state
*replay
= NULL
;
1768 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1769 status_t status
= FAILED
;
1771 /* if IPComp is used, we first update the IPComp SA */
1774 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1775 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1778 memset(&request
, 0, sizeof(request
));
1780 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1782 /* query the existing SA first */
1783 hdr
= (struct nlmsghdr
*)request
;
1784 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1785 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1786 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1788 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1789 host2xfrm(dst
, &sa_id
->daddr
);
1791 sa_id
->proto
= protocol
;
1792 sa_id
->family
= dst
->get_family(dst
);
1794 if (!add_mark(hdr
, sizeof(request
), mark
))
1799 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1802 while (NLMSG_OK(hdr
, len
))
1804 switch (hdr
->nlmsg_type
)
1806 case XFRM_MSG_NEWSA
:
1808 out_sa
= NLMSG_DATA(hdr
);
1813 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1814 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1815 strerror(-err
->error
), -err
->error
);
1819 hdr
= NLMSG_NEXT(hdr
, len
);
1829 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1833 get_replay_state(this, spi
, protocol
, dst
, mark
, &replay_esn
, &replay
);
1835 /* delete the old SA (without affecting the IPComp SA) */
1836 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1838 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1843 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1844 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1845 /* copy over the SA from out to request */
1846 hdr
= (struct nlmsghdr
*)request
;
1847 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1848 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1849 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1850 sa
= NLMSG_DATA(hdr
);
1851 memcpy(sa
, NLMSG_DATA(out
), sizeof(struct xfrm_usersa_info
));
1852 sa
->family
= new_dst
->get_family(new_dst
);
1854 if (!src
->ip_equals(src
, new_src
))
1856 host2xfrm(new_src
, &sa
->saddr
);
1858 if (!dst
->ip_equals(dst
, new_dst
))
1860 host2xfrm(new_dst
, &sa
->id
.daddr
);
1863 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1864 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1865 while (RTA_OK(rta
, rtasize
))
1867 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1868 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1870 if (rta
->rta_type
== XFRMA_ENCAP
)
1871 { /* update encap tmpl */
1872 tmpl
= RTA_DATA(rta
);
1873 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1874 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1876 netlink_add_attribute(hdr
, rta
->rta_type
,
1877 chunk_create(RTA_DATA(rta
), RTA_PAYLOAD(rta
)),
1880 rta
= RTA_NEXT(rta
, rtasize
);
1883 if (tmpl
== NULL
&& new_encap
)
1884 { /* add tmpl if we are enabling it */
1885 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_ENCAP
, sizeof(*tmpl
));
1890 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1891 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1892 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1893 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1898 struct xfrm_replay_state_esn
*state
;
1900 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_ESN_VAL
,
1901 sizeof(*state
) + this->replay_bmp
);
1906 memcpy(state
, replay_esn
, sizeof(*state
) + this->replay_bmp
);
1910 struct xfrm_replay_state
*state
;
1912 state
= netlink_reserve(hdr
, sizeof(request
), XFRMA_REPLAY_VAL
,
1918 memcpy(state
, replay
, sizeof(*state
));
1922 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1923 "with SPI %.8x", ntohl(spi
));
1926 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1928 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1937 memwipe(request
, sizeof(request
));
1943 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1944 private_kernel_netlink_ipsec_t
*this)
1946 netlink_buf_t request
;
1947 struct nlmsghdr
*hdr
;
1948 struct xfrm_usersa_flush
*flush
;
1950 memset(&request
, 0, sizeof(request
));
1952 DBG2(DBG_KNL
, "flushing all SAD entries");
1954 hdr
= (struct nlmsghdr
*)request
;
1955 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1956 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
1957 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1959 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
1960 flush
->proto
= IPSEC_PROTO_ANY
;
1962 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1964 DBG1(DBG_KNL
, "unable to flush SAD entries");
1971 * Add or update a policy in the kernel.
1973 * Note: The mutex has to be locked when entering this function
1974 * and is unlocked here in any case.
1976 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
1977 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
1979 netlink_buf_t request
;
1980 policy_entry_t clone
;
1981 ipsec_sa_t
*ipsec
= mapping
->sa
;
1982 struct xfrm_userpolicy_info
*policy_info
;
1983 struct nlmsghdr
*hdr
;
1986 /* clone the policy so we are able to check it out again later */
1987 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
1989 memset(&request
, 0, sizeof(request
));
1990 hdr
= (struct nlmsghdr
*)request
;
1991 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1992 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
1993 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
1995 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
1996 policy_info
->sel
= policy
->sel
;
1997 policy_info
->dir
= policy
->direction
;
1999 /* calculate priority based on selector size, small size = high prio */
2000 policy_info
->priority
= mapping
->priority
;
2001 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2002 : XFRM_POLICY_BLOCK
;
2003 policy_info
->share
= XFRM_SHARE_ANY
;
2005 /* policies don't expire */
2006 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2007 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2008 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2009 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2010 policy_info
->lft
.soft_add_expires_seconds
= 0;
2011 policy_info
->lft
.hard_add_expires_seconds
= 0;
2012 policy_info
->lft
.soft_use_expires_seconds
= 0;
2013 policy_info
->lft
.hard_use_expires_seconds
= 0;
2015 if (mapping
->type
== POLICY_IPSEC
)
2017 struct xfrm_user_tmpl
*tmpl
;
2022 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2023 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2024 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2026 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2029 for (i
= 0; i
< countof(protos
); i
++)
2036 tmpl
= netlink_reserve(hdr
, sizeof(request
), XFRMA_TMPL
,
2037 count
* sizeof(*tmpl
));
2040 this->mutex
->unlock(this->mutex
);
2044 for (i
= 0; i
< countof(protos
); i
++)
2050 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2051 tmpl
->id
.proto
= protos
[i
].proto
;
2052 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2053 tmpl
->mode
= mode2kernel(proto_mode
);
2054 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2055 policy
->direction
!= POLICY_OUT
;
2056 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2058 if (proto_mode
== MODE_TUNNEL
)
2059 { /* only for tunnel mode */
2060 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2061 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2066 /* use transport mode for other SAs */
2067 proto_mode
= MODE_TRANSPORT
;
2071 if (!add_mark(hdr
, sizeof(request
), ipsec
->mark
))
2073 this->mutex
->unlock(this->mutex
);
2076 this->mutex
->unlock(this->mutex
);
2078 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2083 /* find the policy again */
2084 this->mutex
->lock(this->mutex
);
2085 policy
= this->policies
->get(this->policies
, &clone
);
2087 policy
->used_by
->find_first(policy
->used_by
,
2088 NULL
, (void**)&mapping
) != SUCCESS
)
2089 { /* policy or mapping is already gone, ignore */
2090 this->mutex
->unlock(this->mutex
);
2094 /* install a route, if:
2095 * - this is a forward policy (to just get one for each child)
2096 * - we are in tunnel/BEET mode or install a bypass policy
2097 * - routing is not disabled via strongswan.conf
2099 if (policy
->direction
== POLICY_FWD
&& this->install_routes
&&
2100 (mapping
->type
!= POLICY_IPSEC
|| ipsec
->cfg
.mode
!= MODE_TRANSPORT
))
2102 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2103 route_entry_t
*route
;
2107 .prefixlen
= policy
->sel
.prefixlen_s
,
2110 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2111 fwd
->dst_ts
, &route
->src_ip
, NULL
) == SUCCESS
)
2113 /* get the nexthop to src (src as we are in POLICY_FWD) */
2114 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2115 hydra
->kernel_interface
, ipsec
->src
,
2117 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2118 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2120 /* get the interface to install the route for. If we have a local
2121 * address, use it. Otherwise (for shunt policies) use the
2122 * routes source address. */
2124 if (iface
->is_anyaddr(iface
))
2126 iface
= route
->src_ip
;
2128 /* install route via outgoing interface */
2129 if (!hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
2130 iface
, &route
->if_name
))
2132 this->mutex
->unlock(this->mutex
);
2133 route_entry_destroy(route
);
2139 route_entry_t
*old
= policy
->route
;
2140 if (route_entry_equals(old
, route
))
2142 this->mutex
->unlock(this->mutex
);
2143 route_entry_destroy(route
);
2146 /* uninstall previously installed route */
2147 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2148 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2149 old
->src_ip
, old
->if_name
) != SUCCESS
)
2151 DBG1(DBG_KNL
, "error uninstalling route installed with "
2152 "policy %R === %R %N", fwd
->src_ts
,
2153 fwd
->dst_ts
, policy_dir_names
,
2156 route_entry_destroy(old
);
2157 policy
->route
= NULL
;
2160 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2161 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2162 switch (hydra
->kernel_interface
->add_route(
2163 hydra
->kernel_interface
, route
->dst_net
,
2164 route
->prefixlen
, route
->gateway
,
2165 route
->src_ip
, route
->if_name
))
2168 DBG1(DBG_KNL
, "unable to install source route for %H",
2172 /* route exists, do not uninstall */
2173 route_entry_destroy(route
);
2176 /* cache the installed route */
2177 policy
->route
= route
;
2186 this->mutex
->unlock(this->mutex
);
2190 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2191 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2192 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2193 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2194 mark_t mark
, policy_priority_t priority
)
2196 policy_entry_t
*policy
, *current
;
2197 policy_sa_t
*assigned_sa
, *current_sa
;
2198 enumerator_t
*enumerator
;
2199 bool found
= FALSE
, update
= TRUE
;
2201 /* create a policy */
2203 .sel
= ts2selector(src_ts
, dst_ts
),
2204 .mark
= mark
.value
& mark
.mask
,
2205 .direction
= direction
,
2208 /* find the policy, which matches EXACTLY */
2209 this->mutex
->lock(this->mutex
);
2210 current
= this->policies
->get(this->policies
, policy
);
2213 /* use existing policy */
2214 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%08x) "
2215 "already exists, increasing refcount",
2216 src_ts
, dst_ts
, policy_dir_names
, direction
,
2217 mark
.value
, mark
.mask
);
2218 policy_entry_destroy(this, policy
);
2223 { /* use the new one, if we have no such policy */
2224 policy
->used_by
= linked_list_create();
2225 this->policies
->put(this->policies
, policy
, policy
);
2228 /* cache the assigned IPsec SA */
2229 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2231 assigned_sa
->priority
= get_priority(policy
, priority
);
2233 if (this->policy_history
)
2234 { /* insert the SA according to its priority */
2235 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2236 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2238 if (current_sa
->priority
>= assigned_sa
->priority
)
2244 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2246 enumerator
->destroy(enumerator
);
2249 { /* simply insert it last and only update if it is not installed yet */
2250 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2255 { /* we don't update the policy if the priority is lower than that of
2256 * the currently installed one */
2257 this->mutex
->unlock(this->mutex
);
2261 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%08x)",
2262 found ?
"updating" : "adding", src_ts
, dst_ts
,
2263 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2265 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2267 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2268 found ?
"update" : "add", src_ts
, dst_ts
,
2269 policy_dir_names
, direction
);
2275 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2276 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2277 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2278 u_int32_t
*use_time
)
2280 netlink_buf_t request
;
2281 struct nlmsghdr
*out
= NULL
, *hdr
;
2282 struct xfrm_userpolicy_id
*policy_id
;
2283 struct xfrm_userpolicy_info
*policy
= NULL
;
2286 memset(&request
, 0, sizeof(request
));
2288 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%08x)",
2289 src_ts
, dst_ts
, policy_dir_names
, direction
,
2290 mark
.value
, mark
.mask
);
2292 hdr
= (struct nlmsghdr
*)request
;
2293 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2294 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2295 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2297 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2298 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2299 policy_id
->dir
= direction
;
2301 if (!add_mark(hdr
, sizeof(request
), mark
))
2306 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2309 while (NLMSG_OK(hdr
, len
))
2311 switch (hdr
->nlmsg_type
)
2313 case XFRM_MSG_NEWPOLICY
:
2315 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2320 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2321 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2322 strerror(-err
->error
), -err
->error
);
2326 hdr
= NLMSG_NEXT(hdr
, len
);
2337 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2338 policy_dir_names
, direction
);
2343 if (policy
->curlft
.use_time
)
2345 /* we need the monotonic time, but the kernel returns system time. */
2346 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2357 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2358 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2359 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2360 mark_t mark
, policy_priority_t prio
)
2362 policy_entry_t
*current
, policy
;
2363 enumerator_t
*enumerator
;
2364 policy_sa_t
*mapping
;
2365 netlink_buf_t request
;
2366 struct nlmsghdr
*hdr
;
2367 struct xfrm_userpolicy_id
*policy_id
;
2368 bool is_installed
= TRUE
;
2371 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x)",
2372 src_ts
, dst_ts
, policy_dir_names
, direction
,
2373 mark
.value
, mark
.mask
);
2375 /* create a policy */
2376 memset(&policy
, 0, sizeof(policy_entry_t
));
2377 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2378 policy
.mark
= mark
.value
& mark
.mask
;
2379 policy
.direction
= direction
;
2381 /* find the policy */
2382 this->mutex
->lock(this->mutex
);
2383 current
= this->policies
->get(this->policies
, &policy
);
2388 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x) "
2389 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2390 direction
, mark
.value
, mark
.mask
);
2394 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2395 src_ts
, dst_ts
, policy_dir_names
, direction
);
2397 this->mutex
->unlock(this->mutex
);
2401 if (this->policy_history
)
2402 { /* remove mapping to SA by reqid and priority */
2403 priority
= get_priority(current
, prio
);
2404 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2405 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2407 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2408 priority
== mapping
->priority
)
2410 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2411 policy_sa_destroy(mapping
, &direction
, this);
2414 is_installed
= FALSE
;
2416 enumerator
->destroy(enumerator
);
2419 { /* remove one of the SAs but don't update the policy */
2420 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2421 policy_sa_destroy(mapping
, &direction
, this);
2422 is_installed
= FALSE
;
2425 if (current
->used_by
->get_count(current
->used_by
) > 0)
2426 { /* policy is used by more SAs, keep in kernel */
2427 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2429 { /* no need to update as the policy was not installed for this SA */
2430 this->mutex
->unlock(this->mutex
);
2434 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%08x)",
2435 src_ts
, dst_ts
, policy_dir_names
, direction
,
2436 mark
.value
, mark
.mask
);
2438 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2439 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2441 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2442 src_ts
, dst_ts
, policy_dir_names
, direction
);
2448 memset(&request
, 0, sizeof(request
));
2450 hdr
= (struct nlmsghdr
*)request
;
2451 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2452 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2453 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2455 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2456 policy_id
->sel
= current
->sel
;
2457 policy_id
->dir
= direction
;
2459 if (!add_mark(hdr
, sizeof(request
), mark
))
2466 route_entry_t
*route
= current
->route
;
2467 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2468 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2469 route
->src_ip
, route
->if_name
) != SUCCESS
)
2471 DBG1(DBG_KNL
, "error uninstalling route installed with "
2472 "policy %R === %R %N", src_ts
, dst_ts
,
2473 policy_dir_names
, direction
);
2477 this->policies
->remove(this->policies
, current
);
2478 policy_entry_destroy(this, current
);
2479 this->mutex
->unlock(this->mutex
);
2481 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2485 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2486 "(mark %u/0x%08x)", src_ts
, dst_ts
, policy_dir_names
,
2487 direction
, mark
.value
, mark
.mask
);
2491 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2492 src_ts
, dst_ts
, policy_dir_names
, direction
);
2499 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2500 private_kernel_netlink_ipsec_t
*this)
2502 netlink_buf_t request
;
2503 struct nlmsghdr
*hdr
;
2505 memset(&request
, 0, sizeof(request
));
2507 DBG2(DBG_KNL
, "flushing all policies from SPD");
2509 hdr
= (struct nlmsghdr
*)request
;
2510 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2511 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2512 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2514 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2515 * to main or sub policies (default is main) */
2517 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2519 DBG1(DBG_KNL
, "unable to flush SPD entries");
2526 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2527 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2529 struct xfrm_userpolicy_info policy
;
2530 u_int sol
, ipsec_policy
;
2536 ipsec_policy
= IP_XFRM_POLICY
;
2540 ipsec_policy
= IPV6_XFRM_POLICY
;
2546 memset(&policy
, 0, sizeof(policy
));
2547 policy
.action
= XFRM_POLICY_ALLOW
;
2548 policy
.sel
.family
= family
;
2550 policy
.dir
= XFRM_POLICY_OUT
;
2551 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2553 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2557 policy
.dir
= XFRM_POLICY_IN
;
2558 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2560 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2567 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
2568 private_kernel_netlink_ipsec_t
*this, int fd
, int family
, u_int16_t port
)
2570 int type
= UDP_ENCAP_ESPINUDP
;
2572 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
2574 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
2580 METHOD(kernel_ipsec_t
, destroy
, void,
2581 private_kernel_netlink_ipsec_t
*this)
2583 enumerator_t
*enumerator
;
2584 policy_entry_t
*policy
;
2586 if (this->socket_xfrm_events
> 0)
2588 close(this->socket_xfrm_events
);
2590 DESTROY_IF(this->socket_xfrm
);
2591 enumerator
= this->policies
->create_enumerator(this->policies
);
2592 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2594 policy_entry_destroy(this, policy
);
2596 enumerator
->destroy(enumerator
);
2597 this->policies
->destroy(this->policies
);
2598 this->sas
->destroy(this->sas
);
2599 this->mutex
->destroy(this->mutex
);
2604 * Described in header.
2606 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2608 private_kernel_netlink_ipsec_t
*this;
2609 bool register_for_events
= TRUE
;
2615 .get_features
= _get_features
,
2616 .get_spi
= _get_spi
,
2617 .get_cpi
= _get_cpi
,
2619 .update_sa
= _update_sa
,
2620 .query_sa
= _query_sa
,
2622 .flush_sas
= _flush_sas
,
2623 .add_policy
= _add_policy
,
2624 .query_policy
= _query_policy
,
2625 .del_policy
= _del_policy
,
2626 .flush_policies
= _flush_policies
,
2627 .bypass_socket
= _bypass_socket
,
2628 .enable_udp_decap
= _enable_udp_decap
,
2629 .destroy
= _destroy
,
2632 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2633 (hashtable_equals_t
)policy_equals
, 32),
2634 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2635 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2636 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2637 .policy_history
= TRUE
,
2638 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2639 "%s.install_routes", TRUE
, hydra
->daemon
),
2640 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2641 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2644 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2645 (sizeof(u_int32_t
) * 8);
2647 if (streq(hydra
->daemon
, "starter"))
2648 { /* starter has no threads, so we do not register for kernel events */
2649 register_for_events
= FALSE
;
2652 /* disable lifetimes for allocated SPIs in kernel */
2653 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2656 ignore_result(write(fd
, "165", 3));
2660 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2661 if (!this->socket_xfrm
)
2667 if (register_for_events
)
2669 struct sockaddr_nl addr
;
2671 memset(&addr
, 0, sizeof(addr
));
2672 addr
.nl_family
= AF_NETLINK
;
2674 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2675 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2676 if (this->socket_xfrm_events
<= 0)
2678 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2682 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2683 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2684 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2686 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2690 lib
->processor
->queue_job(lib
->processor
,
2691 (job_t
*)callback_job_create_with_prio(
2692 (callback_job_cb_t
)receive_events
, this, NULL
,
2693 (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
2696 return &this->public;