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 /* 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 a pointer to the next rtattr following rta.
99 * !!! Do not use this to parse messages. Use RTA_NEXT and RTA_OK instead !!!
101 #define XFRM_RTA_NEXT(rta) ((struct rtattr*)(((char*)(rta)) + \
102 RTA_ALIGN((rta)->rta_len)))
104 * Returns the total size of attached rta data
105 * (after 'usual' netlink data x like 'struct xfrm_usersa_info')
107 #define XFRM_PAYLOAD(nlh, x) NLMSG_PAYLOAD(nlh, sizeof(x))
109 typedef struct kernel_algorithm_t kernel_algorithm_t
;
112 * Mapping of IKEv2 kernel identifier to linux crypto API names
114 struct kernel_algorithm_t
{
116 * Identifier specified in IKEv2
121 * Name of the algorithm in linux crypto API
126 ENUM(xfrm_msg_names
, XFRM_MSG_NEWSA
, XFRM_MSG_MAPPING
,
130 "XFRM_MSG_NEWPOLICY",
131 "XFRM_MSG_DELPOLICY",
132 "XFRM_MSG_GETPOLICY",
136 "XFRM_MSG_UPDPOLICY",
138 "XFRM_MSG_POLEXPIRE",
140 "XFRM_MSG_FLUSHPOLICY",
145 "XFRM_MSG_NEWSADINFO",
146 "XFRM_MSG_GETSADINFO",
147 "XFRM_MSG_NEWSPDINFO",
148 "XFRM_MSG_GETSPDINFO",
152 ENUM(xfrm_attr_type_names
, XFRMA_UNSPEC
, XFRMA_REPLAY_ESN_VAL
,
164 "XFRMA_REPLAY_THRESH",
165 "XFRMA_ETIMER_THRESH",
173 "XFRMA_ALG_AUTH_TRUNC",
176 "XFRMA_REPLAY_ESN_VAL",
179 #define END_OF_LIST -1
182 * Algorithms for encryption
184 static kernel_algorithm_t encryption_algs
[] = {
185 /* {ENCR_DES_IV64, "***" }, */
187 {ENCR_3DES
, "des3_ede" },
188 /* {ENCR_RC5, "***" }, */
189 /* {ENCR_IDEA, "***" }, */
190 {ENCR_CAST
, "cast128" },
191 {ENCR_BLOWFISH
, "blowfish" },
192 /* {ENCR_3IDEA, "***" }, */
193 /* {ENCR_DES_IV32, "***" }, */
194 {ENCR_NULL
, "cipher_null" },
195 {ENCR_AES_CBC
, "aes" },
196 {ENCR_AES_CTR
, "rfc3686(ctr(aes))" },
197 {ENCR_AES_CCM_ICV8
, "rfc4309(ccm(aes))" },
198 {ENCR_AES_CCM_ICV12
, "rfc4309(ccm(aes))" },
199 {ENCR_AES_CCM_ICV16
, "rfc4309(ccm(aes))" },
200 {ENCR_AES_GCM_ICV8
, "rfc4106(gcm(aes))" },
201 {ENCR_AES_GCM_ICV12
, "rfc4106(gcm(aes))" },
202 {ENCR_AES_GCM_ICV16
, "rfc4106(gcm(aes))" },
203 {ENCR_NULL_AUTH_AES_GMAC
, "rfc4543(gcm(aes))" },
204 {ENCR_CAMELLIA_CBC
, "cbc(camellia)" },
205 /* {ENCR_CAMELLIA_CTR, "***" }, */
206 /* {ENCR_CAMELLIA_CCM_ICV8, "***" }, */
207 /* {ENCR_CAMELLIA_CCM_ICV12, "***" }, */
208 /* {ENCR_CAMELLIA_CCM_ICV16, "***" }, */
209 {ENCR_SERPENT_CBC
, "serpent" },
210 {ENCR_TWOFISH_CBC
, "twofish" },
215 * Algorithms for integrity protection
217 static kernel_algorithm_t integrity_algs
[] = {
218 {AUTH_HMAC_MD5_96
, "md5" },
219 {AUTH_HMAC_MD5_128
, "hmac(md5)" },
220 {AUTH_HMAC_SHA1_96
, "sha1" },
221 {AUTH_HMAC_SHA1_160
, "hmac(sha1)" },
222 {AUTH_HMAC_SHA2_256_96
, "sha256" },
223 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
224 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
225 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
226 /* {AUTH_DES_MAC, "***" }, */
227 /* {AUTH_KPDK_MD5, "***" }, */
228 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
233 * Algorithms for IPComp
235 static kernel_algorithm_t compression_algs
[] = {
236 /* {IPCOMP_OUI, "***" }, */
237 {IPCOMP_DEFLATE
, "deflate" },
238 {IPCOMP_LZS
, "lzs" },
239 {IPCOMP_LZJH
, "lzjh" },
244 * Look up a kernel algorithm name and its key size
246 static char* lookup_algorithm(transform_type_t type
, int ikev2
)
248 kernel_algorithm_t
*list
;
253 case ENCRYPTION_ALGORITHM
:
254 list
= encryption_algs
;
256 case INTEGRITY_ALGORITHM
:
257 list
= integrity_algs
;
259 case COMPRESSION_ALGORITHM
:
260 list
= compression_algs
;
265 while (list
->ikev2
!= END_OF_LIST
)
267 if (list
->ikev2
== ikev2
)
273 hydra
->kernel_interface
->lookup_algorithm(hydra
->kernel_interface
, ikev2
,
278 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
281 * Private variables and functions of kernel_netlink class.
283 struct private_kernel_netlink_ipsec_t
{
285 * Public part of the kernel_netlink_t object
287 kernel_netlink_ipsec_t
public;
290 * Mutex to lock access to installed policies
295 * Hash table of installed policies (policy_entry_t)
297 hashtable_t
*policies
;
300 * Hash table of IPsec SAs using policies (ipsec_sa_t)
305 * Netlink xfrm socket (IPsec)
307 netlink_socket_t
*socket_xfrm
;
310 * Netlink xfrm socket to receive acquire and expire events
312 int socket_xfrm_events
;
315 * Whether to install routes along policies
320 * Whether to track the history of a policy
325 * Size of the replay window, in packets
327 u_int32_t replay_window
;
330 * Size of the replay window bitmap, in bytes
332 u_int32_t replay_bmp
;
335 typedef struct route_entry_t route_entry_t
;
338 * Installed routing entry
340 struct route_entry_t
{
341 /** Name of the interface the route is bound to */
344 /** Source ip of the route */
347 /** Gateway for this route */
350 /** Destination net */
353 /** Destination net prefixlen */
358 * Destroy a route_entry_t object
360 static void route_entry_destroy(route_entry_t
*this)
363 this->src_ip
->destroy(this->src_ip
);
364 DESTROY_IF(this->gateway
);
365 chunk_free(&this->dst_net
);
370 * Compare two route_entry_t objects
372 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
374 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
375 a
->src_ip
->ip_equals(a
->src_ip
, b
->src_ip
) &&
376 a
->gateway
->ip_equals(a
->gateway
, b
->gateway
) &&
377 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
380 typedef struct ipsec_sa_t ipsec_sa_t
;
383 * IPsec SA assigned to a policy.
386 /** Source address of this SA */
389 /** Destination address of this SA */
395 /** Description of this SA */
398 /** Reference count for this SA */
403 * Hash function for ipsec_sa_t objects
405 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
407 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
408 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
409 chunk_hash_inc(chunk_from_thing(sa
->mark
),
410 chunk_hash(chunk_from_thing(sa
->cfg
)))));
414 * Equality function for ipsec_sa_t objects
416 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
418 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
419 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
420 memeq(&sa
->mark
, &other_sa
->mark
, sizeof(mark_t
)) &&
421 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
425 * Allocate or reference an IPsec SA object
427 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
428 host_t
*src
, host_t
*dst
, mark_t mark
,
431 ipsec_sa_t
*sa
, *found
;
438 found
= this->sas
->get(this->sas
, sa
);
441 sa
->src
= src
->clone(src
);
442 sa
->dst
= dst
->clone(dst
);
443 this->sas
->put(this->sas
, sa
, sa
);
450 ref_get(&sa
->refcount
);
455 * Release and destroy an IPsec SA object
457 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
460 if (ref_put(&sa
->refcount
))
462 this->sas
->remove(this->sas
, sa
);
469 typedef struct policy_sa_t policy_sa_t
;
470 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
473 * Mapping between a policy and an IPsec SA.
476 /** Priority assigned to the policy when installed with this SA */
479 /** Type of the policy */
487 * For forward policies we also cache the traffic selectors in order to install
490 struct policy_sa_fwd_t
{
491 /** Generic interface */
494 /** Source traffic selector of this policy */
495 traffic_selector_t
*src_ts
;
497 /** Destination traffic selector of this policy */
498 traffic_selector_t
*dst_ts
;
502 * Create a policy_sa(_fwd)_t object
504 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
505 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
506 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
511 if (dir
== POLICY_FWD
)
513 policy_sa_fwd_t
*fwd
;
515 .src_ts
= src_ts
->clone(src_ts
),
516 .dst_ts
= dst_ts
->clone(dst_ts
),
518 policy
= &fwd
->generic
;
522 INIT(policy
, .priority
= 0);
525 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
530 * Destroy a policy_sa(_fwd)_t object
532 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
533 private_kernel_netlink_ipsec_t
*this)
535 if (*dir
== POLICY_FWD
)
537 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
538 fwd
->src_ts
->destroy(fwd
->src_ts
);
539 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
541 ipsec_sa_destroy(this, policy
->sa
);
545 typedef struct policy_entry_t policy_entry_t
;
548 * Installed kernel policy.
550 struct policy_entry_t
{
552 /** Direction of this policy: in, out, forward */
555 /** Parameters of installed policy */
556 struct xfrm_selector sel
;
561 /** Associated route installed for this policy */
562 route_entry_t
*route
;
564 /** List of SAs this policy is used by, ordered by priority */
565 linked_list_t
*used_by
;
569 * Destroy a policy_entry_t object
571 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
572 policy_entry_t
*policy
)
576 route_entry_destroy(policy
->route
);
580 policy
->used_by
->invoke_function(policy
->used_by
,
581 (linked_list_invoke_t
)policy_sa_destroy
,
582 &policy
->direction
, this);
583 policy
->used_by
->destroy(policy
->used_by
);
589 * Hash function for policy_entry_t objects
591 static u_int
policy_hash(policy_entry_t
*key
)
593 chunk_t chunk
= chunk_create((void*)&key
->sel
,
594 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
));
595 return chunk_hash(chunk
);
599 * Equality function for policy_entry_t objects
601 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
603 return memeq(&key
->sel
, &other_key
->sel
,
604 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
)) &&
605 key
->direction
== other_key
->direction
;
609 * Calculate the priority of a policy
611 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
612 policy_priority_t prio
)
614 u_int32_t priority
= PRIO_BASE
;
617 case POLICY_PRIORITY_FALLBACK
:
620 case POLICY_PRIORITY_ROUTED
:
623 case POLICY_PRIORITY_DEFAULT
:
626 /* calculate priority based on selector size, small size = high prio */
627 priority
-= policy
->sel
.prefixlen_s
;
628 priority
-= policy
->sel
.prefixlen_d
;
629 priority
<<= 2; /* make some room for the two flags */
630 priority
+= policy
->sel
.sport_mask
|| policy
->sel
.dport_mask ?
0 : 2;
631 priority
+= policy
->sel
.proto ?
0 : 1;
636 * Convert the general ipsec mode to the one defined in xfrm.h
638 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
643 return XFRM_MODE_TRANSPORT
;
645 return XFRM_MODE_TUNNEL
;
647 return XFRM_MODE_BEET
;
654 * Convert a host_t to a struct xfrm_address
656 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
658 chunk_t chunk
= host
->get_address(host
);
659 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
663 * Convert a struct xfrm_address to a host_t
665 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
672 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
675 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
680 return host_create_from_chunk(family
, chunk
, ntohs(port
));
684 * Convert a traffic selector address range to subnet and its mask.
686 static void ts2subnet(traffic_selector_t
* ts
,
687 xfrm_address_t
*net
, u_int8_t
*mask
)
692 ts
->to_subnet(ts
, &net_host
, mask
);
693 net_chunk
= net_host
->get_address(net_host
);
694 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
695 net_host
->destroy(net_host
);
699 * Convert a traffic selector port range to port/portmask
701 static void ts2ports(traffic_selector_t
* ts
,
702 u_int16_t
*port
, u_int16_t
*mask
)
704 /* Linux does not seem to accept complex portmasks. Only
705 * any or a specific port is allowed. We set to any, if we have
706 * a port range, or to a specific, if we have one port only.
710 from
= ts
->get_from_port(ts
);
711 to
= ts
->get_to_port(ts
);
726 * Convert a pair of traffic_selectors to an xfrm_selector
728 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
729 traffic_selector_t
*dst
)
731 struct xfrm_selector sel
;
733 memset(&sel
, 0, sizeof(sel
));
734 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
735 /* src or dest proto may be "any" (0), use more restrictive one */
736 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
737 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
738 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
739 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
740 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
748 * Convert an xfrm_selector to a src|dst traffic_selector
750 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
759 addr
= (u_char
*)&sel
->saddr
;
760 prefixlen
= sel
->prefixlen_s
;
763 port
= htons(sel
->sport
);
768 addr
= (u_char
*)&sel
->daddr
;
769 prefixlen
= sel
->prefixlen_d
;
772 port
= htons(sel
->dport
);
776 /* The Linux 2.6 kernel does not set the selector's family field,
777 * so as a kludge we additionally test the prefix length.
779 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
781 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
783 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
785 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
790 return traffic_selector_create_from_subnet(host
, prefixlen
,
797 * Process a XFRM_MSG_ACQUIRE from kernel
799 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
800 struct nlmsghdr
*hdr
)
802 struct xfrm_user_acquire
*acquire
;
805 traffic_selector_t
*src_ts
, *dst_ts
;
809 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
810 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
811 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
813 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
815 while (RTA_OK(rta
, rtasize
))
817 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
819 if (rta
->rta_type
== XFRMA_TMPL
)
821 struct xfrm_user_tmpl
* tmpl
;
822 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
824 proto
= tmpl
->id
.proto
;
826 rta
= RTA_NEXT(rta
, rtasize
);
835 /* acquire for AH/ESP only, not for IPCOMP */
838 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
839 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
841 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
846 * Process a XFRM_MSG_EXPIRE from kernel
848 static void process_expire(private_kernel_netlink_ipsec_t
*this,
849 struct nlmsghdr
*hdr
)
851 struct xfrm_user_expire
*expire
;
852 u_int32_t spi
, reqid
;
855 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
856 protocol
= expire
->state
.id
.proto
;
857 spi
= expire
->state
.id
.spi
;
858 reqid
= expire
->state
.reqid
;
860 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
862 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
864 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and "
865 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
869 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
870 spi
, expire
->hard
!= 0);
874 * Process a XFRM_MSG_MIGRATE from kernel
876 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
877 struct nlmsghdr
*hdr
)
879 struct xfrm_userpolicy_id
*policy_id
;
882 traffic_selector_t
*src_ts
, *dst_ts
;
883 host_t
*local
= NULL
, *remote
= NULL
;
884 host_t
*old_src
= NULL
, *old_dst
= NULL
;
885 host_t
*new_src
= NULL
, *new_dst
= NULL
;
889 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
890 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
891 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
893 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
895 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
896 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
897 dir
= (policy_dir_t
)policy_id
->dir
;
899 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
901 while (RTA_OK(rta
, rtasize
))
903 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
904 if (rta
->rta_type
== XFRMA_KMADDRESS
)
906 struct xfrm_user_kmaddress
*kmaddress
;
908 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
909 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
910 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
911 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
913 else if (rta
->rta_type
== XFRMA_MIGRATE
)
915 struct xfrm_user_migrate
*migrate
;
917 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
918 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
919 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
920 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
921 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
922 reqid
= migrate
->reqid
;
923 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
924 old_src
, old_dst
, new_src
, new_dst
, reqid
);
930 rta
= RTA_NEXT(rta
, rtasize
);
933 if (src_ts
&& dst_ts
&& local
&& remote
)
935 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
936 src_ts
, dst_ts
, dir
, local
, remote
);
948 * Process a XFRM_MSG_MAPPING from kernel
950 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
951 struct nlmsghdr
*hdr
)
953 struct xfrm_user_mapping
*mapping
;
954 u_int32_t spi
, reqid
;
956 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
957 spi
= mapping
->id
.spi
;
958 reqid
= mapping
->reqid
;
960 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
962 if (mapping
->id
.proto
== IPPROTO_ESP
)
965 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
969 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
976 * Receives events from kernel
978 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
981 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
982 struct sockaddr_nl addr
;
983 socklen_t addr_len
= sizeof(addr
);
987 oldstate
= thread_cancelability(TRUE
);
988 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
989 (struct sockaddr
*)&addr
, &addr_len
);
990 thread_cancelability(oldstate
);
997 /* interrupted, try again */
998 return JOB_REQUEUE_DIRECT
;
1000 /* no data ready, select again */
1001 return JOB_REQUEUE_DIRECT
;
1003 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
1005 return JOB_REQUEUE_FAIR
;
1009 if (addr
.nl_pid
!= 0)
1010 { /* not from kernel. not interested, try another one */
1011 return JOB_REQUEUE_DIRECT
;
1014 while (NLMSG_OK(hdr
, len
))
1016 switch (hdr
->nlmsg_type
)
1018 case XFRM_MSG_ACQUIRE
:
1019 process_acquire(this, hdr
);
1021 case XFRM_MSG_EXPIRE
:
1022 process_expire(this, hdr
);
1024 case XFRM_MSG_MIGRATE
:
1025 process_migrate(this, hdr
);
1027 case XFRM_MSG_MAPPING
:
1028 process_mapping(this, hdr
);
1031 DBG1(DBG_KNL
, "received unknown event from xfrm event "
1032 "socket: %d", hdr
->nlmsg_type
);
1035 hdr
= NLMSG_NEXT(hdr
, len
);
1037 return JOB_REQUEUE_DIRECT
;
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
);
1148 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1149 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1150 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
1151 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1152 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
1153 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1154 traffic_selector_t
* src_ts
, traffic_selector_t
* dst_ts
)
1156 netlink_buf_t request
;
1158 struct nlmsghdr
*hdr
;
1159 struct xfrm_usersa_info
*sa
;
1160 u_int16_t icv_size
= 64;
1161 status_t status
= FAILED
;
1163 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1164 * we are in the recursive call below */
1165 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
1167 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1168 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, mark
,
1169 tfc
, &lft
, ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
,
1170 chunk_empty
, mode
, ipcomp
, 0, FALSE
, FALSE
, inbound
, NULL
, NULL
);
1171 ipcomp
= IPCOMP_NONE
;
1172 /* use transport mode ESP SA, IPComp uses tunnel mode */
1173 mode
= MODE_TRANSPORT
;
1176 memset(&request
, 0, sizeof(request
));
1178 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u} (mark "
1179 "%u/0x%08x)", ntohl(spi
), reqid
, mark
.value
, mark
.mask
);
1181 hdr
= (struct nlmsghdr
*)request
;
1182 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1183 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1184 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1186 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1187 host2xfrm(src
, &sa
->saddr
);
1188 host2xfrm(dst
, &sa
->id
.daddr
);
1190 sa
->id
.proto
= protocol
;
1191 sa
->family
= src
->get_family(src
);
1192 sa
->mode
= mode2kernel(mode
);
1196 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1199 case MODE_TRANSPORT
:
1200 if(src_ts
&& dst_ts
)
1202 sa
->sel
= ts2selector(src_ts
, dst_ts
);
1210 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.rekey
);
1211 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.life
);
1212 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(lifetime
->packets
.rekey
);
1213 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(lifetime
->packets
.life
);
1214 /* we use lifetimes since added, not since used */
1215 sa
->lft
.soft_add_expires_seconds
= lifetime
->time
.rekey
;
1216 sa
->lft
.hard_add_expires_seconds
= lifetime
->time
.life
;
1217 sa
->lft
.soft_use_expires_seconds
= 0;
1218 sa
->lft
.hard_use_expires_seconds
= 0;
1220 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1224 case ENCR_UNDEFINED
:
1227 case ENCR_AES_CCM_ICV16
:
1228 case ENCR_AES_GCM_ICV16
:
1229 case ENCR_NULL_AUTH_AES_GMAC
:
1230 case ENCR_CAMELLIA_CCM_ICV16
:
1233 case ENCR_AES_CCM_ICV12
:
1234 case ENCR_AES_GCM_ICV12
:
1235 case ENCR_CAMELLIA_CCM_ICV12
:
1238 case ENCR_AES_CCM_ICV8
:
1239 case ENCR_AES_GCM_ICV8
:
1240 case ENCR_CAMELLIA_CCM_ICV8
:
1242 struct xfrm_algo_aead
*algo
;
1244 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1245 if (alg_name
== NULL
)
1247 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1248 encryption_algorithm_names
, enc_alg
);
1251 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1252 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1254 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
1255 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) +
1257 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1258 if (hdr
->nlmsg_len
> sizeof(request
))
1263 algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
1264 algo
->alg_key_len
= enc_key
.len
* 8;
1265 algo
->alg_icv_len
= icv_size
;
1266 strcpy(algo
->alg_name
, alg_name
);
1267 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1269 rthdr
= XFRM_RTA_NEXT(rthdr
);
1274 struct xfrm_algo
*algo
;
1276 alg_name
= lookup_algorithm(ENCRYPTION_ALGORITHM
, enc_alg
);
1277 if (alg_name
== NULL
)
1279 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1280 encryption_algorithm_names
, enc_alg
);
1283 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1284 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1286 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1287 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
1288 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1289 if (hdr
->nlmsg_len
> sizeof(request
))
1294 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1295 algo
->alg_key_len
= enc_key
.len
* 8;
1296 strcpy(algo
->alg_name
, alg_name
);
1297 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1299 rthdr
= XFRM_RTA_NEXT(rthdr
);
1303 if (int_alg
!= AUTH_UNDEFINED
)
1305 u_int trunc_len
= 0;
1307 alg_name
= lookup_algorithm(INTEGRITY_ALGORITHM
, int_alg
);
1308 if (alg_name
== NULL
)
1310 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1311 integrity_algorithm_names
, int_alg
);
1314 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1315 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1319 case AUTH_HMAC_MD5_128
:
1320 case AUTH_HMAC_SHA2_256_128
:
1323 case AUTH_HMAC_SHA1_160
:
1332 struct xfrm_algo_auth
* algo
;
1334 /* the kernel uses SHA256 with 96 bit truncation by default,
1335 * use specified truncation size supported by newer kernels.
1336 * also use this for untruncated MD5 and SHA1. */
1337 rthdr
->rta_type
= XFRMA_ALG_AUTH_TRUNC
;
1338 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_auth
) +
1341 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1342 if (hdr
->nlmsg_len
> sizeof(request
))
1347 algo
= (struct xfrm_algo_auth
*)RTA_DATA(rthdr
);
1348 algo
->alg_key_len
= int_key
.len
* 8;
1349 algo
->alg_trunc_len
= trunc_len
;
1350 strcpy(algo
->alg_name
, alg_name
);
1351 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1355 struct xfrm_algo
* algo
;
1357 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1358 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1360 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1361 if (hdr
->nlmsg_len
> sizeof(request
))
1366 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1367 algo
->alg_key_len
= int_key
.len
* 8;
1368 strcpy(algo
->alg_name
, alg_name
);
1369 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1371 rthdr
= XFRM_RTA_NEXT(rthdr
);
1374 if (ipcomp
!= IPCOMP_NONE
)
1376 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1377 alg_name
= lookup_algorithm(COMPRESSION_ALGORITHM
, ipcomp
);
1378 if (alg_name
== NULL
)
1380 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1381 ipcomp_transform_names
, ipcomp
);
1384 DBG2(DBG_KNL
, " using compression algorithm %N",
1385 ipcomp_transform_names
, ipcomp
);
1387 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1388 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1389 if (hdr
->nlmsg_len
> sizeof(request
))
1394 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1395 algo
->alg_key_len
= 0;
1396 strcpy(algo
->alg_name
, alg_name
);
1398 rthdr
= XFRM_RTA_NEXT(rthdr
);
1403 struct xfrm_encap_tmpl
*tmpl
;
1405 rthdr
->rta_type
= XFRMA_ENCAP
;
1406 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1408 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1409 if (hdr
->nlmsg_len
> sizeof(request
))
1414 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1415 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1416 tmpl
->encap_sport
= htons(src
->get_port(src
));
1417 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1418 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1419 /* encap_oa could probably be derived from the
1420 * traffic selectors [rfc4306, p39]. In the netlink kernel
1421 * implementation pluto does the same as we do here but it uses
1422 * encap_oa in the pfkey implementation.
1423 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1425 * -> does that mean that NAT-T encap doesn't work in transport mode?
1426 * No. The reason the kernel ignores NAT-OA is that it recomputes
1427 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1428 * checks it marks them "checksum ok" so OA isn't needed. */
1429 rthdr
= XFRM_RTA_NEXT(rthdr
);
1434 struct xfrm_mark
*mrk
;
1436 rthdr
->rta_type
= XFRMA_MARK
;
1437 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1439 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1440 if (hdr
->nlmsg_len
> sizeof(request
))
1445 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1446 mrk
->v
= mark
.value
;
1448 rthdr
= XFRM_RTA_NEXT(rthdr
);
1455 rthdr
->rta_type
= XFRMA_TFCPAD
;
1456 rthdr
->rta_len
= RTA_LENGTH(sizeof(u_int32_t
));
1458 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1459 if (hdr
->nlmsg_len
> sizeof(request
))
1464 tfcpad
= (u_int32_t
*)RTA_DATA(rthdr
);
1466 rthdr
= XFRM_RTA_NEXT(rthdr
);
1469 if (protocol
!= IPPROTO_COMP
)
1471 if (esn
|| this->replay_window
> DEFAULT_REPLAY_WINDOW
)
1473 /* for ESN or larger replay windows we need the new
1474 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1475 struct xfrm_replay_state_esn
*replay
;
1477 rthdr
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1478 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1479 (this->replay_window
+ 7) / 8);
1481 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1482 if (hdr
->nlmsg_len
> sizeof(request
))
1487 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(rthdr
);
1488 /* bmp_len contains number uf __u32's */
1489 replay
->bmp_len
= this->replay_bmp
;
1490 replay
->replay_window
= this->replay_window
;
1491 DBG2(DBG_KNL
, " using replay window of %u bytes",
1492 this->replay_window
);
1494 rthdr
= XFRM_RTA_NEXT(rthdr
);
1497 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1498 sa
->flags
|= XFRM_STATE_ESN
;
1503 sa
->replay_window
= DEFAULT_REPLAY_WINDOW
;
1507 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1511 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x "
1512 "(mark %u/0x%08x)", ntohl(spi
), mark
.value
, mark
.mask
);
1516 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1524 memwipe(request
, sizeof(request
));
1529 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1531 * Allocates into one the replay state structure we get from the kernel.
1533 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1534 u_int32_t spi
, u_int8_t protocol
, host_t
*dst
,
1535 struct xfrm_replay_state_esn
**replay_esn
,
1536 struct xfrm_replay_state
**replay
)
1538 netlink_buf_t request
;
1539 struct nlmsghdr
*hdr
, *out
= NULL
;
1540 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1545 memset(&request
, 0, sizeof(request
));
1547 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1550 hdr
= (struct nlmsghdr
*)request
;
1551 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1552 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1553 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1555 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1556 aevent_id
->flags
= XFRM_AE_RVAL
;
1558 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1559 aevent_id
->sa_id
.spi
= spi
;
1560 aevent_id
->sa_id
.proto
= protocol
;
1561 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1563 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1566 while (NLMSG_OK(hdr
, len
))
1568 switch (hdr
->nlmsg_type
)
1570 case XFRM_MSG_NEWAE
:
1572 out_aevent
= NLMSG_DATA(hdr
);
1577 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1578 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1579 "failed: %s (%d)", strerror(-err
->error
),
1584 hdr
= NLMSG_NEXT(hdr
, len
);
1595 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1596 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1597 while (RTA_OK(rta
, rtasize
))
1599 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1600 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1602 *replay
= malloc(RTA_PAYLOAD(rta
));
1603 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1606 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1607 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
) + this->replay_bmp
)
1609 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1610 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1613 rta
= RTA_NEXT(rta
, rtasize
);
1619 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1620 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1621 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1623 netlink_buf_t request
;
1624 struct nlmsghdr
*out
= NULL
, *hdr
;
1625 struct xfrm_usersa_id
*sa_id
;
1626 struct xfrm_usersa_info
*sa
= NULL
;
1627 status_t status
= FAILED
;
1630 memset(&request
, 0, sizeof(request
));
1632 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%08x)",
1633 ntohl(spi
), mark
.value
, mark
.mask
);
1635 hdr
= (struct nlmsghdr
*)request
;
1636 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1637 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1638 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1640 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1641 host2xfrm(dst
, &sa_id
->daddr
);
1643 sa_id
->proto
= protocol
;
1644 sa_id
->family
= dst
->get_family(dst
);
1648 struct xfrm_mark
*mrk
;
1649 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1651 rthdr
->rta_type
= XFRMA_MARK
;
1652 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1653 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1654 if (hdr
->nlmsg_len
> sizeof(request
))
1659 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1660 mrk
->v
= mark
.value
;
1664 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1667 while (NLMSG_OK(hdr
, len
))
1669 switch (hdr
->nlmsg_type
)
1671 case XFRM_MSG_NEWSA
:
1673 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1678 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1682 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1683 "(mark %u/0x%08x) failed: %s (%d)",
1684 ntohl(spi
), mark
.value
, mark
.mask
,
1685 strerror(-err
->error
), -err
->error
);
1689 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1690 "failed: %s (%d)", ntohl(spi
),
1691 strerror(-err
->error
), -err
->error
);
1696 hdr
= NLMSG_NEXT(hdr
, len
);
1707 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1711 *bytes
= sa
->curlft
.bytes
;
1719 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1720 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1721 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1723 netlink_buf_t request
;
1724 struct nlmsghdr
*hdr
;
1725 struct xfrm_usersa_id
*sa_id
;
1727 /* if IPComp was used, we first delete the additional IPComp SA */
1730 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1733 memset(&request
, 0, sizeof(request
));
1735 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%08x)",
1736 ntohl(spi
), mark
.value
, mark
.mask
);
1738 hdr
= (struct nlmsghdr
*)request
;
1739 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1740 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1741 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1743 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1744 host2xfrm(dst
, &sa_id
->daddr
);
1746 sa_id
->proto
= protocol
;
1747 sa_id
->family
= dst
->get_family(dst
);
1751 struct xfrm_mark
*mrk
;
1752 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1754 rthdr
->rta_type
= XFRMA_MARK
;
1755 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1756 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1757 if (hdr
->nlmsg_len
> sizeof(request
))
1762 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1763 mrk
->v
= mark
.value
;
1767 switch (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
))
1770 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%08x)",
1771 ntohl(spi
), mark
.value
, mark
.mask
);
1778 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1779 "(mark %u/0x%08x)", ntohl(spi
), mark
.value
, mark
.mask
);
1783 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1790 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1791 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1792 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1793 bool old_encap
, bool new_encap
, mark_t mark
)
1795 netlink_buf_t request
;
1797 struct nlmsghdr
*hdr
, *out
= NULL
;
1798 struct xfrm_usersa_id
*sa_id
;
1799 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1803 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1804 struct xfrm_replay_state
*replay
= NULL
;
1805 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1806 status_t status
= FAILED
;
1808 /* if IPComp is used, we first update the IPComp SA */
1811 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1812 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1815 memset(&request
, 0, sizeof(request
));
1817 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1819 /* query the existing SA first */
1820 hdr
= (struct nlmsghdr
*)request
;
1821 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1822 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1823 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1825 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1826 host2xfrm(dst
, &sa_id
->daddr
);
1828 sa_id
->proto
= protocol
;
1829 sa_id
->family
= dst
->get_family(dst
);
1831 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1834 while (NLMSG_OK(hdr
, len
))
1836 switch (hdr
->nlmsg_type
)
1838 case XFRM_MSG_NEWSA
:
1840 out_sa
= NLMSG_DATA(hdr
);
1845 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1846 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1847 strerror(-err
->error
), -err
->error
);
1851 hdr
= NLMSG_NEXT(hdr
, len
);
1861 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1865 get_replay_state(this, spi
, protocol
, dst
, &replay_esn
, &replay
);
1867 /* delete the old SA (without affecting the IPComp SA) */
1868 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1870 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1875 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1876 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1877 /* copy over the SA from out to request */
1878 hdr
= (struct nlmsghdr
*)request
;
1879 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1880 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1881 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1882 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1883 sa
= NLMSG_DATA(hdr
);
1884 sa
->family
= new_dst
->get_family(new_dst
);
1886 if (!src
->ip_equals(src
, new_src
))
1888 host2xfrm(new_src
, &sa
->saddr
);
1890 if (!dst
->ip_equals(dst
, new_dst
))
1892 host2xfrm(new_dst
, &sa
->id
.daddr
);
1895 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1896 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1897 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1898 while(RTA_OK(rta
, rtasize
))
1900 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1901 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1903 if (rta
->rta_type
== XFRMA_ENCAP
)
1904 { /* update encap tmpl */
1905 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1906 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1907 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1909 memcpy(pos
, rta
, rta
->rta_len
);
1910 pos
+= RTA_ALIGN(rta
->rta_len
);
1911 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1913 rta
= RTA_NEXT(rta
, rtasize
);
1916 rta
= (struct rtattr
*)pos
;
1917 if (tmpl
== NULL
&& new_encap
)
1918 { /* add tmpl if we are enabling it */
1919 rta
->rta_type
= XFRMA_ENCAP
;
1920 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1922 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1923 if (hdr
->nlmsg_len
> sizeof(request
))
1928 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1929 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1930 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1931 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1932 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1934 rta
= XFRM_RTA_NEXT(rta
);
1939 rta
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1940 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1943 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1944 if (hdr
->nlmsg_len
> sizeof(request
))
1948 memcpy(RTA_DATA(rta
), replay_esn
,
1949 sizeof(struct xfrm_replay_state_esn
) + this->replay_bmp
);
1951 rta
= XFRM_RTA_NEXT(rta
);
1955 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1956 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1958 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1959 if (hdr
->nlmsg_len
> sizeof(request
))
1963 memcpy(RTA_DATA(rta
), replay
, sizeof(replay
));
1965 rta
= XFRM_RTA_NEXT(rta
);
1969 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1970 "with SPI %.8x", ntohl(spi
));
1973 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1975 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1984 memwipe(request
, sizeof(request
));
1990 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1991 private_kernel_netlink_ipsec_t
*this)
1993 netlink_buf_t request
;
1994 struct nlmsghdr
*hdr
;
1995 struct xfrm_usersa_flush
*flush
;
1997 memset(&request
, 0, sizeof(request
));
1999 DBG2(DBG_KNL
, "flushing all SAD entries");
2001 hdr
= (struct nlmsghdr
*)request
;
2002 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2003 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
2004 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
2006 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
2007 flush
->proto
= IPSEC_PROTO_ANY
;
2009 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2011 DBG1(DBG_KNL
, "unable to flush SAD entries");
2018 * Add or update a policy in the kernel.
2020 * Note: The mutex has to be locked when entering this function
2021 * and is unlocked here in any case.
2023 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
2024 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2026 netlink_buf_t request
;
2027 policy_entry_t clone
;
2028 ipsec_sa_t
*ipsec
= mapping
->sa
;
2029 struct xfrm_userpolicy_info
*policy_info
;
2030 struct nlmsghdr
*hdr
;
2033 /* clone the policy so we are able to check it out again later */
2034 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2036 memset(&request
, 0, sizeof(request
));
2037 hdr
= (struct nlmsghdr
*)request
;
2038 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2039 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2040 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2042 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2043 policy_info
->sel
= policy
->sel
;
2044 policy_info
->dir
= policy
->direction
;
2046 /* calculate priority based on selector size, small size = high prio */
2047 policy_info
->priority
= mapping
->priority
;
2048 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2049 : XFRM_POLICY_BLOCK
;
2050 policy_info
->share
= XFRM_SHARE_ANY
;
2052 /* policies don't expire */
2053 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2054 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2055 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2056 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2057 policy_info
->lft
.soft_add_expires_seconds
= 0;
2058 policy_info
->lft
.hard_add_expires_seconds
= 0;
2059 policy_info
->lft
.soft_use_expires_seconds
= 0;
2060 policy_info
->lft
.hard_use_expires_seconds
= 0;
2062 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2064 if (mapping
->type
== POLICY_IPSEC
)
2066 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2071 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2072 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2073 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2075 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2077 rthdr
->rta_type
= XFRMA_TMPL
;
2078 rthdr
->rta_len
= 0; /* actual length is set below */
2080 for (i
= 0; i
< countof(protos
); i
++)
2087 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2088 hdr
->nlmsg_len
+= RTA_ALIGN(RTA_LENGTH(sizeof(struct xfrm_user_tmpl
)));
2089 if (hdr
->nlmsg_len
> sizeof(request
))
2091 this->mutex
->unlock(this->mutex
);
2095 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2096 tmpl
->id
.proto
= protos
[i
].proto
;
2097 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2098 tmpl
->mode
= mode2kernel(proto_mode
);
2099 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2100 policy
->direction
!= POLICY_OUT
;
2101 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2103 if (proto_mode
== MODE_TUNNEL
)
2104 { /* only for tunnel mode */
2105 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2106 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2111 /* use transport mode for other SAs */
2112 proto_mode
= MODE_TRANSPORT
;
2115 rthdr
= XFRM_RTA_NEXT(rthdr
);
2118 if (ipsec
->mark
.value
)
2120 struct xfrm_mark
*mrk
;
2122 rthdr
->rta_type
= XFRMA_MARK
;
2123 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2125 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2126 if (hdr
->nlmsg_len
> sizeof(request
))
2128 this->mutex
->unlock(this->mutex
);
2132 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2133 mrk
->v
= ipsec
->mark
.value
;
2134 mrk
->m
= ipsec
->mark
.mask
;
2136 this->mutex
->unlock(this->mutex
);
2138 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2143 /* find the policy again */
2144 this->mutex
->lock(this->mutex
);
2145 policy
= this->policies
->get(this->policies
, &clone
);
2147 policy
->used_by
->find_first(policy
->used_by
,
2148 NULL
, (void**)&mapping
) != SUCCESS
)
2149 { /* policy or mapping is already gone, ignore */
2150 this->mutex
->unlock(this->mutex
);
2154 /* install a route, if:
2155 * - this is a forward policy (to just get one for each child)
2156 * - we are in tunnel/BEET mode
2157 * - routing is not disabled via strongswan.conf
2159 if (policy
->direction
== POLICY_FWD
&&
2160 ipsec
->cfg
.mode
!= MODE_TRANSPORT
&& this->install_routes
)
2162 route_entry_t
*route
= malloc_thing(route_entry_t
);
2163 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)mapping
;
2165 if (hydra
->kernel_interface
->get_address_by_ts(hydra
->kernel_interface
,
2166 fwd
->dst_ts
, &route
->src_ip
) == SUCCESS
)
2168 /* get the nexthop to src (src as we are in POLICY_FWD) */
2169 route
->gateway
= hydra
->kernel_interface
->get_nexthop(
2170 hydra
->kernel_interface
, ipsec
->src
);
2171 /* install route via outgoing interface */
2172 route
->if_name
= hydra
->kernel_interface
->get_interface(
2173 hydra
->kernel_interface
, ipsec
->dst
);
2174 route
->dst_net
= chunk_alloc(policy
->sel
.family
== AF_INET ?
4 : 16);
2175 memcpy(route
->dst_net
.ptr
, &policy
->sel
.saddr
, route
->dst_net
.len
);
2176 route
->prefixlen
= policy
->sel
.prefixlen_s
;
2178 if (!route
->if_name
)
2180 this->mutex
->unlock(this->mutex
);
2181 route_entry_destroy(route
);
2187 route_entry_t
*old
= policy
->route
;
2188 if (route_entry_equals(old
, route
))
2190 this->mutex
->unlock(this->mutex
);
2191 route_entry_destroy(route
);
2194 /* uninstall previously installed route */
2195 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2196 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2197 old
->src_ip
, old
->if_name
) != SUCCESS
)
2199 DBG1(DBG_KNL
, "error uninstalling route installed with "
2200 "policy %R === %R %N", fwd
->src_ts
,
2201 fwd
->dst_ts
, policy_dir_names
,
2204 route_entry_destroy(old
);
2205 policy
->route
= NULL
;
2208 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2209 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2210 switch (hydra
->kernel_interface
->add_route(
2211 hydra
->kernel_interface
, route
->dst_net
,
2212 route
->prefixlen
, route
->gateway
,
2213 route
->src_ip
, route
->if_name
))
2216 DBG1(DBG_KNL
, "unable to install source route for %H",
2220 /* route exists, do not uninstall */
2221 route_entry_destroy(route
);
2224 /* cache the installed route */
2225 policy
->route
= route
;
2234 this->mutex
->unlock(this->mutex
);
2238 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2239 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2240 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2241 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2242 mark_t mark
, policy_priority_t priority
)
2244 policy_entry_t
*policy
, *current
;
2245 policy_sa_t
*assigned_sa
, *current_sa
;
2246 enumerator_t
*enumerator
;
2247 bool found
= FALSE
, update
= TRUE
;
2249 /* create a policy */
2251 .sel
= ts2selector(src_ts
, dst_ts
),
2252 .mark
= mark
.value
& mark
.mask
,
2253 .direction
= direction
,
2256 /* find the policy, which matches EXACTLY */
2257 this->mutex
->lock(this->mutex
);
2258 current
= this->policies
->get(this->policies
, policy
);
2261 /* use existing policy */
2262 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%08x) "
2263 "already exists, increasing refcount",
2264 src_ts
, dst_ts
, policy_dir_names
, direction
,
2265 mark
.value
, mark
.mask
);
2266 policy_entry_destroy(this, policy
);
2271 { /* use the new one, if we have no such policy */
2272 policy
->used_by
= linked_list_create();
2273 this->policies
->put(this->policies
, policy
, policy
);
2276 /* cache the assigned IPsec SA */
2277 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2279 assigned_sa
->priority
= get_priority(policy
, priority
);
2281 if (this->policy_history
)
2282 { /* insert the SA according to its priority */
2283 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2284 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2286 if (current_sa
->priority
>= assigned_sa
->priority
)
2292 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2294 enumerator
->destroy(enumerator
);
2297 { /* simply insert it last and only update if it is not installed yet */
2298 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2303 { /* we don't update the policy if the priority is lower than that of
2304 * the currently installed one */
2305 this->mutex
->unlock(this->mutex
);
2309 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%08x)",
2310 found ?
"updating" : "adding", src_ts
, dst_ts
,
2311 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2313 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2315 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2316 found ?
"update" : "add", src_ts
, dst_ts
,
2317 policy_dir_names
, direction
);
2323 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2324 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2325 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2326 u_int32_t
*use_time
)
2328 netlink_buf_t request
;
2329 struct nlmsghdr
*out
= NULL
, *hdr
;
2330 struct xfrm_userpolicy_id
*policy_id
;
2331 struct xfrm_userpolicy_info
*policy
= NULL
;
2334 memset(&request
, 0, sizeof(request
));
2336 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%08x)",
2337 src_ts
, dst_ts
, policy_dir_names
, direction
,
2338 mark
.value
, mark
.mask
);
2340 hdr
= (struct nlmsghdr
*)request
;
2341 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2342 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2343 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2345 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2346 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2347 policy_id
->dir
= direction
;
2351 struct xfrm_mark
*mrk
;
2352 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2354 rthdr
->rta_type
= XFRMA_MARK
;
2355 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2357 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2358 if (hdr
->nlmsg_len
> sizeof(request
))
2363 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2364 mrk
->v
= mark
.value
;
2368 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2371 while (NLMSG_OK(hdr
, len
))
2373 switch (hdr
->nlmsg_type
)
2375 case XFRM_MSG_NEWPOLICY
:
2377 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2382 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2383 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2384 strerror(-err
->error
), -err
->error
);
2388 hdr
= NLMSG_NEXT(hdr
, len
);
2399 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2400 policy_dir_names
, direction
);
2405 if (policy
->curlft
.use_time
)
2407 /* we need the monotonic time, but the kernel returns system time. */
2408 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2419 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2420 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2421 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2422 mark_t mark
, policy_priority_t prio
)
2424 policy_entry_t
*current
, policy
;
2425 enumerator_t
*enumerator
;
2426 policy_sa_t
*mapping
;
2427 netlink_buf_t request
;
2428 struct nlmsghdr
*hdr
;
2429 struct xfrm_userpolicy_id
*policy_id
;
2430 bool is_installed
= TRUE
;
2433 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x)",
2434 src_ts
, dst_ts
, policy_dir_names
, direction
,
2435 mark
.value
, mark
.mask
);
2437 /* create a policy */
2438 memset(&policy
, 0, sizeof(policy_entry_t
));
2439 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2440 policy
.mark
= mark
.value
& mark
.mask
;
2441 policy
.direction
= direction
;
2443 /* find the policy */
2444 this->mutex
->lock(this->mutex
);
2445 current
= this->policies
->get(this->policies
, &policy
);
2450 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%08x) "
2451 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2452 direction
, mark
.value
, mark
.mask
);
2456 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2457 src_ts
, dst_ts
, policy_dir_names
, direction
);
2459 this->mutex
->unlock(this->mutex
);
2463 if (this->policy_history
)
2464 { /* remove mapping to SA by reqid and priority */
2465 priority
= get_priority(current
, prio
);
2466 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2467 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2469 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2470 priority
== mapping
->priority
)
2472 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2473 policy_sa_destroy(mapping
, &direction
, this);
2476 is_installed
= FALSE
;
2478 enumerator
->destroy(enumerator
);
2481 { /* remove one of the SAs but don't update the policy */
2482 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2483 policy_sa_destroy(mapping
, &direction
, this);
2484 is_installed
= FALSE
;
2487 if (current
->used_by
->get_count(current
->used_by
) > 0)
2488 { /* policy is used by more SAs, keep in kernel */
2489 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2491 { /* no need to update as the policy was not installed for this SA */
2492 this->mutex
->unlock(this->mutex
);
2496 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%08x)",
2497 src_ts
, dst_ts
, policy_dir_names
, direction
,
2498 mark
.value
, mark
.mask
);
2500 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2501 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2503 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2504 src_ts
, dst_ts
, policy_dir_names
, direction
);
2510 memset(&request
, 0, sizeof(request
));
2512 hdr
= (struct nlmsghdr
*)request
;
2513 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2514 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2515 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2517 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2518 policy_id
->sel
= current
->sel
;
2519 policy_id
->dir
= direction
;
2523 struct xfrm_mark
*mrk
;
2524 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2526 rthdr
->rta_type
= XFRMA_MARK
;
2527 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2528 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2529 if (hdr
->nlmsg_len
> sizeof(request
))
2531 this->mutex
->unlock(this->mutex
);
2535 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2536 mrk
->v
= mark
.value
;
2542 route_entry_t
*route
= current
->route
;
2543 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2544 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2545 route
->src_ip
, route
->if_name
) != SUCCESS
)
2547 DBG1(DBG_KNL
, "error uninstalling route installed with "
2548 "policy %R === %R %N", src_ts
, dst_ts
,
2549 policy_dir_names
, direction
);
2553 this->policies
->remove(this->policies
, current
);
2554 policy_entry_destroy(this, current
);
2555 this->mutex
->unlock(this->mutex
);
2557 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2561 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2562 "(mark %u/0x%08x)", src_ts
, dst_ts
, policy_dir_names
,
2563 direction
, mark
.value
, mark
.mask
);
2567 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2568 src_ts
, dst_ts
, policy_dir_names
, direction
);
2575 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2576 private_kernel_netlink_ipsec_t
*this)
2578 netlink_buf_t request
;
2579 struct nlmsghdr
*hdr
;
2581 memset(&request
, 0, sizeof(request
));
2583 DBG2(DBG_KNL
, "flushing all policies from SPD");
2585 hdr
= (struct nlmsghdr
*)request
;
2586 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2587 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2588 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2590 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2591 * to main or sub policies (default is main) */
2593 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2595 DBG1(DBG_KNL
, "unable to flush SPD entries");
2602 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2603 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2605 struct xfrm_userpolicy_info policy
;
2606 u_int sol
, ipsec_policy
;
2612 ipsec_policy
= IP_XFRM_POLICY
;
2616 ipsec_policy
= IPV6_XFRM_POLICY
;
2622 memset(&policy
, 0, sizeof(policy
));
2623 policy
.action
= XFRM_POLICY_ALLOW
;
2624 policy
.sel
.family
= family
;
2626 policy
.dir
= XFRM_POLICY_OUT
;
2627 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2629 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2633 policy
.dir
= XFRM_POLICY_IN
;
2634 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2636 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2643 METHOD(kernel_ipsec_t
, enable_udp_decap
, bool,
2644 private_kernel_netlink_ipsec_t
*this, int fd
, int family
, u_int16_t port
)
2646 int type
= UDP_ENCAP_ESPINUDP
;
2648 if (setsockopt(fd
, SOL_UDP
, UDP_ENCAP
, &type
, sizeof(type
)) < 0)
2650 DBG1(DBG_KNL
, "unable to set UDP_ENCAP: %s", strerror(errno
));
2656 METHOD(kernel_ipsec_t
, destroy
, void,
2657 private_kernel_netlink_ipsec_t
*this)
2659 enumerator_t
*enumerator
;
2660 policy_entry_t
*policy
;
2662 if (this->socket_xfrm_events
> 0)
2664 close(this->socket_xfrm_events
);
2666 DESTROY_IF(this->socket_xfrm
);
2667 enumerator
= this->policies
->create_enumerator(this->policies
);
2668 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2670 policy_entry_destroy(this, policy
);
2672 enumerator
->destroy(enumerator
);
2673 this->policies
->destroy(this->policies
);
2674 this->sas
->destroy(this->sas
);
2675 this->mutex
->destroy(this->mutex
);
2680 * Described in header.
2682 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2684 private_kernel_netlink_ipsec_t
*this;
2685 bool register_for_events
= TRUE
;
2691 .get_spi
= _get_spi
,
2692 .get_cpi
= _get_cpi
,
2694 .update_sa
= _update_sa
,
2695 .query_sa
= _query_sa
,
2697 .flush_sas
= _flush_sas
,
2698 .add_policy
= _add_policy
,
2699 .query_policy
= _query_policy
,
2700 .del_policy
= _del_policy
,
2701 .flush_policies
= _flush_policies
,
2702 .bypass_socket
= _bypass_socket
,
2703 .enable_udp_decap
= _enable_udp_decap
,
2704 .destroy
= _destroy
,
2707 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2708 (hashtable_equals_t
)policy_equals
, 32),
2709 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2710 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2711 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2712 .policy_history
= TRUE
,
2713 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2714 "%s.install_routes", TRUE
, hydra
->daemon
),
2715 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2716 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2719 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2720 (sizeof(u_int32_t
) * 8);
2722 if (streq(hydra
->daemon
, "pluto"))
2723 { /* no routes for pluto, they are installed via updown script */
2724 this->install_routes
= FALSE
;
2725 /* no policy history for pluto */
2726 this->policy_history
= FALSE
;
2728 else if (streq(hydra
->daemon
, "starter"))
2729 { /* starter has no threads, so we do not register for kernel events */
2730 register_for_events
= FALSE
;
2733 /* disable lifetimes for allocated SPIs in kernel */
2734 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2737 ignore_result(write(fd
, "165", 3));
2741 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2742 if (!this->socket_xfrm
)
2748 if (register_for_events
)
2750 struct sockaddr_nl addr
;
2752 memset(&addr
, 0, sizeof(addr
));
2753 addr
.nl_family
= AF_NETLINK
;
2755 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2756 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2757 if (this->socket_xfrm_events
<= 0)
2759 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2763 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2764 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2765 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2767 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2771 lib
->processor
->queue_job(lib
->processor
,
2772 (job_t
*)callback_job_create_with_prio(
2773 (callback_job_cb_t
)receive_events
, this, NULL
,
2774 (callback_job_cancel_t
)return_false
, JOB_PRIO_CRITICAL
));
2777 return &this->public;