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"
40 #include <threading/thread.h>
41 #include <threading/mutex.h>
42 #include <utils/hashtable.h>
43 #include <utils/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 /** Default priority of installed policies */
64 /** Default replay window size, if not set using charon.replay_window */
65 #define DEFAULT_REPLAY_WINDOW 32
68 * Map the limit for bytes and packets to XFRM_INF by default
70 #define XFRM_LIMIT(x) ((x) == 0 ? XFRM_INF : (x))
73 * Create ORable bitfield of XFRM NL groups
75 #define XFRMNLGRP(x) (1<<(XFRMNLGRP_##x-1))
78 * Returns a pointer to the first rtattr following the nlmsghdr *nlh and the
79 * 'usual' netlink data x like 'struct xfrm_usersa_info'
81 #define XFRM_RTA(nlh, x) ((struct rtattr*)(NLMSG_DATA(nlh) + \
82 NLMSG_ALIGN(sizeof(x))))
84 * Returns a pointer to the next rtattr following rta.
85 * !!! Do not use this to parse messages. Use RTA_NEXT and RTA_OK instead !!!
87 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + \
88 RTA_ALIGN((rta)->rta_len)))
90 * Returns the total size of attached rta data
91 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
93 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
95 typedef struct kernel_algorithm_t kernel_algorithm_t
;
98 * Mapping of IKEv2 kernel identifier to linux crypto API names
100 struct kernel_algorithm_t
{
102 * Identifier specified in IKEv2
107 * Name of the algorithm in linux crypto API
112 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
116 "XFRM_MSG_NEWPOLICY",
117 "XFRM_MSG_DELPOLICY",
118 "XFRM_MSG_GETPOLICY",
122 "XFRM_MSG_UPDPOLICY",
124 "XFRM_MSG_POLEXPIRE",
126 "XFRM_MSG_FLUSHPOLICY",
131 "XFRM_MSG_NEWSADINFO",
132 "XFRM_MSG_GETSADINFO",
133 "XFRM_MSG_NEWSPDINFO",
134 "XFRM_MSG_GETSPDINFO",
138 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_REPLAY_ESN_VAL
,
150 "XFRMA_REPLAY_THRESH",
151 "XFRMA_ETIMER_THRESH",
159 "XFRMA_ALG_AUTH_TRUNC",
162 "XFRMA_REPLAY_ESN_VAL",
165 #define END_OF_LIST -1
168 * Algorithms for encryption
170 static kernel_algorithm_t encryption_algs
[] = {
171 /* {ENCR_DES_IV64, "***" }, */
173 {ENCR_3DES
, "des3_ede" },
174 /* {ENCR_RC5, "***" }, */
175 /* {ENCR_IDEA, "***" }, */
176 {ENCR_CAST
, "cast128" },
177 {ENCR_BLOWFISH
, "blowfish" },
178 /* {ENCR_3IDEA, "***" }, */
179 /* {ENCR_DES_IV32, "***" }, */
180 {ENCR_NULL
, "cipher_null" },
181 {ENCR_AES_CBC
, "aes" },
182 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
183 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
184 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
185 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
186 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
187 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
188 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
189 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
190 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
191 /* {ENCR_CAMELLIA_CTR, "***" }, */
192 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
193 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
194 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
195 {ENCR_SERPENT_CBC
, "serpent" },
196 {ENCR_TWOFISH_CBC
, "twofish" },
201 * Algorithms for integrity protection
203 static kernel_algorithm_t integrity_algs
[] = {
204 {AUTH_HMAC_MD5_96
, "md5" },
205 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
206 {AUTH_HMAC_SHA1_96
, "sha1" },
207 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
208 {AUTH_HMAC_SHA2_256_96
, "sha256" },
209 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
210 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
211 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
212 /* {AUTH_DES_MAC, "***" }, */
213 /* {AUTH_KPDK_MD5, "***" }, */
214 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
219 * Algorithms for IPComp
221 static kernel_algorithm_t compression_algs
[] = {
222 /* {IPCOMP_OUI, "***" }, */
223 {IPCOMP_DEFLATE
, "deflate" },
224 {IPCOMP_LZS
, "lzs" },
225 {IPCOMP_LZJH
, "lzjh" },
230 * Look up a kernel algorithm name and its key size
232 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
234 while (list
->ikev2
!= END_OF_LIST
)
236 if (list
->ikev2
== ikev2
)
245 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
248 * Private variables and functions of kernel_netlink class.
250 struct private_kernel_netlink_ipsec_t
{
252 * Public part of the kernel_netlink_t object
254 kernel_netlink_ipsec_t
public;
257 * Mutex to lock access to installed policies
262 * Hash table of installed policies (policy_entry_t)
264 hashtable_t
*policies
;
267 * Hash table of IPsec SAs using policies (ipsec_sa_t)
272 * Job receiving netlink events
277 * Netlink xfrm socket (IPsec)
279 netlink_socket_t
*socket_xfrm
;
282 * Netlink xfrm socket to receive acquire and expire events
284 int socket_xfrm_events
;
287 * Whether to install routes along policies
292 * Whether to track the history of a policy
297 * Size of the replay window, in packets
299 u_int32_t replay_window
;
302 * Size of the replay window bitmap, in bytes
304 u_int32_t replay_bmp
;
307 typedef struct route_entry_t route_entry_t
;
310 * Installed routing entry
312 struct route_entry_t
{
313 /** Name of the interface the route is bound to */
316 /** Source ip of the route */
319 /** Gateway for this route */
322 /** Destination net */
325 /** Destination net prefixlen */
330 * Destroy a route_entry_t object
332 static void route_entry_destroy(route_entry_t
*this)
335 this->src_ip
->destroy(this->src_ip
);
336 DESTROY_IF(this->gateway
);
337 chunk_free(&this->dst_net
);
342 * Compare two route_entry_t objects
344 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
346 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
347 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
348 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
349 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
352 typedef struct ipsec_sa_t ipsec_sa_t
;
355 * IPsec SA assigned to a policy.
358 /** Source address of this SA */
361 /** Destination address of this SA */
367 /** Description of this SA */
370 /** Reference count for this SA */
375 * Hash function for ipsec_sa_t objects
377 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
379 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
380 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
381 chunk_hash_inc(chunk_from_thing(sa
->mark
),
382 chunk_hash(chunk_from_thing(sa
->cfg
)))));
386 * Equality function for ipsec_sa_t objects
388 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
390 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
391 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
392 memeq(&sa
->mark
, &other_sa
->mark
, sizeof(mark_t
)) &&
393 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
397 * Allocate or reference an IPsec SA object
399 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
400 host_t
*src
, host_t
*dst
, mark_t mark
,
403 ipsec_sa_t
*sa
, *found
;
410 found
= this->sas
->get(this->sas
, sa
);
413 sa
->src
= src
->clone(src
);
414 sa
->dst
= dst
->clone(dst
);
415 this->sas
->put(this->sas
, sa
, sa
);
422 ref_get(&sa
->refcount
);
427 * Release and destroy an IPsec SA object
429 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
432 if (ref_put(&sa
->refcount
))
434 this->sas
->remove(this->sas
, sa
);
441 typedef struct policy_sa_t policy_sa_t
;
442 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
445 * Mapping between a policy and an IPsec SA.
448 /** Priority assigned to the policy when installed with this SA */
451 /** Type of the policy */
459 * For forward policies we also cache the traffic selectors in order to install
462 struct policy_sa_fwd_t
{
463 /** Generic interface */
466 /** Source traffic selector of this policy */
467 traffic_selector_t
*src_ts
;
469 /** Destination traffic selector of this policy */
470 traffic_selector_t
*dst_ts
;
474 * Create a policy_sa(_fwd)_t object
476 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
477 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
478 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
483 if (dir
== POLICY_FWD
)
485 policy_sa_fwd_t
*fwd
;
487 .src_ts
= src_ts
->clone(src_ts
),
488 .dst_ts
= dst_ts
->clone(dst_ts
),
490 policy
= &fwd
->generic
;
494 INIT(policy
, .priority
= 0);
497 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
502 * Destroy a policy_sa(_fwd)_t object
504 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
505 private_kernel_netlink_ipsec_t
*this)
507 if (*dir
== POLICY_FWD
)
509 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
510 fwd
->src_ts
->destroy(fwd
->src_ts
);
511 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
513 ipsec_sa_destroy(this, policy
->sa
);
517 typedef struct policy_entry_t policy_entry_t
;
520 * Installed kernel policy.
522 struct policy_entry_t
{
524 /** Direction of this policy: in, out, forward */
527 /** Parameters of installed policy */
528 struct xfrm_selector sel
;
533 /** Associated route installed for this policy */
534 route_entry_t
*route
;
536 /** List of SAs this policy is used by, ordered by priority */
537 linked_list_t
*used_by
;
541 * Destroy a policy_entry_t object
543 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
544 policy_entry_t
*policy
)
548 route_entry_destroy(policy
->route
);
552 policy
->used_by
->invoke_function(policy
->used_by
,
553 (linked_list_invoke_t
)policy_sa_destroy
,
554 &policy
->direction
, this);
555 policy
->used_by
->destroy(policy
->used_by
);
561 * Hash function for policy_entry_t objects
563 static u_int
policy_hash(policy_entry_t
*key
)
565 chunk_t chunk
= chunk_create((void*)&key
->sel
,
566 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
));
567 return chunk_hash(chunk
);
571 * Equality function for policy_entry_t objects
573 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
575 return memeq(&key
->sel
, &other_key
->sel
,
576 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
)) &&
577 key
->direction
== other_key
->direction
;
581 * Calculate the priority of a policy
583 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
584 policy_priority_t prio
)
586 u_int32_t priority
= PRIO_BASE
;
589 case POLICY_PRIORITY_FALLBACK
:
592 case POLICY_PRIORITY_ROUTED
:
595 case POLICY_PRIORITY_DEFAULT
:
598 /* calculate priority based on selector size, small size = high prio */
599 priority
-= policy
->sel
.prefixlen_s
;
600 priority
-= policy
->sel
.prefixlen_d
;
601 priority
<<= 2; /* make some room for the two flags */
602 priority
+= policy
->sel
.sport_mask
|| policy
->sel
.dport_mask ?
0 : 2;
603 priority
+= policy
->sel
.proto ?
0 : 1;
608 * Convert the general ipsec mode to the one defined in xfrm.h
610 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
615 return XFRM_MODE_TRANSPORT
;
617 return XFRM_MODE_TUNNEL
;
619 return XFRM_MODE_BEET
;
626 * Convert a host_t to a struct xfrm_address
628 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
630 chunk_t chunk
= host
->get_address(host
);
631 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
635 * Convert a struct xfrm_address to a host_t
637 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
644 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
647 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
652 return host_create_from_chunk(family
, chunk
, ntohs(port
));
656 * Convert a traffic selector address range to subnet and its mask.
658 static void ts2subnet(traffic_selector_t
* ts
,
659 xfrm_address_t
*net
, u_int8_t
*mask
)
664 ts
->to_subnet(ts
, &net_host
, mask
);
665 net_chunk
= net_host
->get_address(net_host
);
666 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
667 net_host
->destroy(net_host
);
671 * Convert a traffic selector port range to port/portmask
673 static void ts2ports(traffic_selector_t
* ts
,
674 u_int16_t
*port
, u_int16_t
*mask
)
676 /* Linux does not seem to accept complex portmasks. Only
677 * any or a specific port is allowed. We set to any, if we have
678 * a port range, or to a specific, if we have one port only.
682 from
= ts
->get_from_port(ts
);
683 to
= ts
->get_to_port(ts
);
698 * Convert a pair of traffic_selectors to an xfrm_selector
700 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
701 traffic_selector_t
*dst
)
703 struct xfrm_selector sel
;
705 memset(&sel
, 0, sizeof(sel
));
706 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
707 /* src or dest proto may be "any" (0), use more restrictive one */
708 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
709 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
710 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
711 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
712 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
720 * Convert an xfrm_selector to a src|dst traffic_selector
722 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
731 addr
= (u_char
*)&sel
->saddr
;
732 prefixlen
= sel
->prefixlen_s
;
735 port
= htons(sel
->sport
);
740 addr
= (u_char
*)&sel
->daddr
;
741 prefixlen
= sel
->prefixlen_d
;
744 port
= htons(sel
->dport
);
748 /* The Linux 2.6 kernel does not set the selector's family field,
749 * so as a kludge we additionally test the prefix length.
751 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
753 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
755 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
757 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
762 return traffic_selector_create_from_subnet(host
, prefixlen
,
769 * Process a XFRM_MSG_ACQUIRE from kernel
771 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
772 struct nlmsghdr
*hdr
)
774 struct xfrm_user_acquire
*acquire
;
777 traffic_selector_t
*src_ts
, *dst_ts
;
781 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
782 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
783 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
785 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
787 while (RTA_OK(rta
, rtasize
))
789 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
791 if (rta
->rta_type
== XFRMA_TMPL
)
793 struct xfrm_user_tmpl
* tmpl
;
794 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
796 proto
= tmpl
->id
.proto
;
798 rta
= RTA_NEXT(rta
, rtasize
);
807 /* acquire for AH/ESP only, not for IPCOMP */
810 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
811 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
813 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
818 * Process a XFRM_MSG_EXPIRE from kernel
820 static void process_expire(private_kernel_netlink_ipsec_t
*this,
821 struct nlmsghdr
*hdr
)
823 struct xfrm_user_expire
*expire
;
824 u_int32_t spi
, reqid
;
827 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
828 protocol
= expire
->state
.id
.proto
;
829 spi
= expire
->state
.id
.spi
;
830 reqid
= expire
->state
.reqid
;
832 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
834 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
836 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and "
837 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
841 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
842 spi
, expire
->hard
!= 0);
846 * Process a XFRM_MSG_MIGRATE from kernel
848 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
849 struct nlmsghdr
*hdr
)
851 struct xfrm_userpolicy_id
*policy_id
;
854 traffic_selector_t
*src_ts
, *dst_ts
;
855 host_t
*local
= NULL
, *remote
= NULL
;
856 host_t
*old_src
= NULL
, *old_dst
= NULL
;
857 host_t
*new_src
= NULL
, *new_dst
= NULL
;
861 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
862 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
863 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
865 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
867 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
868 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
869 dir
= (policy_dir_t
)policy_id
->dir
;
871 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
873 while (RTA_OK(rta
, rtasize
))
875 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
876 if (rta
->rta_type
== XFRMA_KMADDRESS
)
878 struct xfrm_user_kmaddress
*kmaddress
;
880 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
881 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
882 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
883 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
885 else if (rta
->rta_type
== XFRMA_MIGRATE
)
887 struct xfrm_user_migrate
*migrate
;
889 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
890 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
891 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
892 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
893 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
894 reqid
= migrate
->reqid
;
895 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
896 old_src
, old_dst
, new_src
, new_dst
, reqid
);
902 rta
= RTA_NEXT(rta
, rtasize
);
905 if (src_ts
&& dst_ts
&& local
&& remote
)
907 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
908 src_ts
, dst_ts
, dir
, local
, remote
);
920 * Process a XFRM_MSG_MAPPING from kernel
922 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
923 struct nlmsghdr
*hdr
)
925 struct xfrm_user_mapping
*mapping
;
926 u_int32_t spi
, reqid
;
928 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
929 spi
= mapping
->id
.spi
;
930 reqid
= mapping
->reqid
;
932 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
934 if (mapping
->id
.proto
== IPPROTO_ESP
)
937 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
941 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
948 * Receives events from kernel
950 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
953 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
954 struct sockaddr_nl addr
;
955 socklen_t addr_len
= sizeof(addr
);
959 oldstate
= thread_cancelability(TRUE
);
960 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
961 (struct sockaddr
*)&addr
, &addr_len
);
962 thread_cancelability(oldstate
);
969 /* interrupted, try again */
970 return JOB_REQUEUE_DIRECT
;
972 /* no data ready, select again */
973 return JOB_REQUEUE_DIRECT
;
975 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
977 return JOB_REQUEUE_FAIR
;
981 if (addr
.nl_pid
!= 0)
982 { /* not from kernel. not interested, try another one */
983 return JOB_REQUEUE_DIRECT
;
986 while (NLMSG_OK(hdr
, len
))
988 switch (hdr
->nlmsg_type
)
990 case XFRM_MSG_ACQUIRE
:
991 process_acquire(this, hdr
);
993 case XFRM_MSG_EXPIRE
:
994 process_expire(this, hdr
);
996 case XFRM_MSG_MIGRATE
:
997 process_migrate(this, hdr
);
999 case XFRM_MSG_MAPPING
:
1000 process_mapping(this, hdr
);
1003 DBG1(DBG_KNL
, "received unknown event from xfrm event "
1004 "socket: %d", hdr
->nlmsg_type
);
1007 hdr
= NLMSG_NEXT(hdr
, len
);
1009 return JOB_REQUEUE_DIRECT
;
1013 * Get an SPI for a specific protocol from the kernel.
1015 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1016 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1017 u_int32_t reqid
, u_int32_t
*spi
)
1019 netlink_buf_t request
;
1020 struct nlmsghdr
*hdr
, *out
;
1021 struct xfrm_userspi_info
*userspi
;
1022 u_int32_t received_spi
= 0;
1025 memset(&request
, 0, sizeof(request
));
1027 hdr
= (struct nlmsghdr
*)request
;
1028 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1029 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1030 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1032 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1033 host2xfrm(src
, &userspi
->info
.saddr
);
1034 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1035 userspi
->info
.id
.proto
= proto
;
1036 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1037 userspi
->info
.reqid
= reqid
;
1038 userspi
->info
.family
= src
->get_family(src
);
1042 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1045 while (NLMSG_OK(hdr
, len
))
1047 switch (hdr
->nlmsg_type
)
1049 case XFRM_MSG_NEWSA
:
1051 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1052 received_spi
= usersa
->id
.spi
;
1057 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1058 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1059 strerror(-err
->error
), -err
->error
);
1063 hdr
= NLMSG_NEXT(hdr
, len
);
1073 if (received_spi
== 0)
1078 *spi
= received_spi
;
1082 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1083 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1084 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1086 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
1088 if (get_spi_internal(this, src
, dst
, protocol
,
1089 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1091 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
1095 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
1099 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1100 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1101 u_int32_t reqid
, u_int16_t
*cpi
)
1103 u_int32_t received_spi
= 0;
1105 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
1107 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1108 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
1110 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
1114 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
1116 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
1120 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1121 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1122 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
1123 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1124 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
1125 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1126 traffic_selector_t
* src_ts
, traffic_selector_t
* dst_ts
)
1128 netlink_buf_t request
;
1130 struct nlmsghdr
*hdr
;
1131 struct xfrm_usersa_info
*sa
;
1132 u_int16_t icv_size
= 64;
1133 status_t status
= FAILED
;
1135 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1136 * we are in the recursive call below */
1137 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
1139 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1140 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, mark
,
1141 tfc
, &lft
, ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
,
1142 chunk_empty
, mode
, ipcomp
, 0, FALSE
, FALSE
, inbound
, NULL
, NULL
);
1143 ipcomp
= IPCOMP_NONE
;
1144 /* use transport mode ESP SA, IPComp uses tunnel mode */
1145 mode
= MODE_TRANSPORT
;
1148 memset(&request
, 0, sizeof(request
));
1150 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u} (mark "
1151 "%u/0x%8x)", ntohl(spi
), reqid
, mark
.value
, mark
.mask
);
1153 hdr
= (struct nlmsghdr
*)request
;
1154 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1155 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1156 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1158 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1159 host2xfrm(src
, &sa
->saddr
);
1160 host2xfrm(dst
, &sa
->id
.daddr
);
1162 sa
->id
.proto
= protocol
;
1163 sa
->family
= src
->get_family(src
);
1164 sa
->mode
= mode2kernel(mode
);
1168 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1171 case MODE_TRANSPORT
:
1172 if(src_ts
&& dst_ts
)
1174 sa
->sel
= ts2selector(src_ts
, dst_ts
);
1182 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.rekey
);
1183 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.life
);
1184 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(lifetime
->packets
.rekey
);
1185 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(lifetime
->packets
.life
);
1186 /* we use lifetimes since added, not since used */
1187 sa
->lft
.soft_add_expires_seconds
= lifetime
->time
.rekey
;
1188 sa
->lft
.hard_add_expires_seconds
= lifetime
->time
.life
;
1189 sa
->lft
.soft_use_expires_seconds
= 0;
1190 sa
->lft
.hard_use_expires_seconds
= 0;
1192 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1196 case ENCR_UNDEFINED
:
1199 case ENCR_AES_CCM_ICV16
:
1200 case ENCR_AES_GCM_ICV16
:
1201 case ENCR_NULL_AUTH_AES_GMAC
:
1202 case ENCR_CAMELLIA_CCM_ICV16
:
1205 case ENCR_AES_CCM_ICV12
:
1206 case ENCR_AES_GCM_ICV12
:
1207 case ENCR_CAMELLIA_CCM_ICV12
:
1210 case ENCR_AES_CCM_ICV8
:
1211 case ENCR_AES_GCM_ICV8
:
1212 case ENCR_CAMELLIA_CCM_ICV8
:
1214 struct xfrm_algo_aead
*algo
;
1216 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1217 if (alg_name
== NULL
)
1219 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1220 encryption_algorithm_names
, enc_alg
);
1223 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1224 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1226 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
1227 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) +
1229 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1230 if (hdr
->nlmsg_len
> sizeof(request
))
1235 algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
1236 algo
->alg_key_len
= enc_key
.len
* 8;
1237 algo
->alg_icv_len
= icv_size
;
1238 strcpy(algo
->alg_name
, alg_name
);
1239 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1241 rthdr
= XFRM_RTA_NEXT(rthdr
);
1246 struct xfrm_algo
*algo
;
1248 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1249 if (alg_name
== NULL
)
1251 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1252 encryption_algorithm_names
, enc_alg
);
1255 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1256 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1258 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1259 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
1260 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1261 if (hdr
->nlmsg_len
> sizeof(request
))
1266 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1267 algo
->alg_key_len
= enc_key
.len
* 8;
1268 strcpy(algo
->alg_name
, alg_name
);
1269 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1271 rthdr
= XFRM_RTA_NEXT(rthdr
);
1275 if (int_alg
!= AUTH_UNDEFINED
)
1277 u_int trunc_len
= 0;
1279 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
1280 if (alg_name
== NULL
)
1282 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1283 integrity_algorithm_names
, int_alg
);
1286 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1287 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1291 case AUTH_HMAC_MD5_128
:
1292 case AUTH_HMAC_SHA2_256_128
:
1295 case AUTH_HMAC_SHA1_160
:
1304 struct xfrm_algo_auth
* algo
;
1306 /* the kernel uses SHA256 with 96 bit truncation by default,
1307 * use specified truncation size supported by newer kernels.
1308 * also use this for untruncated MD5 and SHA1. */
1309 rthdr
->rta_type
= XFRMA_ALG_AUTH_TRUNC
;
1310 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_auth
) +
1313 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1314 if (hdr
->nlmsg_len
> sizeof(request
))
1319 algo
= (struct xfrm_algo_auth
*)RTA_DATA(rthdr
);
1320 algo
->alg_key_len
= int_key
.len
* 8;
1321 algo
->alg_trunc_len
= trunc_len
;
1322 strcpy(algo
->alg_name
, alg_name
);
1323 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1327 struct xfrm_algo
* algo
;
1329 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1330 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1332 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1333 if (hdr
->nlmsg_len
> sizeof(request
))
1338 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1339 algo
->alg_key_len
= int_key
.len
* 8;
1340 strcpy(algo
->alg_name
, alg_name
);
1341 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1343 rthdr
= XFRM_RTA_NEXT(rthdr
);
1346 if (ipcomp
!= IPCOMP_NONE
)
1348 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1349 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1350 if (alg_name
== NULL
)
1352 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1353 ipcomp_transform_names
, ipcomp
);
1356 DBG2(DBG_KNL
, " using compression algorithm %N",
1357 ipcomp_transform_names
, ipcomp
);
1359 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1360 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1361 if (hdr
->nlmsg_len
> sizeof(request
))
1366 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1367 algo
->alg_key_len
= 0;
1368 strcpy(algo
->alg_name
, alg_name
);
1370 rthdr
= XFRM_RTA_NEXT(rthdr
);
1375 struct xfrm_encap_tmpl
*tmpl
;
1377 rthdr
->rta_type
= XFRMA_ENCAP
;
1378 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1380 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1381 if (hdr
->nlmsg_len
> sizeof(request
))
1386 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1387 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1388 tmpl
->encap_sport
= htons(src
->get_port(src
));
1389 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1390 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1391 /* encap_oa could probably be derived from the
1392 * traffic selectors [rfc4306, p39]. In the netlink kernel
1393 * implementation pluto does the same as we do here but it uses
1394 * encap_oa in the pfkey implementation.
1395 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1397 * -> does that mean that NAT-T encap doesn't work in transport mode?
1398 * No. The reason the kernel ignores NAT-OA is that it recomputes
1399 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1400 * checks it marks them "checksum ok" so OA isn't needed. */
1401 rthdr
= XFRM_RTA_NEXT(rthdr
);
1406 struct xfrm_mark
*mrk
;
1408 rthdr
->rta_type
= XFRMA_MARK
;
1409 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1411 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1412 if (hdr
->nlmsg_len
> sizeof(request
))
1417 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1418 mrk
->v
= mark
.value
;
1420 rthdr
= XFRM_RTA_NEXT(rthdr
);
1427 rthdr
->rta_type
= XFRMA_TFCPAD
;
1428 rthdr
->rta_len
= RTA_LENGTH(sizeof(u_int32_t
));
1430 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1431 if (hdr
->nlmsg_len
> sizeof(request
))
1436 tfcpad
= (u_int32_t
*)RTA_DATA(rthdr
);
1438 rthdr
= XFRM_RTA_NEXT(rthdr
);
1441 if (protocol
!= IPPROTO_COMP
)
1443 if (esn
|| this->replay_window
> DEFAULT_REPLAY_WINDOW
)
1445 /* for ESN or larger replay windows we need the new
1446 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1447 struct xfrm_replay_state_esn
*replay
;
1449 rthdr
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1450 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1451 (this->replay_window
+ 7) / 8);
1453 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1454 if (hdr
->nlmsg_len
> sizeof(request
))
1459 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(rthdr
);
1460 /* bmp_len contains number uf __u32's */
1461 replay
->bmp_len
= this->replay_bmp
;
1462 replay
->replay_window
= this->replay_window
;
1463 DBG2(DBG_KNL
, " using replay window of %u bytes",
1464 this->replay_window
);
1466 rthdr
= XFRM_RTA_NEXT(rthdr
);
1469 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1470 sa
->flags
|= XFRM_STATE_ESN
;
1475 sa
->replay_window
= DEFAULT_REPLAY_WINDOW
;
1479 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1483 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x "
1484 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1488 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1496 memwipe(request
, sizeof(request
));
1501 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1503 * Allocates into one the replay state structure we get from the kernel.
1505 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1506 u_int32_t spi
, u_int8_t protocol
, host_t
*dst
,
1507 struct xfrm_replay_state_esn
**replay_esn
,
1508 struct xfrm_replay_state
**replay
)
1510 netlink_buf_t request
;
1511 struct nlmsghdr
*hdr
, *out
= NULL
;
1512 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1517 memset(&request
, 0, sizeof(request
));
1519 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1522 hdr
= (struct nlmsghdr
*)request
;
1523 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1524 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1525 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1527 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1528 aevent_id
->flags
= XFRM_AE_RVAL
;
1530 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1531 aevent_id
->sa_id
.spi
= spi
;
1532 aevent_id
->sa_id
.proto
= protocol
;
1533 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1535 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1538 while (NLMSG_OK(hdr
, len
))
1540 switch (hdr
->nlmsg_type
)
1542 case XFRM_MSG_NEWAE
:
1544 out_aevent
= NLMSG_DATA(hdr
);
1549 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1550 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1551 "failed: %s (%d)", strerror(-err
->error
),
1556 hdr
= NLMSG_NEXT(hdr
, len
);
1567 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1568 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1569 while (RTA_OK(rta
, rtasize
))
1571 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1572 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1574 *replay
= malloc(RTA_PAYLOAD(rta
));
1575 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1578 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1579 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
) + this->replay_bmp
)
1581 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1582 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1585 rta
= RTA_NEXT(rta
, rtasize
);
1591 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1592 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1593 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1595 netlink_buf_t request
;
1596 struct nlmsghdr
*out
= NULL
, *hdr
;
1597 struct xfrm_usersa_id
*sa_id
;
1598 struct xfrm_usersa_info
*sa
= NULL
;
1599 status_t status
= FAILED
;
1602 memset(&request
, 0, sizeof(request
));
1604 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%8x)",
1605 ntohl(spi
), mark
.value
, mark
.mask
);
1607 hdr
= (struct nlmsghdr
*)request
;
1608 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1609 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1610 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1612 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1613 host2xfrm(dst
, &sa_id
->daddr
);
1615 sa_id
->proto
= protocol
;
1616 sa_id
->family
= dst
->get_family(dst
);
1620 struct xfrm_mark
*mrk
;
1621 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1623 rthdr
->rta_type
= XFRMA_MARK
;
1624 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1625 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1626 if (hdr
->nlmsg_len
> sizeof(request
))
1631 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1632 mrk
->v
= mark
.value
;
1636 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1639 while (NLMSG_OK(hdr
, len
))
1641 switch (hdr
->nlmsg_type
)
1643 case XFRM_MSG_NEWSA
:
1645 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1650 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1654 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1655 "(mark %u/0x%8x) failed: %s (%d)",
1656 ntohl(spi
), mark
.value
, mark
.mask
,
1657 strerror(-err
->error
), -err
->error
);
1661 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1662 "failed: %s (%d)", ntohl(spi
),
1663 strerror(-err
->error
), -err
->error
);
1668 hdr
= NLMSG_NEXT(hdr
, len
);
1679 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1683 *bytes
= sa
->curlft
.bytes
;
1691 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1692 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1693 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1695 netlink_buf_t request
;
1696 struct nlmsghdr
*hdr
;
1697 struct xfrm_usersa_id
*sa_id
;
1699 /* if IPComp was used, we first delete the additional IPComp SA */
1702 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1705 memset(&request
, 0, sizeof(request
));
1707 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%8x)",
1708 ntohl(spi
), mark
.value
, mark
.mask
);
1710 hdr
= (struct nlmsghdr
*)request
;
1711 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1712 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1713 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1715 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1716 host2xfrm(dst
, &sa_id
->daddr
);
1718 sa_id
->proto
= protocol
;
1719 sa_id
->family
= dst
->get_family(dst
);
1723 struct xfrm_mark
*mrk
;
1724 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1726 rthdr
->rta_type
= XFRMA_MARK
;
1727 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1728 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1729 if (hdr
->nlmsg_len
> sizeof(request
))
1734 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1735 mrk
->v
= mark
.value
;
1739 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
1742 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%08x)",
1743 ntohl(spi
), mark
.value
, mark
.mask
);
1750 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1751 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1755 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1762 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1763 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1764 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1765 bool old_encap
, bool new_encap
, mark_t mark
)
1767 netlink_buf_t request
;
1769 struct nlmsghdr
*hdr
, *out
= NULL
;
1770 struct xfrm_usersa_id
*sa_id
;
1771 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1775 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1776 struct xfrm_replay_state
*replay
= NULL
;
1777 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1778 status_t status
= FAILED
;
1780 /* if IPComp is used, we first update the IPComp SA */
1783 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1784 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1787 memset(&request
, 0, sizeof(request
));
1789 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1791 /* query the existing SA first */
1792 hdr
= (struct nlmsghdr
*)request
;
1793 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1794 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1795 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1797 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1798 host2xfrm(dst
, &sa_id
->daddr
);
1800 sa_id
->proto
= protocol
;
1801 sa_id
->family
= dst
->get_family(dst
);
1803 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1806 while (NLMSG_OK(hdr
, len
))
1808 switch (hdr
->nlmsg_type
)
1810 case XFRM_MSG_NEWSA
:
1812 out_sa
= NLMSG_DATA(hdr
);
1817 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1818 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1819 strerror(-err
->error
), -err
->error
);
1823 hdr
= NLMSG_NEXT(hdr
, len
);
1833 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1837 get_replay_state(this, spi
, protocol
, dst
, &replay_esn
, &replay
);
1839 /* delete the old SA (without affecting the IPComp SA) */
1840 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1842 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1847 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1848 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1849 /* copy over the SA from out to request */
1850 hdr
= (struct nlmsghdr
*)request
;
1851 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1852 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1853 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1854 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1855 sa
= NLMSG_DATA(hdr
);
1856 sa
->family
= new_dst
->get_family(new_dst
);
1858 if (!src
->ip_equals(src
, new_src
))
1860 host2xfrm(new_src
, &sa
->saddr
);
1862 if (!dst
->ip_equals(dst
, new_dst
))
1864 host2xfrm(new_dst
, &sa
->id
.daddr
);
1867 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1868 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1869 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1870 while(RTA_OK(rta
, rtasize
))
1872 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1873 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1875 if (rta
->rta_type
== XFRMA_ENCAP
)
1876 { /* update encap tmpl */
1877 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1878 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1879 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1881 memcpy(pos
, rta
, rta
->rta_len
);
1882 pos
+= RTA_ALIGN(rta
->rta_len
);
1883 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1885 rta
= RTA_NEXT(rta
, rtasize
);
1888 rta
= (struct rtattr
*)pos
;
1889 if (tmpl
== NULL
&& new_encap
)
1890 { /* add tmpl if we are enabling it */
1891 rta
->rta_type
= XFRMA_ENCAP
;
1892 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1894 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1895 if (hdr
->nlmsg_len
> sizeof(request
))
1900 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1901 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1902 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1903 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1904 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1906 rta
= XFRM_RTA_NEXT(rta
);
1911 rta
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1912 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1915 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1916 if (hdr
->nlmsg_len
> sizeof(request
))
1920 memcpy(RTA_DATA(rta
), replay_esn
,
1921 sizeof(struct xfrm_replay_state_esn
) + this->replay_bmp
);
1923 rta
= XFRM_RTA_NEXT(rta
);
1927 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1928 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1930 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1931 if (hdr
->nlmsg_len
> sizeof(request
))
1935 memcpy(RTA_DATA(rta
), replay
, sizeof(replay
));
1937 rta
= XFRM_RTA_NEXT(rta
);
1941 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1942 "with SPI %.8x", ntohl(spi
));
1945 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1947 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1956 memwipe(request
, sizeof(request
));
1962 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1963 private_kernel_netlink_ipsec_t
*this)
1965 netlink_buf_t request
;
1966 struct nlmsghdr
*hdr
;
1967 struct xfrm_usersa_flush
*flush
;
1969 memset(&request
, 0, sizeof(request
));
1971 DBG2(DBG_KNL
, "flushing all SAD entries");
1973 hdr
= (struct nlmsghdr
*)request
;
1974 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1975 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
1976 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1978 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
1979 flush
->proto
= IPSEC_PROTO_ANY
;
1981 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1983 DBG1(DBG_KNL
, "unable to flush SAD entries");
1990 * Add or update a policy in the kernel.
1992 * Note: The mutex has to be locked when entering this function
1993 * and is unlocked here in any case.
1995 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
1996 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
1998 netlink_buf_t request
;
1999 policy_entry_t clone
;
2000 ipsec_sa_t
*ipsec
= mapping
->sa
;
2001 struct xfrm_userpolicy_info
*policy_info
;
2002 struct nlmsghdr
*hdr
;
2005 /* clone the policy so we are able to check it out again later */
2006 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2008 memset(&request
, 0, sizeof(request
));
2009 hdr
= (struct nlmsghdr
*)request
;
2010 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2011 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2012 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2014 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2015 policy_info
->sel
= policy
->sel
;
2016 policy_info
->dir
= policy
->direction
;
2018 /* calculate priority based on selector size, small size = high prio */
2019 policy_info
->priority
= mapping
->priority
;
2020 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2021 : XFRM_POLICY_BLOCK
;
2022 policy_info
->share
= XFRM_SHARE_ANY
;
2024 /* policies don't expire */
2025 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2026 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2027 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2028 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2029 policy_info
->lft
.soft_add_expires_seconds
= 0;
2030 policy_info
->lft
.hard_add_expires_seconds
= 0;
2031 policy_info
->lft
.soft_use_expires_seconds
= 0;
2032 policy_info
->lft
.hard_use_expires_seconds
= 0;
2034 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2036 if (mapping
->type
== POLICY_IPSEC
)
2038 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2043 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2044 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2045 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2047 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2049 rthdr
->rta_type
= XFRMA_TMPL
;
2050 rthdr
->rta_len
= 0; /* actual length is set below */
2052 for (i
= 0; i
< countof(protos
); i
++)
2059 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2060 hdr
->nlmsg_len
+= RTA_ALIGN(RTA_LENGTH(sizeof(struct xfrm_user_tmpl
)));
2061 if (hdr
->nlmsg_len
> sizeof(request
))
2063 this->mutex
->unlock(this->mutex
);
2067 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2068 tmpl
->id
.proto
= protos
[i
].proto
;
2069 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2070 tmpl
->mode
= mode2kernel(proto_mode
);
2071 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2072 policy
->direction
!= POLICY_OUT
;
2073 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2075 if (proto_mode
== MODE_TUNNEL
)
2076 { /* only for tunnel mode */
2077 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2078 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2083 /* use transport mode for other SAs */
2084 proto_mode
= MODE_TRANSPORT
;
2087 rthdr
= XFRM_RTA_NEXT(rthdr
);
2090 if (ipsec
->mark
.value
)
2092 struct xfrm_mark
*mrk
;
2094 rthdr
->rta_type
= XFRMA_MARK
;
2095 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2097 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2098 if (hdr
->nlmsg_len
> sizeof(request
))
2100 this->mutex
->unlock(this->mutex
);
2104 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2105 mrk
->v
= ipsec
->mark
.value
;
2106 mrk
->m
= ipsec
->mark
.mask
;
2108 this->mutex
->unlock(this->mutex
);
2110 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2115 /* find the policy again */
2116 this->mutex
->lock(this->mutex
);
2117 policy
= this->policies
->get(this->policies
, &clone
);
2119 policy
->used_by
->find_first(policy
->used_by
,
2120 NULL
, (void**)&mapping
) != SUCCESS
)
2121 { /* policy or mapping is already gone, ignore */
2122 this->mutex
->unlock(this->mutex
);
2126 /* install a route, if:
2127 * - this is a forward policy (to just get one for each child)
2128 * - we are in tunnel/BEET mode
2129 * - routing is not disabled via strongswan.conf
2131 if (policy
->direction
== POLICY_FWD
&&
2132 ipsec
->cfg
.mode
!= MODE_TRANSPORT
&& this->install_routes
)
2134 route_entry_t
*route
= malloc_thing(route_entry_t
);
2135 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2137 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2138 fwd
->dst_ts
, &route
->src_ip
) == SUCCESS
)
2140 /* get the nexthop to src (src as we are in POLICY_FWD) */
2141 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2142 hydra
->kernel_interface
, ipsec
->src
);
2143 /* install route via outgoing interface */
2144 route
->if_name
= hydra
->kernel_interface
->get_interface(
2145 hydra
->kernel_interface
, ipsec
->dst
);
2146 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2147 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2148 route
->prefixlen
= policy
->sel
.prefixlen_s
;
2150 if (!route
->if_name
)
2152 this->mutex
->unlock(this->mutex
);
2153 route_entry_destroy(route
);
2159 route_entry_t
*old
= policy
->route
;
2160 if (route_entry_equals(old
, route
))
2162 this->mutex
->unlock(this->mutex
);
2163 route_entry_destroy(route
);
2166 /* uninstall previously installed route */
2167 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2168 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2169 old
->src_ip
, old
->if_name
) != SUCCESS
)
2171 DBG1(DBG_KNL
, "error uninstalling route installed with "
2172 "policy %R === %R %N", fwd
->src_ts
,
2173 fwd
->dst_ts
, policy_dir_names
,
2176 route_entry_destroy(old
);
2177 policy
->route
= NULL
;
2180 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2181 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2182 switch (hydra
->kernel_interface
->add_route(
2183 hydra
->kernel_interface
, route
->dst_net
,
2184 route
->prefixlen
, route
->gateway
,
2185 route
->src_ip
, route
->if_name
))
2188 DBG1(DBG_KNL
, "unable to install source route for %H",
2192 /* route exists, do not uninstall */
2193 route_entry_destroy(route
);
2196 /* cache the installed route */
2197 policy
->route
= route
;
2206 this->mutex
->unlock(this->mutex
);
2210 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2211 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2212 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2213 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2214 mark_t mark
, policy_priority_t priority
)
2216 policy_entry_t
*policy
, *current
;
2217 policy_sa_t
*assigned_sa
, *current_sa
;
2218 enumerator_t
*enumerator
;
2219 bool found
= FALSE
, update
= TRUE
;
2221 /* create a policy */
2223 .sel
= ts2selector(src_ts
, dst_ts
),
2224 .mark
= mark
.value
& mark
.mask
,
2225 .direction
= direction
,
2228 /* find the policy, which matches EXACTLY */
2229 this->mutex
->lock(this->mutex
);
2230 current
= this->policies
->get(this->policies
, policy
);
2233 /* use existing policy */
2234 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%8x) "
2235 "already exists, increasing refcount",
2236 src_ts
, dst_ts
, policy_dir_names
, direction
,
2237 mark
.value
, mark
.mask
);
2238 policy_entry_destroy(this, policy
);
2243 { /* use the new one, if we have no such policy */
2244 policy
->used_by
= linked_list_create();
2245 this->policies
->put(this->policies
, policy
, policy
);
2248 /* cache the assigned IPsec SA */
2249 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2251 assigned_sa
->priority
= get_priority(policy
, priority
);
2253 if (this->policy_history
)
2254 { /* insert the SA according to its priority */
2255 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2256 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2258 if (current_sa
->priority
>= assigned_sa
->priority
)
2264 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2266 enumerator
->destroy(enumerator
);
2269 { /* simply insert it last and only update if it is not installed yet */
2270 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2275 { /* we don't update the policy if the priority is lower than that of
2276 * the currently installed one */
2277 this->mutex
->unlock(this->mutex
);
2281 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%8x)",
2282 found ?
"updating" : "adding", src_ts
, dst_ts
,
2283 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2285 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2287 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2288 found ?
"update" : "add", src_ts
, dst_ts
,
2289 policy_dir_names
, direction
);
2295 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2296 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2297 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2298 u_int32_t
*use_time
)
2300 netlink_buf_t request
;
2301 struct nlmsghdr
*out
= NULL
, *hdr
;
2302 struct xfrm_userpolicy_id
*policy_id
;
2303 struct xfrm_userpolicy_info
*policy
= NULL
;
2306 memset(&request
, 0, sizeof(request
));
2308 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%8x)",
2309 src_ts
, dst_ts
, policy_dir_names
, direction
,
2310 mark
.value
, mark
.mask
);
2312 hdr
= (struct nlmsghdr
*)request
;
2313 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2314 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2315 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2317 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2318 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2319 policy_id
->dir
= direction
;
2323 struct xfrm_mark
*mrk
;
2324 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2326 rthdr
->rta_type
= XFRMA_MARK
;
2327 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2329 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2330 if (hdr
->nlmsg_len
> sizeof(request
))
2335 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2336 mrk
->v
= mark
.value
;
2340 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2343 while (NLMSG_OK(hdr
, len
))
2345 switch (hdr
->nlmsg_type
)
2347 case XFRM_MSG_NEWPOLICY
:
2349 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2354 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2355 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2356 strerror(-err
->error
), -err
->error
);
2360 hdr
= NLMSG_NEXT(hdr
, len
);
2371 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2372 policy_dir_names
, direction
);
2377 if (policy
->curlft
.use_time
)
2379 /* we need the monotonic time, but the kernel returns system time. */
2380 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2391 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2392 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2393 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2394 mark_t mark
, policy_priority_t prio
)
2396 policy_entry_t
*current
, policy
;
2397 enumerator_t
*enumerator
;
2398 policy_sa_t
*mapping
;
2399 netlink_buf_t request
;
2400 struct nlmsghdr
*hdr
;
2401 struct xfrm_userpolicy_id
*policy_id
;
2402 bool is_installed
= TRUE
;
2405 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x)",
2406 src_ts
, dst_ts
, policy_dir_names
, direction
,
2407 mark
.value
, mark
.mask
);
2409 /* create a policy */
2410 memset(&policy
, 0, sizeof(policy_entry_t
));
2411 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2412 policy
.mark
= mark
.value
& mark
.mask
;
2413 policy
.direction
= direction
;
2415 /* find the policy */
2416 this->mutex
->lock(this->mutex
);
2417 current
= this->policies
->get(this->policies
, &policy
);
2422 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x) "
2423 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2424 direction
, mark
.value
, mark
.mask
);
2428 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2429 src_ts
, dst_ts
, policy_dir_names
, direction
);
2431 this->mutex
->unlock(this->mutex
);
2435 if (this->policy_history
)
2436 { /* remove mapping to SA by reqid and priority */
2437 priority
= get_priority(current
, prio
);
2438 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2439 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2441 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2442 priority
== mapping
->priority
)
2444 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2445 policy_sa_destroy(mapping
, &direction
, this);
2448 is_installed
= FALSE
;
2450 enumerator
->destroy(enumerator
);
2453 { /* remove one of the SAs but don't update the policy */
2454 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2455 policy_sa_destroy(mapping
, &direction
, this);
2456 is_installed
= FALSE
;
2459 if (current
->used_by
->get_count(current
->used_by
) > 0)
2460 { /* policy is used by more SAs, keep in kernel */
2461 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2463 { /* no need to update as the policy was not installed for this SA */
2464 this->mutex
->unlock(this->mutex
);
2468 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%8x)",
2469 src_ts
, dst_ts
, policy_dir_names
, direction
,
2470 mark
.value
, mark
.mask
);
2472 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2473 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2475 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2476 src_ts
, dst_ts
, policy_dir_names
, direction
);
2482 memset(&request
, 0, sizeof(request
));
2484 hdr
= (struct nlmsghdr
*)request
;
2485 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2486 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2487 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2489 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2490 policy_id
->sel
= current
->sel
;
2491 policy_id
->dir
= direction
;
2495 struct xfrm_mark
*mrk
;
2496 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2498 rthdr
->rta_type
= XFRMA_MARK
;
2499 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2500 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2501 if (hdr
->nlmsg_len
> sizeof(request
))
2503 this->mutex
->unlock(this->mutex
);
2507 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2508 mrk
->v
= mark
.value
;
2514 route_entry_t
*route
= current
->route
;
2515 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2516 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2517 route
->src_ip
, route
->if_name
) != SUCCESS
)
2519 DBG1(DBG_KNL
, "error uninstalling route installed with "
2520 "policy %R === %R %N", src_ts
, dst_ts
,
2521 policy_dir_names
, direction
);
2525 this->policies
->remove(this->policies
, current
);
2526 policy_entry_destroy(this, current
);
2527 this->mutex
->unlock(this->mutex
);
2529 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2533 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2534 "(mark %u/0x%8x)", src_ts
, dst_ts
, policy_dir_names
,
2535 direction
, mark
.value
, mark
.mask
);
2539 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2540 src_ts
, dst_ts
, policy_dir_names
, direction
);
2547 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2548 private_kernel_netlink_ipsec_t
*this)
2550 netlink_buf_t request
;
2551 struct nlmsghdr
*hdr
;
2553 memset(&request
, 0, sizeof(request
));
2555 DBG2(DBG_KNL
, "flushing all policies from SPD");
2557 hdr
= (struct nlmsghdr
*)request
;
2558 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2559 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2560 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2562 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2563 * to main or sub policies (default is main) */
2565 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2567 DBG1(DBG_KNL
, "unable to flush SPD entries");
2574 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2575 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2577 struct xfrm_userpolicy_info policy
;
2578 u_int sol
, ipsec_policy
;
2584 ipsec_policy
= IP_XFRM_POLICY
;
2588 ipsec_policy
= IPV6_XFRM_POLICY
;
2594 memset(&policy
, 0, sizeof(policy
));
2595 policy
.action
= XFRM_POLICY_ALLOW
;
2596 policy
.sel
.family
= family
;
2598 policy
.dir
= XFRM_POLICY_OUT
;
2599 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2601 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2605 policy
.dir
= XFRM_POLICY_IN
;
2606 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2608 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2615 METHOD(kernel_ipsec_t
, destroy
, void,
2616 private_kernel_netlink_ipsec_t
*this)
2618 enumerator_t
*enumerator
;
2619 policy_entry_t
*policy
;
2623 this->job
->cancel(this->job
);
2625 if (this->socket_xfrm_events
> 0)
2627 close(this->socket_xfrm_events
);
2629 DESTROY_IF(this->socket_xfrm
);
2630 enumerator
= this->policies
->create_enumerator(this->policies
);
2631 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2633 policy_entry_destroy(this, policy
);
2635 enumerator
->destroy(enumerator
);
2636 this->policies
->destroy(this->policies
);
2637 this->sas
->destroy(this->sas
);
2638 this->mutex
->destroy(this->mutex
);
2643 * Described in header.
2645 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2647 private_kernel_netlink_ipsec_t
*this;
2648 bool register_for_events
= TRUE
;
2654 .get_spi
= _get_spi
,
2655 .get_cpi
= _get_cpi
,
2657 .update_sa
= _update_sa
,
2658 .query_sa
= _query_sa
,
2660 .flush_sas
= _flush_sas
,
2661 .add_policy
= _add_policy
,
2662 .query_policy
= _query_policy
,
2663 .del_policy
= _del_policy
,
2664 .flush_policies
= _flush_policies
,
2665 .bypass_socket
= _bypass_socket
,
2666 .destroy
= _destroy
,
2669 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2670 (hashtable_equals_t
)policy_equals
, 32),
2671 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2672 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2673 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2674 .policy_history
= TRUE
,
2675 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2676 "%s.install_routes", TRUE
, hydra
->daemon
),
2677 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2678 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2681 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2682 (sizeof(u_int32_t
) * 8);
2684 if (streq(hydra
->daemon
, "pluto"))
2685 { /* no routes for pluto, they are installed via updown script */
2686 this->install_routes
= FALSE
;
2687 /* no policy history for pluto */
2688 this->policy_history
= FALSE
;
2690 else if (streq(hydra
->daemon
, "starter"))
2691 { /* starter has no threads, so we do not register for kernel events */
2692 register_for_events
= FALSE
;
2695 /* disable lifetimes for allocated SPIs in kernel */
2696 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2699 ignore_result(write(fd
, "165", 3));
2703 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2704 if (!this->socket_xfrm
)
2710 if (register_for_events
)
2712 struct sockaddr_nl addr
;
2714 memset(&addr
, 0, sizeof(addr
));
2715 addr
.nl_family
= AF_NETLINK
;
2717 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2718 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2719 if (this->socket_xfrm_events
<= 0)
2721 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2725 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2726 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2727 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2729 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2733 this->job
= callback_job_create_with_prio((callback_job_cb_t
)receive_events
,
2734 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
2735 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
2738 return &this->public;