2 * Copyright (C) 2006-2011 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_SHA1_96
, "sha1" },
206 {AUTH_HMAC_SHA2_256_96
, "sha256" },
207 {AUTH_HMAC_SHA2_256_128
, "hmac(sha256)" },
208 {AUTH_HMAC_SHA2_384_192
, "hmac(sha384)" },
209 {AUTH_HMAC_SHA2_512_256
, "hmac(sha512)" },
210 /* {AUTH_DES_MAC, "***" }, */
211 /* {AUTH_KPDK_MD5, "***" }, */
212 {AUTH_AES_XCBC_96
, "xcbc(aes)" },
217 * Algorithms for IPComp
219 static kernel_algorithm_t compression_algs
[] = {
220 /* {IPCOMP_OUI, "***" }, */
221 {IPCOMP_DEFLATE
, "deflate" },
222 {IPCOMP_LZS
, "lzs" },
223 {IPCOMP_LZJH
, "lzjh" },
228 * Look up a kernel algorithm name and its key size
230 static char* lookup_algorithm(kernel_algorithm_t
*list
, int ikev2
)
232 while (list
->ikev2
!= END_OF_LIST
)
234 if (list
->ikev2
== ikev2
)
243 typedef struct private_kernel_netlink_ipsec_t private_kernel_netlink_ipsec_t
;
246 * Private variables and functions of kernel_netlink class.
248 struct private_kernel_netlink_ipsec_t
{
250 * Public part of the kernel_netlink_t object
252 kernel_netlink_ipsec_t
public;
255 * Mutex to lock access to installed policies
260 * Hash table of installed policies (policy_entry_t)
262 hashtable_t
*policies
;
265 * Hash table of IPsec SAs using policies (ipsec_sa_t)
270 * Job receiving netlink events
275 * Netlink xfrm socket (IPsec)
277 netlink_socket_t
*socket_xfrm
;
280 * Netlink xfrm socket to receive acquire and expire events
282 int socket_xfrm_events
;
285 * Whether to install routes along policies
290 * Whether to track the history of a policy
295 * Size of the replay window, in packets
297 u_int32_t replay_window
;
300 * Size of the replay window bitmap, in bytes
302 u_int32_t replay_bmp
;
305 typedef struct route_entry_t route_entry_t
;
308 * Installed routing entry
310 struct route_entry_t
{
311 /** Name of the interface the route is bound to */
314 /** Source ip of the route */
317 /** Gateway for this route */
320 /** Destination net */
323 /** Destination net prefixlen */
328 * Destroy a route_entry_t object
330 static void route_entry_destroy(route_entry_t
*this)
333 this->src_ip
->destroy(this->src_ip
);
334 DESTROY_IF(this->gateway
);
335 chunk_free(&this->dst_net
);
340 * Compare two route_entry_t objects
342 static bool route_entry_equals(route_entry_t
*a
, route_entry_t
*b
)
344 return a
->if_name
&& b
->if_name
&& streq(a
->if_name
, b
->if_name
) &&
345 a
->src_ip
->equals(a
->src_ip
, b
->src_ip
) &&
346 a
->gateway
->equals(a
->gateway
, b
->gateway
) &&
347 chunk_equals(a
->dst_net
, b
->dst_net
) && a
->prefixlen
== b
->prefixlen
;
350 typedef struct ipsec_sa_t ipsec_sa_t
;
353 * IPsec SA assigned to a policy.
356 /** Source address of this SA */
359 /** Destination address of this SA */
365 /** Description of this SA */
368 /** Reference count for this SA */
373 * Hash function for ipsec_sa_t objects
375 static u_int
ipsec_sa_hash(ipsec_sa_t
*sa
)
377 return chunk_hash_inc(sa
->src
->get_address(sa
->src
),
378 chunk_hash_inc(sa
->dst
->get_address(sa
->dst
),
379 chunk_hash_inc(chunk_from_thing(sa
->mark
),
380 chunk_hash(chunk_from_thing(sa
->cfg
)))));
384 * Equality function for ipsec_sa_t objects
386 static bool ipsec_sa_equals(ipsec_sa_t
*sa
, ipsec_sa_t
*other_sa
)
388 return sa
->src
->ip_equals(sa
->src
, other_sa
->src
) &&
389 sa
->dst
->ip_equals(sa
->dst
, other_sa
->dst
) &&
390 memeq(&sa
->mark
, &other_sa
->mark
, sizeof(mark_t
)) &&
391 memeq(&sa
->cfg
, &other_sa
->cfg
, sizeof(ipsec_sa_cfg_t
));
395 * Allocate or reference an IPsec SA object
397 static ipsec_sa_t
*ipsec_sa_create(private_kernel_netlink_ipsec_t
*this,
398 host_t
*src
, host_t
*dst
, mark_t mark
,
401 ipsec_sa_t
*sa
, *found
;
408 found
= this->sas
->get(this->sas
, sa
);
411 sa
->src
= src
->clone(src
);
412 sa
->dst
= dst
->clone(dst
);
413 this->sas
->put(this->sas
, sa
, sa
);
420 ref_get(&sa
->refcount
);
425 * Release and destroy an IPsec SA object
427 static void ipsec_sa_destroy(private_kernel_netlink_ipsec_t
*this,
430 if (ref_put(&sa
->refcount
))
432 this->sas
->remove(this->sas
, sa
);
439 typedef struct policy_sa_t policy_sa_t
;
440 typedef struct policy_sa_fwd_t policy_sa_fwd_t
;
443 * Mapping between a policy and an IPsec SA.
446 /** Priority assigned to the policy when installed with this SA */
449 /** Type of the policy */
457 * For forward policies we also cache the traffic selectors in order to install
460 struct policy_sa_fwd_t
{
461 /** Generic interface */
464 /** Source traffic selector of this policy */
465 traffic_selector_t
*src_ts
;
467 /** Destination traffic selector of this policy */
468 traffic_selector_t
*dst_ts
;
472 * Create a policy_sa(_fwd)_t object
474 static policy_sa_t
*policy_sa_create(private_kernel_netlink_ipsec_t
*this,
475 policy_dir_t dir
, policy_type_t type
, host_t
*src
, host_t
*dst
,
476 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
, mark_t mark
,
481 if (dir
== POLICY_FWD
)
483 policy_sa_fwd_t
*fwd
;
485 .src_ts
= src_ts
->clone(src_ts
),
486 .dst_ts
= dst_ts
->clone(dst_ts
),
488 policy
= &fwd
->generic
;
492 INIT(policy
, .priority
= 0);
495 policy
->sa
= ipsec_sa_create(this, src
, dst
, mark
, cfg
);
500 * Destroy a policy_sa(_fwd)_t object
502 static void policy_sa_destroy(policy_sa_t
*policy
, policy_dir_t
*dir
,
503 private_kernel_netlink_ipsec_t
*this)
505 if (*dir
== POLICY_FWD
)
507 policy_sa_fwd_t
*fwd
= (policy_sa_fwd_t
*)policy
;
508 fwd
->src_ts
->destroy(fwd
->src_ts
);
509 fwd
->dst_ts
->destroy(fwd
->dst_ts
);
511 ipsec_sa_destroy(this, policy
->sa
);
515 typedef struct policy_entry_t policy_entry_t
;
518 * Installed kernel policy.
520 struct policy_entry_t
{
522 /** Direction of this policy: in, out, forward */
525 /** Parameters of installed policy */
526 struct xfrm_selector sel
;
531 /** Associated route installed for this policy */
532 route_entry_t
*route
;
534 /** List of SAs this policy is used by, ordered by priority */
535 linked_list_t
*used_by
;
539 * Destroy a policy_entry_t object
541 static void policy_entry_destroy(private_kernel_netlink_ipsec_t
*this,
542 policy_entry_t
*policy
)
546 route_entry_destroy(policy
->route
);
550 policy
->used_by
->invoke_function(policy
->used_by
,
551 (linked_list_invoke_t
)policy_sa_destroy
,
552 &policy
->direction
, this);
553 policy
->used_by
->destroy(policy
->used_by
);
559 * Hash function for policy_entry_t objects
561 static u_int
policy_hash(policy_entry_t
*key
)
563 chunk_t chunk
= chunk_create((void*)&key
->sel
,
564 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
));
565 return chunk_hash(chunk
);
569 * Equality function for policy_entry_t objects
571 static bool policy_equals(policy_entry_t
*key
, policy_entry_t
*other_key
)
573 return memeq(&key
->sel
, &other_key
->sel
,
574 sizeof(struct xfrm_selector
) + sizeof(u_int32_t
)) &&
575 key
->direction
== other_key
->direction
;
579 * Calculate the priority of a policy
581 static inline u_int32_t
get_priority(policy_entry_t
*policy
,
582 policy_priority_t prio
)
584 u_int32_t priority
= PRIO_BASE
;
587 case POLICY_PRIORITY_FALLBACK
:
590 case POLICY_PRIORITY_ROUTED
:
593 case POLICY_PRIORITY_DEFAULT
:
596 /* calculate priority based on selector size, small size = high prio */
597 priority
-= policy
->sel
.prefixlen_s
;
598 priority
-= policy
->sel
.prefixlen_d
;
599 priority
<<= 2; /* make some room for the two flags */
600 priority
+= policy
->sel
.sport_mask
|| policy
->sel
.dport_mask ?
0 : 2;
601 priority
+= policy
->sel
.proto ?
0 : 1;
606 * Convert the general ipsec mode to the one defined in xfrm.h
608 static u_int8_t
mode2kernel(ipsec_mode_t mode
)
613 return XFRM_MODE_TRANSPORT
;
615 return XFRM_MODE_TUNNEL
;
617 return XFRM_MODE_BEET
;
624 * Convert a host_t to a struct xfrm_address
626 static void host2xfrm(host_t
*host
, xfrm_address_t
*xfrm
)
628 chunk_t chunk
= host
->get_address(host
);
629 memcpy(xfrm
, chunk
.ptr
, min(chunk
.len
, sizeof(xfrm_address_t
)));
633 * Convert a struct xfrm_address to a host_t
635 static host_t
* xfrm2host(int family
, xfrm_address_t
*xfrm
, u_int16_t port
)
642 chunk
= chunk_create((u_char
*)&xfrm
->a4
, sizeof(xfrm
->a4
));
645 chunk
= chunk_create((u_char
*)&xfrm
->a6
, sizeof(xfrm
->a6
));
650 return host_create_from_chunk(family
, chunk
, ntohs(port
));
654 * Convert a traffic selector address range to subnet and its mask.
656 static void ts2subnet(traffic_selector_t
* ts
,
657 xfrm_address_t
*net
, u_int8_t
*mask
)
662 ts
->to_subnet(ts
, &net_host
, mask
);
663 net_chunk
= net_host
->get_address(net_host
);
664 memcpy(net
, net_chunk
.ptr
, net_chunk
.len
);
665 net_host
->destroy(net_host
);
669 * Convert a traffic selector port range to port/portmask
671 static void ts2ports(traffic_selector_t
* ts
,
672 u_int16_t
*port
, u_int16_t
*mask
)
674 /* Linux does not seem to accept complex portmasks. Only
675 * any or a specific port is allowed. We set to any, if we have
676 * a port range, or to a specific, if we have one port only.
680 from
= ts
->get_from_port(ts
);
681 to
= ts
->get_to_port(ts
);
696 * Convert a pair of traffic_selectors to an xfrm_selector
698 static struct xfrm_selector
ts2selector(traffic_selector_t
*src
,
699 traffic_selector_t
*dst
)
701 struct xfrm_selector sel
;
703 memset(&sel
, 0, sizeof(sel
));
704 sel
.family
= (src
->get_type(src
) == TS_IPV4_ADDR_RANGE
) ? AF_INET
: AF_INET6
;
705 /* src or dest proto may be "any" (0), use more restrictive one */
706 sel
.proto
= max(src
->get_protocol(src
), dst
->get_protocol(dst
));
707 ts2subnet(dst
, &sel
.daddr
, &sel
.prefixlen_d
);
708 ts2subnet(src
, &sel
.saddr
, &sel
.prefixlen_s
);
709 ts2ports(dst
, &sel
.dport
, &sel
.dport_mask
);
710 ts2ports(src
, &sel
.sport
, &sel
.sport_mask
);
718 * Convert an xfrm_selector to a src|dst traffic_selector
720 static traffic_selector_t
* selector2ts(struct xfrm_selector
*sel
, bool src
)
729 addr
= (u_char
*)&sel
->saddr
;
730 prefixlen
= sel
->prefixlen_s
;
733 port
= htons(sel
->sport
);
738 addr
= (u_char
*)&sel
->daddr
;
739 prefixlen
= sel
->prefixlen_d
;
742 port
= htons(sel
->dport
);
746 /* The Linux 2.6 kernel does not set the selector's family field,
747 * so as a kludge we additionally test the prefix length.
749 if (sel
->family
== AF_INET
|| sel
->prefixlen_s
== 32)
751 host
= host_create_from_chunk(AF_INET
, chunk_create(addr
, 4), 0);
753 else if (sel
->family
== AF_INET6
|| sel
->prefixlen_s
== 128)
755 host
= host_create_from_chunk(AF_INET6
, chunk_create(addr
, 16), 0);
760 return traffic_selector_create_from_subnet(host
, prefixlen
,
767 * Process a XFRM_MSG_ACQUIRE from kernel
769 static void process_acquire(private_kernel_netlink_ipsec_t
*this,
770 struct nlmsghdr
*hdr
)
772 struct xfrm_user_acquire
*acquire
;
775 traffic_selector_t
*src_ts
, *dst_ts
;
779 acquire
= (struct xfrm_user_acquire
*)NLMSG_DATA(hdr
);
780 rta
= XFRM_RTA(hdr
, struct xfrm_user_acquire
);
781 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_user_acquire
);
783 DBG2(DBG_KNL
, "received a XFRM_MSG_ACQUIRE");
785 while (RTA_OK(rta
, rtasize
))
787 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
789 if (rta
->rta_type
== XFRMA_TMPL
)
791 struct xfrm_user_tmpl
* tmpl
;
792 tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rta
);
794 proto
= tmpl
->id
.proto
;
796 rta
= RTA_NEXT(rta
, rtasize
);
805 /* acquire for AH/ESP only, not for IPCOMP */
808 src_ts
= selector2ts(&acquire
->sel
, TRUE
);
809 dst_ts
= selector2ts(&acquire
->sel
, FALSE
);
811 hydra
->kernel_interface
->acquire(hydra
->kernel_interface
, reqid
, src_ts
,
816 * Process a XFRM_MSG_EXPIRE from kernel
818 static void process_expire(private_kernel_netlink_ipsec_t
*this,
819 struct nlmsghdr
*hdr
)
821 struct xfrm_user_expire
*expire
;
822 u_int32_t spi
, reqid
;
825 expire
= (struct xfrm_user_expire
*)NLMSG_DATA(hdr
);
826 protocol
= expire
->state
.id
.proto
;
827 spi
= expire
->state
.id
.spi
;
828 reqid
= expire
->state
.reqid
;
830 DBG2(DBG_KNL
, "received a XFRM_MSG_EXPIRE");
832 if (protocol
!= IPPROTO_ESP
&& protocol
!= IPPROTO_AH
)
834 DBG2(DBG_KNL
, "ignoring XFRM_MSG_EXPIRE for SA with SPI %.8x and "
835 "reqid {%u} which is not a CHILD_SA", ntohl(spi
), reqid
);
839 hydra
->kernel_interface
->expire(hydra
->kernel_interface
, reqid
, protocol
,
840 spi
, expire
->hard
!= 0);
844 * Process a XFRM_MSG_MIGRATE from kernel
846 static void process_migrate(private_kernel_netlink_ipsec_t
*this,
847 struct nlmsghdr
*hdr
)
849 struct xfrm_userpolicy_id
*policy_id
;
852 traffic_selector_t
*src_ts
, *dst_ts
;
853 host_t
*local
= NULL
, *remote
= NULL
;
854 host_t
*old_src
= NULL
, *old_dst
= NULL
;
855 host_t
*new_src
= NULL
, *new_dst
= NULL
;
859 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
860 rta
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
861 rtasize
= XFRM_PAYLOAD(hdr
, struct xfrm_userpolicy_id
);
863 DBG2(DBG_KNL
, "received a XFRM_MSG_MIGRATE");
865 src_ts
= selector2ts(&policy_id
->sel
, TRUE
);
866 dst_ts
= selector2ts(&policy_id
->sel
, FALSE
);
867 dir
= (policy_dir_t
)policy_id
->dir
;
869 DBG2(DBG_KNL
, " policy: %R === %R %N", src_ts
, dst_ts
, policy_dir_names
);
871 while (RTA_OK(rta
, rtasize
))
873 DBG2(DBG_KNL
, " %N", xfrm_attr_type_names
, rta
->rta_type
);
874 if (rta
->rta_type
== XFRMA_KMADDRESS
)
876 struct xfrm_user_kmaddress
*kmaddress
;
878 kmaddress
= (struct xfrm_user_kmaddress
*)RTA_DATA(rta
);
879 local
= xfrm2host(kmaddress
->family
, &kmaddress
->local
, 0);
880 remote
= xfrm2host(kmaddress
->family
, &kmaddress
->remote
, 0);
881 DBG2(DBG_KNL
, " kmaddress: %H...%H", local
, remote
);
883 else if (rta
->rta_type
== XFRMA_MIGRATE
)
885 struct xfrm_user_migrate
*migrate
;
887 migrate
= (struct xfrm_user_migrate
*)RTA_DATA(rta
);
888 old_src
= xfrm2host(migrate
->old_family
, &migrate
->old_saddr
, 0);
889 old_dst
= xfrm2host(migrate
->old_family
, &migrate
->old_daddr
, 0);
890 new_src
= xfrm2host(migrate
->new_family
, &migrate
->new_saddr
, 0);
891 new_dst
= xfrm2host(migrate
->new_family
, &migrate
->new_daddr
, 0);
892 reqid
= migrate
->reqid
;
893 DBG2(DBG_KNL
, " migrate %H...%H to %H...%H, reqid {%u}",
894 old_src
, old_dst
, new_src
, new_dst
, reqid
);
900 rta
= RTA_NEXT(rta
, rtasize
);
903 if (src_ts
&& dst_ts
&& local
&& remote
)
905 hydra
->kernel_interface
->migrate(hydra
->kernel_interface
, reqid
,
906 src_ts
, dst_ts
, dir
, local
, remote
);
918 * Process a XFRM_MSG_MAPPING from kernel
920 static void process_mapping(private_kernel_netlink_ipsec_t
*this,
921 struct nlmsghdr
*hdr
)
923 struct xfrm_user_mapping
*mapping
;
924 u_int32_t spi
, reqid
;
926 mapping
= (struct xfrm_user_mapping
*)NLMSG_DATA(hdr
);
927 spi
= mapping
->id
.spi
;
928 reqid
= mapping
->reqid
;
930 DBG2(DBG_KNL
, "received a XFRM_MSG_MAPPING");
932 if (mapping
->id
.proto
== IPPROTO_ESP
)
935 host
= xfrm2host(mapping
->id
.family
, &mapping
->new_saddr
,
939 hydra
->kernel_interface
->mapping(hydra
->kernel_interface
, reqid
,
946 * Receives events from kernel
948 static job_requeue_t
receive_events(private_kernel_netlink_ipsec_t
*this)
951 struct nlmsghdr
*hdr
= (struct nlmsghdr
*)response
;
952 struct sockaddr_nl addr
;
953 socklen_t addr_len
= sizeof(addr
);
957 oldstate
= thread_cancelability(TRUE
);
958 len
= recvfrom(this->socket_xfrm_events
, response
, sizeof(response
), 0,
959 (struct sockaddr
*)&addr
, &addr_len
);
960 thread_cancelability(oldstate
);
967 /* interrupted, try again */
968 return JOB_REQUEUE_DIRECT
;
970 /* no data ready, select again */
971 return JOB_REQUEUE_DIRECT
;
973 DBG1(DBG_KNL
, "unable to receive from xfrm event socket");
975 return JOB_REQUEUE_FAIR
;
979 if (addr
.nl_pid
!= 0)
980 { /* not from kernel. not interested, try another one */
981 return JOB_REQUEUE_DIRECT
;
984 while (NLMSG_OK(hdr
, len
))
986 switch (hdr
->nlmsg_type
)
988 case XFRM_MSG_ACQUIRE
:
989 process_acquire(this, hdr
);
991 case XFRM_MSG_EXPIRE
:
992 process_expire(this, hdr
);
994 case XFRM_MSG_MIGRATE
:
995 process_migrate(this, hdr
);
997 case XFRM_MSG_MAPPING
:
998 process_mapping(this, hdr
);
1001 DBG1(DBG_KNL
, "received unknown event from xfrm event "
1002 "socket: %d", hdr
->nlmsg_type
);
1005 hdr
= NLMSG_NEXT(hdr
, len
);
1007 return JOB_REQUEUE_DIRECT
;
1011 * Get an SPI for a specific protocol from the kernel.
1013 static status_t
get_spi_internal(private_kernel_netlink_ipsec_t
*this,
1014 host_t
*src
, host_t
*dst
, u_int8_t proto
, u_int32_t min
, u_int32_t max
,
1015 u_int32_t reqid
, u_int32_t
*spi
)
1017 netlink_buf_t request
;
1018 struct nlmsghdr
*hdr
, *out
;
1019 struct xfrm_userspi_info
*userspi
;
1020 u_int32_t received_spi
= 0;
1023 memset(&request
, 0, sizeof(request
));
1025 hdr
= (struct nlmsghdr
*)request
;
1026 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1027 hdr
->nlmsg_type
= XFRM_MSG_ALLOCSPI
;
1028 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userspi_info
));
1030 userspi
= (struct xfrm_userspi_info
*)NLMSG_DATA(hdr
);
1031 host2xfrm(src
, &userspi
->info
.saddr
);
1032 host2xfrm(dst
, &userspi
->info
.id
.daddr
);
1033 userspi
->info
.id
.proto
= proto
;
1034 userspi
->info
.mode
= XFRM_MODE_TUNNEL
;
1035 userspi
->info
.reqid
= reqid
;
1036 userspi
->info
.family
= src
->get_family(src
);
1040 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1043 while (NLMSG_OK(hdr
, len
))
1045 switch (hdr
->nlmsg_type
)
1047 case XFRM_MSG_NEWSA
:
1049 struct xfrm_usersa_info
* usersa
= NLMSG_DATA(hdr
);
1050 received_spi
= usersa
->id
.spi
;
1055 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1056 DBG1(DBG_KNL
, "allocating SPI failed: %s (%d)",
1057 strerror(-err
->error
), -err
->error
);
1061 hdr
= NLMSG_NEXT(hdr
, len
);
1071 if (received_spi
== 0)
1076 *spi
= received_spi
;
1080 METHOD(kernel_ipsec_t
, get_spi
, status_t
,
1081 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1082 u_int8_t protocol
, u_int32_t reqid
, u_int32_t
*spi
)
1084 DBG2(DBG_KNL
, "getting SPI for reqid {%u}", reqid
);
1086 if (get_spi_internal(this, src
, dst
, protocol
,
1087 0xc0000000, 0xcFFFFFFF, reqid
, spi
) != SUCCESS
)
1089 DBG1(DBG_KNL
, "unable to get SPI for reqid {%u}", reqid
);
1093 DBG2(DBG_KNL
, "got SPI %.8x for reqid {%u}", ntohl(*spi
), reqid
);
1097 METHOD(kernel_ipsec_t
, get_cpi
, status_t
,
1098 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1099 u_int32_t reqid
, u_int16_t
*cpi
)
1101 u_int32_t received_spi
= 0;
1103 DBG2(DBG_KNL
, "getting CPI for reqid {%u}", reqid
);
1105 if (get_spi_internal(this, src
, dst
, IPPROTO_COMP
,
1106 0x100, 0xEFFF, reqid
, &received_spi
) != SUCCESS
)
1108 DBG1(DBG_KNL
, "unable to get CPI for reqid {%u}", reqid
);
1112 *cpi
= htons((u_int16_t
)ntohl(received_spi
));
1114 DBG2(DBG_KNL
, "got CPI %.4x for reqid {%u}", ntohs(*cpi
), reqid
);
1118 METHOD(kernel_ipsec_t
, add_sa
, status_t
,
1119 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1120 u_int32_t spi
, u_int8_t protocol
, u_int32_t reqid
, mark_t mark
,
1121 u_int32_t tfc
, lifetime_cfg_t
*lifetime
, u_int16_t enc_alg
, chunk_t enc_key
,
1122 u_int16_t int_alg
, chunk_t int_key
, ipsec_mode_t mode
, u_int16_t ipcomp
,
1123 u_int16_t cpi
, bool encap
, bool esn
, bool inbound
,
1124 traffic_selector_t
* src_ts
, traffic_selector_t
* dst_ts
)
1126 netlink_buf_t request
;
1128 struct nlmsghdr
*hdr
;
1129 struct xfrm_usersa_info
*sa
;
1130 u_int16_t icv_size
= 64;
1131 status_t status
= FAILED
;
1133 /* if IPComp is used, we install an additional IPComp SA. if the cpi is 0
1134 * we are in the recursive call below */
1135 if (ipcomp
!= IPCOMP_NONE
&& cpi
!= 0)
1137 lifetime_cfg_t lft
= {{0,0,0},{0,0,0},{0,0,0}};
1138 add_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, reqid
, mark
,
1139 tfc
, &lft
, ENCR_UNDEFINED
, chunk_empty
, AUTH_UNDEFINED
,
1140 chunk_empty
, mode
, ipcomp
, 0, FALSE
, FALSE
, inbound
, NULL
, NULL
);
1141 ipcomp
= IPCOMP_NONE
;
1142 /* use transport mode ESP SA, IPComp uses tunnel mode */
1143 mode
= MODE_TRANSPORT
;
1146 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
);
1155 DBG2(DBG_KNL
, "adding SAD entry with SPI %.8x and reqid {%u}",
1158 hdr
= (struct nlmsghdr
*)request
;
1159 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1160 hdr
->nlmsg_type
= inbound ? XFRM_MSG_UPDSA
: XFRM_MSG_NEWSA
;
1161 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1163 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1164 host2xfrm(src
, &sa
->saddr
);
1165 host2xfrm(dst
, &sa
->id
.daddr
);
1167 sa
->id
.proto
= protocol
;
1168 sa
->family
= src
->get_family(src
);
1169 sa
->mode
= mode2kernel(mode
);
1173 sa
->flags
|= XFRM_STATE_AF_UNSPEC
;
1176 case MODE_TRANSPORT
:
1177 if(src_ts
&& dst_ts
)
1179 sa
->sel
= ts2selector(src_ts
, dst_ts
);
1187 sa
->lft
.soft_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.rekey
);
1188 sa
->lft
.hard_byte_limit
= XFRM_LIMIT(lifetime
->bytes
.life
);
1189 sa
->lft
.soft_packet_limit
= XFRM_LIMIT(lifetime
->packets
.rekey
);
1190 sa
->lft
.hard_packet_limit
= XFRM_LIMIT(lifetime
->packets
.life
);
1191 /* we use lifetimes since added, not since used */
1192 sa
->lft
.soft_add_expires_seconds
= lifetime
->time
.rekey
;
1193 sa
->lft
.hard_add_expires_seconds
= lifetime
->time
.life
;
1194 sa
->lft
.soft_use_expires_seconds
= 0;
1195 sa
->lft
.hard_use_expires_seconds
= 0;
1197 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1201 case ENCR_UNDEFINED
:
1204 case ENCR_AES_CCM_ICV16
:
1205 case ENCR_AES_GCM_ICV16
:
1206 case ENCR_NULL_AUTH_AES_GMAC
:
1207 case ENCR_CAMELLIA_CCM_ICV16
:
1210 case ENCR_AES_CCM_ICV12
:
1211 case ENCR_AES_GCM_ICV12
:
1212 case ENCR_CAMELLIA_CCM_ICV12
:
1215 case ENCR_AES_CCM_ICV8
:
1216 case ENCR_AES_GCM_ICV8
:
1217 case ENCR_CAMELLIA_CCM_ICV8
:
1219 struct xfrm_algo_aead
*algo
;
1221 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1222 if (alg_name
== NULL
)
1224 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1225 encryption_algorithm_names
, enc_alg
);
1228 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1229 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1231 rthdr
->rta_type
= XFRMA_ALG_AEAD
;
1232 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_aead
) +
1234 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1235 if (hdr
->nlmsg_len
> sizeof(request
))
1240 algo
= (struct xfrm_algo_aead
*)RTA_DATA(rthdr
);
1241 algo
->alg_key_len
= enc_key
.len
* 8;
1242 algo
->alg_icv_len
= icv_size
;
1243 strcpy(algo
->alg_name
, alg_name
);
1244 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1246 rthdr
= XFRM_RTA_NEXT(rthdr
);
1251 struct xfrm_algo
*algo
;
1253 alg_name
= lookup_algorithm(encryption_algs
, enc_alg
);
1254 if (alg_name
== NULL
)
1256 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1257 encryption_algorithm_names
, enc_alg
);
1260 DBG2(DBG_KNL
, " using encryption algorithm %N with key size %d",
1261 encryption_algorithm_names
, enc_alg
, enc_key
.len
* 8);
1263 rthdr
->rta_type
= XFRMA_ALG_CRYPT
;
1264 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + enc_key
.len
);
1265 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1266 if (hdr
->nlmsg_len
> sizeof(request
))
1271 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1272 algo
->alg_key_len
= enc_key
.len
* 8;
1273 strcpy(algo
->alg_name
, alg_name
);
1274 memcpy(algo
->alg_key
, enc_key
.ptr
, enc_key
.len
);
1276 rthdr
= XFRM_RTA_NEXT(rthdr
);
1280 if (int_alg
!= AUTH_UNDEFINED
)
1282 alg_name
= lookup_algorithm(integrity_algs
, int_alg
);
1283 if (alg_name
== NULL
)
1285 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1286 integrity_algorithm_names
, int_alg
);
1289 DBG2(DBG_KNL
, " using integrity algorithm %N with key size %d",
1290 integrity_algorithm_names
, int_alg
, int_key
.len
* 8);
1292 if (int_alg
== AUTH_HMAC_SHA2_256_128
)
1294 struct xfrm_algo_auth
* algo
;
1296 /* the kernel uses SHA256 with 96 bit truncation by default,
1297 * use specified truncation size supported by newer kernels */
1298 rthdr
->rta_type
= XFRMA_ALG_AUTH_TRUNC
;
1299 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo_auth
) +
1302 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1303 if (hdr
->nlmsg_len
> sizeof(request
))
1308 algo
= (struct xfrm_algo_auth
*)RTA_DATA(rthdr
);
1309 algo
->alg_key_len
= int_key
.len
* 8;
1310 algo
->alg_trunc_len
= 128;
1311 strcpy(algo
->alg_name
, alg_name
);
1312 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1316 struct xfrm_algo
* algo
;
1318 rthdr
->rta_type
= XFRMA_ALG_AUTH
;
1319 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
) + int_key
.len
);
1321 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1322 if (hdr
->nlmsg_len
> sizeof(request
))
1327 algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1328 algo
->alg_key_len
= int_key
.len
* 8;
1329 strcpy(algo
->alg_name
, alg_name
);
1330 memcpy(algo
->alg_key
, int_key
.ptr
, int_key
.len
);
1332 rthdr
= XFRM_RTA_NEXT(rthdr
);
1335 if (ipcomp
!= IPCOMP_NONE
)
1337 rthdr
->rta_type
= XFRMA_ALG_COMP
;
1338 alg_name
= lookup_algorithm(compression_algs
, ipcomp
);
1339 if (alg_name
== NULL
)
1341 DBG1(DBG_KNL
, "algorithm %N not supported by kernel!",
1342 ipcomp_transform_names
, ipcomp
);
1345 DBG2(DBG_KNL
, " using compression algorithm %N",
1346 ipcomp_transform_names
, ipcomp
);
1348 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_algo
));
1349 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1350 if (hdr
->nlmsg_len
> sizeof(request
))
1355 struct xfrm_algo
* algo
= (struct xfrm_algo
*)RTA_DATA(rthdr
);
1356 algo
->alg_key_len
= 0;
1357 strcpy(algo
->alg_name
, alg_name
);
1359 rthdr
= XFRM_RTA_NEXT(rthdr
);
1364 struct xfrm_encap_tmpl
*tmpl
;
1366 rthdr
->rta_type
= XFRMA_ENCAP
;
1367 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1369 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1370 if (hdr
->nlmsg_len
> sizeof(request
))
1375 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rthdr
);
1376 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1377 tmpl
->encap_sport
= htons(src
->get_port(src
));
1378 tmpl
->encap_dport
= htons(dst
->get_port(dst
));
1379 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1380 /* encap_oa could probably be derived from the
1381 * traffic selectors [rfc4306, p39]. In the netlink kernel
1382 * implementation pluto does the same as we do here but it uses
1383 * encap_oa in the pfkey implementation.
1384 * BUT as /usr/src/linux/net/key/af_key.c indicates the kernel ignores
1386 * -> does that mean that NAT-T encap doesn't work in transport mode?
1387 * No. The reason the kernel ignores NAT-OA is that it recomputes
1388 * (or, rather, just ignores) the checksum. If packets pass the IPsec
1389 * checks it marks them "checksum ok" so OA isn't needed. */
1390 rthdr
= XFRM_RTA_NEXT(rthdr
);
1395 struct xfrm_mark
*mrk
;
1397 rthdr
->rta_type
= XFRMA_MARK
;
1398 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1400 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1401 if (hdr
->nlmsg_len
> sizeof(request
))
1406 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1407 mrk
->v
= mark
.value
;
1409 rthdr
= XFRM_RTA_NEXT(rthdr
);
1416 rthdr
->rta_type
= XFRMA_TFCPAD
;
1417 rthdr
->rta_len
= RTA_LENGTH(sizeof(u_int32_t
));
1419 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1420 if (hdr
->nlmsg_len
> sizeof(request
))
1425 tfcpad
= (u_int32_t
*)RTA_DATA(rthdr
);
1427 rthdr
= XFRM_RTA_NEXT(rthdr
);
1430 if (protocol
!= IPPROTO_COMP
)
1432 if (esn
|| this->replay_window
> DEFAULT_REPLAY_WINDOW
)
1434 /* for ESN or larger replay windows we need the new
1435 * XFRMA_REPLAY_ESN_VAL attribute to configure a bitmap */
1436 struct xfrm_replay_state_esn
*replay
;
1438 rthdr
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1439 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1440 (this->replay_window
+ 7) / 8);
1442 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1443 if (hdr
->nlmsg_len
> sizeof(request
))
1448 replay
= (struct xfrm_replay_state_esn
*)RTA_DATA(rthdr
);
1449 /* bmp_len contains number uf __u32's */
1450 replay
->bmp_len
= this->replay_bmp
;
1451 replay
->replay_window
= this->replay_window
;
1452 DBG2(DBG_KNL
, " using replay window of %u bytes",
1453 this->replay_window
);
1455 rthdr
= XFRM_RTA_NEXT(rthdr
);
1458 DBG2(DBG_KNL
, " using extended sequence numbers (ESN)");
1459 sa
->flags
|= XFRM_STATE_ESN
;
1464 sa
->replay_window
= DEFAULT_REPLAY_WINDOW
;
1468 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1472 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x "
1473 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1477 DBG1(DBG_KNL
, "unable to add SAD entry with SPI %.8x", ntohl(spi
));
1485 memwipe(request
, sizeof(request
));
1490 * Get the ESN replay state (i.e. sequence numbers) of an SA.
1492 * Allocates into one the replay state structure we get from the kernel.
1494 static void get_replay_state(private_kernel_netlink_ipsec_t
*this,
1495 u_int32_t spi
, u_int8_t protocol
, host_t
*dst
,
1496 struct xfrm_replay_state_esn
**replay_esn
,
1497 struct xfrm_replay_state
**replay
)
1499 netlink_buf_t request
;
1500 struct nlmsghdr
*hdr
, *out
= NULL
;
1501 struct xfrm_aevent_id
*out_aevent
= NULL
, *aevent_id
;
1506 memset(&request
, 0, sizeof(request
));
1508 DBG2(DBG_KNL
, "querying replay state from SAD entry with SPI %.8x",
1511 hdr
= (struct nlmsghdr
*)request
;
1512 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1513 hdr
->nlmsg_type
= XFRM_MSG_GETAE
;
1514 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_aevent_id
));
1516 aevent_id
= (struct xfrm_aevent_id
*)NLMSG_DATA(hdr
);
1517 aevent_id
->flags
= XFRM_AE_RVAL
;
1519 host2xfrm(dst
, &aevent_id
->sa_id
.daddr
);
1520 aevent_id
->sa_id
.spi
= spi
;
1521 aevent_id
->sa_id
.proto
= protocol
;
1522 aevent_id
->sa_id
.family
= dst
->get_family(dst
);
1524 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1527 while (NLMSG_OK(hdr
, len
))
1529 switch (hdr
->nlmsg_type
)
1531 case XFRM_MSG_NEWAE
:
1533 out_aevent
= NLMSG_DATA(hdr
);
1538 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1539 DBG1(DBG_KNL
, "querying replay state from SAD entry "
1540 "failed: %s (%d)", strerror(-err
->error
),
1545 hdr
= NLMSG_NEXT(hdr
, len
);
1556 rta
= XFRM_RTA(out
, struct xfrm_aevent_id
);
1557 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_aevent_id
);
1558 while (RTA_OK(rta
, rtasize
))
1560 if (rta
->rta_type
== XFRMA_REPLAY_VAL
&&
1561 RTA_PAYLOAD(rta
) == sizeof(**replay
))
1563 *replay
= malloc(RTA_PAYLOAD(rta
));
1564 memcpy(*replay
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1567 if (rta
->rta_type
== XFRMA_REPLAY_ESN_VAL
&&
1568 RTA_PAYLOAD(rta
) >= sizeof(**replay_esn
) + this->replay_bmp
)
1570 *replay_esn
= malloc(RTA_PAYLOAD(rta
));
1571 memcpy(*replay_esn
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
1574 rta
= RTA_NEXT(rta
, rtasize
);
1580 METHOD(kernel_ipsec_t
, query_sa
, status_t
,
1581 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1582 u_int32_t spi
, u_int8_t protocol
, mark_t mark
, u_int64_t
*bytes
)
1584 netlink_buf_t request
;
1585 struct nlmsghdr
*out
= NULL
, *hdr
;
1586 struct xfrm_usersa_id
*sa_id
;
1587 struct xfrm_usersa_info
*sa
= NULL
;
1588 status_t status
= FAILED
;
1591 memset(&request
, 0, sizeof(request
));
1595 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x (mark %u/0x%8x)",
1596 ntohl(spi
), mark
.value
, mark
.mask
);
1600 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x", ntohl(spi
));
1602 hdr
= (struct nlmsghdr
*)request
;
1603 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1604 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1605 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1607 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1608 host2xfrm(dst
, &sa_id
->daddr
);
1610 sa_id
->proto
= protocol
;
1611 sa_id
->family
= dst
->get_family(dst
);
1615 struct xfrm_mark
*mrk
;
1616 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1618 rthdr
->rta_type
= XFRMA_MARK
;
1619 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1620 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1621 if (hdr
->nlmsg_len
> sizeof(request
))
1626 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1627 mrk
->v
= mark
.value
;
1631 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1634 while (NLMSG_OK(hdr
, len
))
1636 switch (hdr
->nlmsg_type
)
1638 case XFRM_MSG_NEWSA
:
1640 sa
= (struct xfrm_usersa_info
*)NLMSG_DATA(hdr
);
1645 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1649 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1650 "(mark %u/0x%8x) failed: %s (%d)",
1651 ntohl(spi
), mark
.value
, mark
.mask
,
1652 strerror(-err
->error
), -err
->error
);
1656 DBG1(DBG_KNL
, "querying SAD entry with SPI %.8x "
1657 "failed: %s (%d)", ntohl(spi
),
1658 strerror(-err
->error
), -err
->error
);
1663 hdr
= NLMSG_NEXT(hdr
, len
);
1674 DBG2(DBG_KNL
, "unable to query SAD entry with SPI %.8x", ntohl(spi
));
1678 *bytes
= sa
->curlft
.bytes
;
1686 METHOD(kernel_ipsec_t
, del_sa
, status_t
,
1687 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
1688 u_int32_t spi
, u_int8_t protocol
, u_int16_t cpi
, mark_t mark
)
1690 netlink_buf_t request
;
1691 struct nlmsghdr
*hdr
;
1692 struct xfrm_usersa_id
*sa_id
;
1694 /* if IPComp was used, we first delete the additional IPComp SA */
1697 del_sa(this, src
, dst
, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0, mark
);
1700 memset(&request
, 0, sizeof(request
));
1704 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x (mark %u/0x%8x)",
1705 ntohl(spi
), mark
.value
, mark
.mask
);
1709 DBG2(DBG_KNL
, "deleting SAD entry with SPI %.8x", ntohl(spi
));
1711 hdr
= (struct nlmsghdr
*)request
;
1712 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1713 hdr
->nlmsg_type
= XFRM_MSG_DELSA
;
1714 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1716 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1717 host2xfrm(dst
, &sa_id
->daddr
);
1719 sa_id
->proto
= protocol
;
1720 sa_id
->family
= dst
->get_family(dst
);
1724 struct xfrm_mark
*mrk
;
1725 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_usersa_id
);
1727 rthdr
->rta_type
= XFRMA_MARK
;
1728 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
1729 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
1730 if (hdr
->nlmsg_len
> sizeof(request
))
1735 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
1736 mrk
->v
= mark
.value
;
1740 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1744 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x "
1745 "(mark %u/0x%8x)", ntohl(spi
), mark
.value
, mark
.mask
);
1749 DBG1(DBG_KNL
, "unable to delete SAD entry with SPI %.8x",
1756 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x (mark %u/0x%8x)",
1757 ntohl(spi
), mark
.value
, mark
.mask
);
1761 DBG2(DBG_KNL
, "deleted SAD entry with SPI %.8x", ntohl(spi
));
1766 METHOD(kernel_ipsec_t
, update_sa
, status_t
,
1767 private_kernel_netlink_ipsec_t
*this, u_int32_t spi
, u_int8_t protocol
,
1768 u_int16_t cpi
, host_t
*src
, host_t
*dst
, host_t
*new_src
, host_t
*new_dst
,
1769 bool old_encap
, bool new_encap
, mark_t mark
)
1771 netlink_buf_t request
;
1773 struct nlmsghdr
*hdr
, *out
= NULL
;
1774 struct xfrm_usersa_id
*sa_id
;
1775 struct xfrm_usersa_info
*out_sa
= NULL
, *sa
;
1779 struct xfrm_encap_tmpl
* tmpl
= NULL
;
1780 struct xfrm_replay_state
*replay
= NULL
;
1781 struct xfrm_replay_state_esn
*replay_esn
= NULL
;
1782 status_t status
= FAILED
;
1784 /* if IPComp is used, we first update the IPComp SA */
1787 update_sa(this, htonl(ntohs(cpi
)), IPPROTO_COMP
, 0,
1788 src
, dst
, new_src
, new_dst
, FALSE
, FALSE
, mark
);
1791 memset(&request
, 0, sizeof(request
));
1793 DBG2(DBG_KNL
, "querying SAD entry with SPI %.8x for update", ntohl(spi
));
1795 /* query the existing SA first */
1796 hdr
= (struct nlmsghdr
*)request
;
1797 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
1798 hdr
->nlmsg_type
= XFRM_MSG_GETSA
;
1799 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_id
));
1801 sa_id
= (struct xfrm_usersa_id
*)NLMSG_DATA(hdr
);
1802 host2xfrm(dst
, &sa_id
->daddr
);
1804 sa_id
->proto
= protocol
;
1805 sa_id
->family
= dst
->get_family(dst
);
1807 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
1810 while (NLMSG_OK(hdr
, len
))
1812 switch (hdr
->nlmsg_type
)
1814 case XFRM_MSG_NEWSA
:
1816 out_sa
= NLMSG_DATA(hdr
);
1821 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
1822 DBG1(DBG_KNL
, "querying SAD entry failed: %s (%d)",
1823 strerror(-err
->error
), -err
->error
);
1827 hdr
= NLMSG_NEXT(hdr
, len
);
1837 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1841 get_replay_state(this, spi
, protocol
, dst
, &replay_esn
, &replay
);
1843 /* delete the old SA (without affecting the IPComp SA) */
1844 if (del_sa(this, src
, dst
, spi
, protocol
, 0, mark
) != SUCCESS
)
1846 DBG1(DBG_KNL
, "unable to delete old SAD entry with SPI %.8x",
1851 DBG2(DBG_KNL
, "updating SAD entry with SPI %.8x from %#H..%#H to %#H..%#H",
1852 ntohl(spi
), src
, dst
, new_src
, new_dst
);
1853 /* copy over the SA from out to request */
1854 hdr
= (struct nlmsghdr
*)request
;
1855 memcpy(hdr
, out
, min(out
->nlmsg_len
, sizeof(request
)));
1856 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1857 hdr
->nlmsg_type
= XFRM_MSG_NEWSA
;
1858 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_info
));
1859 sa
= NLMSG_DATA(hdr
);
1860 sa
->family
= new_dst
->get_family(new_dst
);
1862 if (!src
->ip_equals(src
, new_src
))
1864 host2xfrm(new_src
, &sa
->saddr
);
1866 if (!dst
->ip_equals(dst
, new_dst
))
1868 host2xfrm(new_dst
, &sa
->id
.daddr
);
1871 rta
= XFRM_RTA(out
, struct xfrm_usersa_info
);
1872 rtasize
= XFRM_PAYLOAD(out
, struct xfrm_usersa_info
);
1873 pos
= (u_char
*)XFRM_RTA(hdr
, struct xfrm_usersa_info
);
1874 while(RTA_OK(rta
, rtasize
))
1876 /* copy all attributes, but not XFRMA_ENCAP if we are disabling it */
1877 if (rta
->rta_type
!= XFRMA_ENCAP
|| new_encap
)
1879 if (rta
->rta_type
== XFRMA_ENCAP
)
1880 { /* update encap tmpl */
1881 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1882 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1883 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1885 memcpy(pos
, rta
, rta
->rta_len
);
1886 pos
+= RTA_ALIGN(rta
->rta_len
);
1887 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1889 rta
= RTA_NEXT(rta
, rtasize
);
1892 rta
= (struct rtattr
*)pos
;
1893 if (tmpl
== NULL
&& new_encap
)
1894 { /* add tmpl if we are enabling it */
1895 rta
->rta_type
= XFRMA_ENCAP
;
1896 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_encap_tmpl
));
1898 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1899 if (hdr
->nlmsg_len
> sizeof(request
))
1904 tmpl
= (struct xfrm_encap_tmpl
*)RTA_DATA(rta
);
1905 tmpl
->encap_type
= UDP_ENCAP_ESPINUDP
;
1906 tmpl
->encap_sport
= ntohs(new_src
->get_port(new_src
));
1907 tmpl
->encap_dport
= ntohs(new_dst
->get_port(new_dst
));
1908 memset(&tmpl
->encap_oa
, 0, sizeof (xfrm_address_t
));
1910 rta
= XFRM_RTA_NEXT(rta
);
1915 rta
->rta_type
= XFRMA_REPLAY_ESN_VAL
;
1916 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state_esn
) +
1919 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1920 if (hdr
->nlmsg_len
> sizeof(request
))
1924 memcpy(RTA_DATA(rta
), replay_esn
,
1925 sizeof(struct xfrm_replay_state_esn
) + this->replay_bmp
);
1927 rta
= XFRM_RTA_NEXT(rta
);
1931 rta
->rta_type
= XFRMA_REPLAY_VAL
;
1932 rta
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_replay_state
));
1934 hdr
->nlmsg_len
+= RTA_ALIGN(rta
->rta_len
);
1935 if (hdr
->nlmsg_len
> sizeof(request
))
1939 memcpy(RTA_DATA(rta
), replay
, sizeof(replay
));
1941 rta
= XFRM_RTA_NEXT(rta
);
1945 DBG1(DBG_KNL
, "unable to copy replay state from old SAD entry "
1946 "with SPI %.8x", ntohl(spi
));
1949 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1951 DBG1(DBG_KNL
, "unable to update SAD entry with SPI %.8x", ntohl(spi
));
1965 METHOD(kernel_ipsec_t
, flush_sas
, status_t
,
1966 private_kernel_netlink_ipsec_t
*this)
1968 netlink_buf_t request
;
1969 struct nlmsghdr
*hdr
;
1970 struct xfrm_usersa_flush
*flush
;
1972 memset(&request
, 0, sizeof(request
));
1974 DBG2(DBG_KNL
, "flushing all SAD entries");
1976 hdr
= (struct nlmsghdr
*)request
;
1977 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1978 hdr
->nlmsg_type
= XFRM_MSG_FLUSHSA
;
1979 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush
));
1981 flush
= (struct xfrm_usersa_flush
*)NLMSG_DATA(hdr
);
1982 flush
->proto
= IPSEC_PROTO_ANY
;
1984 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
1986 DBG1(DBG_KNL
, "unable to flush SAD entries");
1993 * Add or update a policy in the kernel.
1995 * Note: The mutex has to be locked when entering this function.
1997 static status_t
add_policy_internal(private_kernel_netlink_ipsec_t
*this,
1998 policy_entry_t
*policy
, policy_sa_t
*mapping
, bool update
)
2000 netlink_buf_t request
;
2001 policy_entry_t clone
;
2002 ipsec_sa_t
*ipsec
= mapping
->sa
;
2003 struct xfrm_userpolicy_info
*policy_info
;
2004 struct nlmsghdr
*hdr
;
2007 /* clone the policy so we are able to check it out again later */
2008 memcpy(&clone
, policy
, sizeof(policy_entry_t
));
2010 memset(&request
, 0, sizeof(request
));
2011 hdr
= (struct nlmsghdr
*)request
;
2012 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2013 hdr
->nlmsg_type
= update ? XFRM_MSG_UPDPOLICY
: XFRM_MSG_NEWPOLICY
;
2014 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info
));
2016 policy_info
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2017 policy_info
->sel
= policy
->sel
;
2018 policy_info
->dir
= policy
->direction
;
2020 /* calculate priority based on selector size, small size = high prio */
2021 policy_info
->priority
= mapping
->priority
;
2022 policy_info
->action
= mapping
->type
!= POLICY_DROP ? XFRM_POLICY_ALLOW
2023 : XFRM_POLICY_BLOCK
;
2024 policy_info
->share
= XFRM_SHARE_ANY
;
2026 /* policies don't expire */
2027 policy_info
->lft
.soft_byte_limit
= XFRM_INF
;
2028 policy_info
->lft
.soft_packet_limit
= XFRM_INF
;
2029 policy_info
->lft
.hard_byte_limit
= XFRM_INF
;
2030 policy_info
->lft
.hard_packet_limit
= XFRM_INF
;
2031 policy_info
->lft
.soft_add_expires_seconds
= 0;
2032 policy_info
->lft
.hard_add_expires_seconds
= 0;
2033 policy_info
->lft
.soft_use_expires_seconds
= 0;
2034 policy_info
->lft
.hard_use_expires_seconds
= 0;
2036 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_info
);
2038 if (mapping
->type
== POLICY_IPSEC
)
2040 struct xfrm_user_tmpl
*tmpl
= (struct xfrm_user_tmpl
*)RTA_DATA(rthdr
);
2045 { IPPROTO_COMP
, ipsec
->cfg
.ipcomp
.transform
!= IPCOMP_NONE
},
2046 { IPPROTO_ESP
, ipsec
->cfg
.esp
.use
},
2047 { IPPROTO_AH
, ipsec
->cfg
.ah
.use
},
2049 ipsec_mode_t proto_mode
= ipsec
->cfg
.mode
;
2051 rthdr
->rta_type
= XFRMA_TMPL
;
2052 rthdr
->rta_len
= 0; /* actual length is set below */
2054 for (i
= 0; i
< countof(protos
); i
++)
2061 rthdr
->rta_len
+= RTA_LENGTH(sizeof(struct xfrm_user_tmpl
));
2062 hdr
->nlmsg_len
+= RTA_ALIGN(RTA_LENGTH(sizeof(struct xfrm_user_tmpl
)));
2063 if (hdr
->nlmsg_len
> sizeof(request
))
2068 tmpl
->reqid
= ipsec
->cfg
.reqid
;
2069 tmpl
->id
.proto
= protos
[i
].proto
;
2070 tmpl
->aalgos
= tmpl
->ealgos
= tmpl
->calgos
= ~0;
2071 tmpl
->mode
= mode2kernel(proto_mode
);
2072 tmpl
->optional
= protos
[i
].proto
== IPPROTO_COMP
&&
2073 policy
->direction
!= POLICY_OUT
;
2074 tmpl
->family
= ipsec
->src
->get_family(ipsec
->src
);
2076 if (proto_mode
== MODE_TUNNEL
)
2077 { /* only for tunnel mode */
2078 host2xfrm(ipsec
->src
, &tmpl
->saddr
);
2079 host2xfrm(ipsec
->dst
, &tmpl
->id
.daddr
);
2084 /* use transport mode for other SAs */
2085 proto_mode
= MODE_TRANSPORT
;
2088 rthdr
= XFRM_RTA_NEXT(rthdr
);
2091 if (ipsec
->mark
.value
)
2093 struct xfrm_mark
*mrk
;
2095 rthdr
->rta_type
= XFRMA_MARK
;
2096 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2098 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2099 if (hdr
->nlmsg_len
> sizeof(request
))
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
))
2161 { /* keep previously installed route. since it might have
2162 * still been removed by an address change, we install it
2163 * again but ignore the result */
2164 hydra
->kernel_interface
->add_route(hydra
->kernel_interface
,
2165 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2166 route
->src_ip
, route
->if_name
);
2167 this->mutex
->unlock(this->mutex
);
2168 route_entry_destroy(route
);
2171 /* uninstall previously installed route */
2172 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2173 old
->dst_net
, old
->prefixlen
, old
->gateway
,
2174 old
->src_ip
, old
->if_name
) != SUCCESS
)
2176 DBG1(DBG_KNL
, "error uninstalling route installed with "
2177 "policy %R === %R %N", fwd
->src_ts
,
2178 fwd
->dst_ts
, policy_dir_names
,
2181 route_entry_destroy(old
);
2182 policy
->route
= NULL
;
2185 DBG2(DBG_KNL
, "installing route: %R via %H src %H dev %s",
2186 fwd
->src_ts
, route
->gateway
, route
->src_ip
, route
->if_name
);
2187 switch (hydra
->kernel_interface
->add_route(
2188 hydra
->kernel_interface
, route
->dst_net
,
2189 route
->prefixlen
, route
->gateway
,
2190 route
->src_ip
, route
->if_name
))
2193 DBG1(DBG_KNL
, "unable to install source route for %H",
2197 /* route exists, do not uninstall */
2198 route_entry_destroy(route
);
2201 /* cache the installed route */
2202 policy
->route
= route
;
2211 this->mutex
->unlock(this->mutex
);
2215 METHOD(kernel_ipsec_t
, add_policy
, status_t
,
2216 private_kernel_netlink_ipsec_t
*this, host_t
*src
, host_t
*dst
,
2217 traffic_selector_t
*src_ts
, traffic_selector_t
*dst_ts
,
2218 policy_dir_t direction
, policy_type_t type
, ipsec_sa_cfg_t
*sa
,
2219 mark_t mark
, policy_priority_t priority
)
2221 policy_entry_t
*policy
, *current
;
2222 policy_sa_t
*assigned_sa
, *current_sa
;
2223 enumerator_t
*enumerator
;
2224 bool found
= FALSE
, update
= TRUE
;
2226 /* create a policy */
2228 .sel
= ts2selector(src_ts
, dst_ts
),
2229 .mark
= mark
.value
& mark
.mask
,
2230 .direction
= direction
,
2233 /* find the policy, which matches EXACTLY */
2234 this->mutex
->lock(this->mutex
);
2235 current
= this->policies
->get(this->policies
, policy
);
2238 /* use existing policy */
2241 DBG2(DBG_KNL
, "policy %R === %R %N (mark %u/0x%8x) "
2242 "already exists, increasing refcount",
2243 src_ts
, dst_ts
, policy_dir_names
, direction
,
2244 mark
.value
, mark
.mask
);
2248 DBG2(DBG_KNL
, "policy %R === %R %N "
2249 "already exists, increasing refcount",
2250 src_ts
, dst_ts
, policy_dir_names
, direction
);
2252 policy_entry_destroy(this, policy
);
2257 { /* use the new one, if we have no such policy */
2258 policy
->used_by
= linked_list_create();
2259 this->policies
->put(this->policies
, policy
, policy
);
2262 /* cache the assigned IPsec SA */
2263 assigned_sa
= policy_sa_create(this, direction
, type
, src
, dst
, src_ts
,
2265 assigned_sa
->priority
= get_priority(policy
, priority
);
2267 if (this->policy_history
)
2268 { /* insert the SA according to its priority */
2269 enumerator
= policy
->used_by
->create_enumerator(policy
->used_by
);
2270 while (enumerator
->enumerate(enumerator
, (void**)¤t_sa
))
2272 if (current_sa
->priority
>= assigned_sa
->priority
)
2278 policy
->used_by
->insert_before(policy
->used_by
, enumerator
,
2280 enumerator
->destroy(enumerator
);
2283 { /* simply insert it last and only update if it is not installed yet */
2284 policy
->used_by
->insert_last(policy
->used_by
, assigned_sa
);
2289 { /* we don't update the policy if the priority is lower than that of
2290 * the currently installed one */
2291 this->mutex
->unlock(this->mutex
);
2297 DBG2(DBG_KNL
, "%s policy %R === %R %N (mark %u/0x%8x)",
2298 found ?
"updating" : "adding", src_ts
, dst_ts
,
2299 policy_dir_names
, direction
, mark
.value
, mark
.mask
);
2303 DBG2(DBG_KNL
, "%s policy %R === %R %N",
2304 found ?
"updating" : "adding", src_ts
, dst_ts
,
2305 policy_dir_names
, direction
);
2308 if (add_policy_internal(this, policy
, assigned_sa
, found
) != SUCCESS
)
2310 DBG1(DBG_KNL
, "unable to %s policy %R === %R %N",
2311 found ?
"update" : "add", src_ts
, dst_ts
,
2312 policy_dir_names
, direction
);
2318 METHOD(kernel_ipsec_t
, query_policy
, status_t
,
2319 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2320 traffic_selector_t
*dst_ts
, policy_dir_t direction
, mark_t mark
,
2321 u_int32_t
*use_time
)
2323 netlink_buf_t request
;
2324 struct nlmsghdr
*out
= NULL
, *hdr
;
2325 struct xfrm_userpolicy_id
*policy_id
;
2326 struct xfrm_userpolicy_info
*policy
= NULL
;
2329 memset(&request
, 0, sizeof(request
));
2333 DBG2(DBG_KNL
, "querying policy %R === %R %N (mark %u/0x%8x)",
2334 src_ts
, dst_ts
, policy_dir_names
, direction
,
2335 mark
.value
, mark
.mask
);
2339 DBG2(DBG_KNL
, "querying policy %R === %R %N", src_ts
, dst_ts
,
2340 policy_dir_names
, direction
);
2342 hdr
= (struct nlmsghdr
*)request
;
2343 hdr
->nlmsg_flags
= NLM_F_REQUEST
;
2344 hdr
->nlmsg_type
= XFRM_MSG_GETPOLICY
;
2345 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2347 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2348 policy_id
->sel
= ts2selector(src_ts
, dst_ts
);
2349 policy_id
->dir
= direction
;
2353 struct xfrm_mark
*mrk
;
2354 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2356 rthdr
->rta_type
= XFRMA_MARK
;
2357 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2359 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2360 if (hdr
->nlmsg_len
> sizeof(request
))
2365 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2366 mrk
->v
= mark
.value
;
2370 if (this->socket_xfrm
->send(this->socket_xfrm
, hdr
, &out
, &len
) == SUCCESS
)
2373 while (NLMSG_OK(hdr
, len
))
2375 switch (hdr
->nlmsg_type
)
2377 case XFRM_MSG_NEWPOLICY
:
2379 policy
= (struct xfrm_userpolicy_info
*)NLMSG_DATA(hdr
);
2384 struct nlmsgerr
*err
= NLMSG_DATA(hdr
);
2385 DBG1(DBG_KNL
, "querying policy failed: %s (%d)",
2386 strerror(-err
->error
), -err
->error
);
2390 hdr
= NLMSG_NEXT(hdr
, len
);
2401 DBG2(DBG_KNL
, "unable to query policy %R === %R %N", src_ts
, dst_ts
,
2402 policy_dir_names
, direction
);
2407 if (policy
->curlft
.use_time
)
2409 /* we need the monotonic time, but the kernel returns system time. */
2410 *use_time
= time_monotonic(NULL
) - (time(NULL
) - policy
->curlft
.use_time
);
2421 METHOD(kernel_ipsec_t
, del_policy
, status_t
,
2422 private_kernel_netlink_ipsec_t
*this, traffic_selector_t
*src_ts
,
2423 traffic_selector_t
*dst_ts
, policy_dir_t direction
, u_int32_t reqid
,
2424 mark_t mark
, policy_priority_t prio
)
2426 policy_entry_t
*current
, policy
;
2427 enumerator_t
*enumerator
;
2428 policy_sa_t
*mapping
;
2429 netlink_buf_t request
;
2430 struct nlmsghdr
*hdr
;
2431 struct xfrm_userpolicy_id
*policy_id
;
2432 bool is_installed
= TRUE
;
2437 DBG2(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x)",
2438 src_ts
, dst_ts
, policy_dir_names
, direction
,
2439 mark
.value
, mark
.mask
);
2443 DBG2(DBG_KNL
, "deleting policy %R === %R %N",
2444 src_ts
, dst_ts
, policy_dir_names
, direction
);
2447 /* create a policy */
2448 memset(&policy
, 0, sizeof(policy_entry_t
));
2449 policy
.sel
= ts2selector(src_ts
, dst_ts
);
2450 policy
.mark
= mark
.value
& mark
.mask
;
2451 policy
.direction
= direction
;
2453 /* find the policy */
2454 this->mutex
->lock(this->mutex
);
2455 current
= this->policies
->get(this->policies
, &policy
);
2460 DBG1(DBG_KNL
, "deleting policy %R === %R %N (mark %u/0x%8x) "
2461 "failed, not found", src_ts
, dst_ts
, policy_dir_names
,
2462 direction
, mark
.value
, mark
.mask
);
2466 DBG1(DBG_KNL
, "deleting policy %R === %R %N failed, not found",
2467 src_ts
, dst_ts
, policy_dir_names
, direction
);
2469 this->mutex
->unlock(this->mutex
);
2473 if (this->policy_history
)
2474 { /* remove mapping to SA by reqid and priority */
2475 priority
= get_priority(current
, prio
);
2476 enumerator
= current
->used_by
->create_enumerator(current
->used_by
);
2477 while (enumerator
->enumerate(enumerator
, (void**)&mapping
))
2479 if (reqid
== mapping
->sa
->cfg
.reqid
&&
2480 priority
== mapping
->priority
)
2482 current
->used_by
->remove_at(current
->used_by
, enumerator
);
2483 policy_sa_destroy(mapping
, &direction
, this);
2486 is_installed
= FALSE
;
2488 enumerator
->destroy(enumerator
);
2491 { /* remove one of the SAs but don't update the policy */
2492 current
->used_by
->remove_last(current
->used_by
, (void**)&mapping
);
2493 policy_sa_destroy(mapping
, &direction
, this);
2494 is_installed
= FALSE
;
2497 if (current
->used_by
->get_count(current
->used_by
) > 0)
2498 { /* policy is used by more SAs, keep in kernel */
2499 DBG2(DBG_KNL
, "policy still used by another CHILD_SA, not removed");
2501 { /* no need to update as the policy was not installed for this SA */
2502 this->mutex
->unlock(this->mutex
);
2508 DBG2(DBG_KNL
, "updating policy %R === %R %N (mark %u/0x%8x)",
2509 src_ts
, dst_ts
, policy_dir_names
, direction
,
2510 mark
.value
, mark
.mask
);
2514 DBG2(DBG_KNL
, "updating policy %R === %R %N",
2515 src_ts
, dst_ts
, policy_dir_names
, direction
);
2518 current
->used_by
->get_first(current
->used_by
, (void**)&mapping
);
2519 if (add_policy_internal(this, current
, mapping
, TRUE
) != SUCCESS
)
2521 DBG1(DBG_KNL
, "unable to update policy %R === %R %N",
2522 src_ts
, dst_ts
, policy_dir_names
, direction
);
2528 memset(&request
, 0, sizeof(request
));
2530 hdr
= (struct nlmsghdr
*)request
;
2531 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2532 hdr
->nlmsg_type
= XFRM_MSG_DELPOLICY
;
2533 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_id
));
2535 policy_id
= (struct xfrm_userpolicy_id
*)NLMSG_DATA(hdr
);
2536 policy_id
->sel
= current
->sel
;
2537 policy_id
->dir
= direction
;
2541 struct xfrm_mark
*mrk
;
2542 struct rtattr
*rthdr
= XFRM_RTA(hdr
, struct xfrm_userpolicy_id
);
2544 rthdr
->rta_type
= XFRMA_MARK
;
2545 rthdr
->rta_len
= RTA_LENGTH(sizeof(struct xfrm_mark
));
2546 hdr
->nlmsg_len
+= RTA_ALIGN(rthdr
->rta_len
);
2547 if (hdr
->nlmsg_len
> sizeof(request
))
2552 mrk
= (struct xfrm_mark
*)RTA_DATA(rthdr
);
2553 mrk
->v
= mark
.value
;
2559 route_entry_t
*route
= current
->route
;
2560 if (hydra
->kernel_interface
->del_route(hydra
->kernel_interface
,
2561 route
->dst_net
, route
->prefixlen
, route
->gateway
,
2562 route
->src_ip
, route
->if_name
) != SUCCESS
)
2564 DBG1(DBG_KNL
, "error uninstalling route installed with "
2565 "policy %R === %R %N", src_ts
, dst_ts
,
2566 policy_dir_names
, direction
);
2570 this->policies
->remove(this->policies
, current
);
2571 policy_entry_destroy(this, current
);
2572 this->mutex
->unlock(this->mutex
);
2574 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2578 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N "
2579 "(mark %u/0x%8x)", src_ts
, dst_ts
, policy_dir_names
,
2580 direction
, mark
.value
, mark
.mask
);
2584 DBG1(DBG_KNL
, "unable to delete policy %R === %R %N",
2585 src_ts
, dst_ts
, policy_dir_names
, direction
);
2592 METHOD(kernel_ipsec_t
, flush_policies
, status_t
,
2593 private_kernel_netlink_ipsec_t
*this)
2595 netlink_buf_t request
;
2596 struct nlmsghdr
*hdr
;
2598 memset(&request
, 0, sizeof(request
));
2600 DBG2(DBG_KNL
, "flushing all policies from SPD");
2602 hdr
= (struct nlmsghdr
*)request
;
2603 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2604 hdr
->nlmsg_type
= XFRM_MSG_FLUSHPOLICY
;
2605 hdr
->nlmsg_len
= NLMSG_LENGTH(0); /* no data associated */
2607 /* by adding an rtattr of type XFRMA_POLICY_TYPE we could restrict this
2608 * to main or sub policies (default is main) */
2610 if (this->socket_xfrm
->send_ack(this->socket_xfrm
, hdr
) != SUCCESS
)
2612 DBG1(DBG_KNL
, "unable to flush SPD entries");
2619 METHOD(kernel_ipsec_t
, bypass_socket
, bool,
2620 private_kernel_netlink_ipsec_t
*this, int fd
, int family
)
2622 struct xfrm_userpolicy_info policy
;
2623 u_int sol
, ipsec_policy
;
2629 ipsec_policy
= IP_XFRM_POLICY
;
2633 ipsec_policy
= IPV6_XFRM_POLICY
;
2639 memset(&policy
, 0, sizeof(policy
));
2640 policy
.action
= XFRM_POLICY_ALLOW
;
2641 policy
.sel
.family
= family
;
2643 policy
.dir
= XFRM_POLICY_OUT
;
2644 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2646 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2650 policy
.dir
= XFRM_POLICY_IN
;
2651 if (setsockopt(fd
, sol
, ipsec_policy
, &policy
, sizeof(policy
)) < 0)
2653 DBG1(DBG_KNL
, "unable to set IPSEC_POLICY on socket: %s",
2660 METHOD(kernel_ipsec_t
, destroy
, void,
2661 private_kernel_netlink_ipsec_t
*this)
2663 enumerator_t
*enumerator
;
2664 policy_entry_t
*policy
;
2668 this->job
->cancel(this->job
);
2670 if (this->socket_xfrm_events
> 0)
2672 close(this->socket_xfrm_events
);
2674 DESTROY_IF(this->socket_xfrm
);
2675 enumerator
= this->policies
->create_enumerator(this->policies
);
2676 while (enumerator
->enumerate(enumerator
, &policy
, &policy
))
2678 policy_entry_destroy(this, policy
);
2680 enumerator
->destroy(enumerator
);
2681 this->policies
->destroy(this->policies
);
2682 this->sas
->destroy(this->sas
);
2683 this->mutex
->destroy(this->mutex
);
2688 * Described in header.
2690 kernel_netlink_ipsec_t
*kernel_netlink_ipsec_create()
2692 private_kernel_netlink_ipsec_t
*this;
2693 struct sockaddr_nl addr
;
2699 .get_spi
= _get_spi
,
2700 .get_cpi
= _get_cpi
,
2702 .update_sa
= _update_sa
,
2703 .query_sa
= _query_sa
,
2705 .flush_sas
= _flush_sas
,
2706 .add_policy
= _add_policy
,
2707 .query_policy
= _query_policy
,
2708 .del_policy
= _del_policy
,
2709 .flush_policies
= _flush_policies
,
2710 .bypass_socket
= _bypass_socket
,
2711 .destroy
= _destroy
,
2714 .policies
= hashtable_create((hashtable_hash_t
)policy_hash
,
2715 (hashtable_equals_t
)policy_equals
, 32),
2716 .sas
= hashtable_create((hashtable_hash_t
)ipsec_sa_hash
,
2717 (hashtable_equals_t
)ipsec_sa_equals
, 32),
2718 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
2719 .policy_history
= TRUE
,
2720 .install_routes
= lib
->settings
->get_bool(lib
->settings
,
2721 "%s.install_routes", TRUE
, hydra
->daemon
),
2722 .replay_window
= lib
->settings
->get_int(lib
->settings
,
2723 "%s.replay_window", DEFAULT_REPLAY_WINDOW
, hydra
->daemon
),
2726 this->replay_bmp
= (this->replay_window
+ sizeof(u_int32_t
) * 8 - 1) /
2727 (sizeof(u_int32_t
) * 8);
2729 if (streq(hydra
->daemon
, "pluto"))
2730 { /* no routes for pluto, they are installed via updown script */
2731 this->install_routes
= FALSE
;
2732 /* no policy history for pluto */
2733 this->policy_history
= FALSE
;
2736 /* disable lifetimes for allocated SPIs in kernel */
2737 fd
= open("/proc/sys/net/core/xfrm_acq_expires", O_WRONLY
);
2740 ignore_result(write(fd
, "165", 3));
2744 this->socket_xfrm
= netlink_socket_create(NETLINK_XFRM
);
2745 if (!this->socket_xfrm
)
2751 memset(&addr
, 0, sizeof(addr
));
2752 addr
.nl_family
= AF_NETLINK
;
2754 /* create and bind XFRM socket for ACQUIRE, EXPIRE, MIGRATE & MAPPING */
2755 this->socket_xfrm_events
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_XFRM
);
2756 if (this->socket_xfrm_events
<= 0)
2758 DBG1(DBG_KNL
, "unable to create XFRM event socket");
2762 addr
.nl_groups
= XFRMNLGRP(ACQUIRE
) | XFRMNLGRP(EXPIRE
) |
2763 XFRMNLGRP(MIGRATE
) | XFRMNLGRP(MAPPING
);
2764 if (bind(this->socket_xfrm_events
, (struct sockaddr
*)&addr
, sizeof(addr
)))
2766 DBG1(DBG_KNL
, "unable to bind XFRM event socket");
2770 this->job
= callback_job_create_with_prio((callback_job_cb_t
)receive_events
,
2771 this, NULL
, NULL
, JOB_PRIO_CRITICAL
);
2772 lib
->processor
->queue_job(lib
->processor
, (job_t
*)this->job
);
2774 return &this->public;